Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit ecf092a

Browse files
Blagoj Atanasovskiatanasovskib
authored andcommitted
Modify respond method to return proper label values
1 parent a6f0ba8 commit ecf092a

File tree

8 files changed

+194
-47
lines changed

8 files changed

+194
-47
lines changed

pkg/api/label_values.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/NYTimes/gziphandler"
77
"github.com/prometheus/common/model"
88
"github.com/prometheus/common/route"
9+
"github.com/timescale/timescale-prometheus/pkg/promql"
910
"github.com/timescale/timescale-prometheus/pkg/query"
1011
"math"
1112
"net/http"
@@ -26,13 +27,16 @@ func LabelValues(queriable *query.Queryable) http.Handler {
2627
respondError(w, http.StatusInternalServerError, err, "internal")
2728
return
2829
}
29-
names, warnings, err := querier.LabelValues(name)
30+
var values labelsValue
31+
values, warnings, err := querier.LabelValues(name)
3032
if err != nil {
3133
respondError(w, http.StatusInternalServerError, err, "internal")
3234
return
3335
}
3436

35-
respond(w, names, warnings)
37+
respondLabels(w, &promql.Result{
38+
Value: values,
39+
}, warnings)
3640
})
3741

3842
return gziphandler.GzipHandler(hf)

pkg/api/labels.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ package api
22

33
import (
44
"context"
5+
"encoding/json"
56
"github.com/NYTimes/gziphandler"
7+
"github.com/prometheus/prometheus/promql/parser"
8+
"github.com/prometheus/prometheus/storage"
9+
"github.com/timescale/timescale-prometheus/pkg/promql"
610
"github.com/timescale/timescale-prometheus/pkg/query"
711
"math"
812
"net/http"
13+
"strings"
914
)
1015

16+
type labelsValue []string
17+
18+
func (l labelsValue) Type() parser.ValueType {
19+
return parser.ValueTypeNone
20+
}
21+
22+
func (l labelsValue) String() string {
23+
return strings.Join(l, "\n")
24+
}
25+
1126
func Labels(queriable *query.Queryable) http.Handler {
1227
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1328
w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -17,14 +32,25 @@ func Labels(queriable *query.Queryable) http.Handler {
1732
respondError(w, http.StatusInternalServerError, err, "internal")
1833
return
1934
}
35+
var names labelsValue
2036
names, warnings, err := querier.LabelNames()
2137
if err != nil {
2238
respondError(w, http.StatusInternalServerError, err, "internal")
2339
return
2440
}
25-
26-
respond(w, names, warnings)
41+
respondLabels(w, &promql.Result{
42+
Value: names,
43+
}, warnings)
2744
})
2845

2946
return gziphandler.GzipHandler(hf)
3047
}
48+
49+
func respondLabels(w http.ResponseWriter, res *promql.Result, warnings storage.Warnings) {
50+
setHeaders(w, res, warnings)
51+
resp := &response{
52+
Status: "success",
53+
Data: res.Value,
54+
}
55+
_ = json.NewEncoder(w).Encode(resp)
56+
}

pkg/api/query.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/prometheus/common/model"
1515
"github.com/prometheus/prometheus/promql/parser"
1616
"github.com/prometheus/prometheus/storage"
17-
"github.com/prometheus/prometheus/util/stats"
1817
"github.com/timescale/timescale-prometheus/pkg/log"
1918
"github.com/timescale/timescale-prometheus/pkg/promql"
2019
"github.com/timescale/timescale-prometheus/pkg/query"
@@ -74,19 +73,26 @@ func Query(queryEngine *promql.Engine, queryable *query.Queryable) http.Handler
7473
return
7574
}
7675

77-
respond(w, res, res.Warnings)
76+
respondQuery(w, res, res.Warnings)
7877
})
7978

8079
return gziphandler.GzipHandler(hf)
8180
}
8281

83-
func respond(w http.ResponseWriter, res *promql.Result, warnings storage.Warnings) {
82+
func setHeaders(w http.ResponseWriter, res *promql.Result, warnings storage.Warnings) {
8483
w.Header().Set("Content-Type", "application/json")
8584
if warnings != nil && len(warnings) > 0 {
8685
w.Header().Set("Cache-Control", "no-store")
8786
}
88-
w.WriteHeader(http.StatusOK)
87+
if res != nil && res.Value != nil {
88+
w.WriteHeader(http.StatusOK)
89+
} else {
90+
w.WriteHeader(http.StatusNoContent)
91+
}
92+
}
8993

94+
func respondQuery(w http.ResponseWriter, res *promql.Result, warnings storage.Warnings) {
95+
setHeaders(w, res, warnings)
9096
switch resVal := res.Value.(type) {
9197
case promql.Vector:
9298
warnings := make([]string, 0, len(res.Warnings))
@@ -113,7 +119,6 @@ func respond(w http.ResponseWriter, res *promql.Result, warnings storage.Warning
113119
}
114120
_ = json.NewEncoder(w).Encode(resp)
115121
}
116-
117122
}
118123

119124
func respondError(w http.ResponseWriter, status int, err error, errType string) {

pkg/api/query_range.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"context"
5-
"github.com/prometheus/prometheus/util/stats"
65
"net/http"
76

87
"github.com/NYTimes/gziphandler"
@@ -101,15 +100,7 @@ func QueryRange(queryEngine *promql.Engine, queriable *query.Queryable) http.Han
101100
return
102101
}
103102

104-
var qs *stats.QueryStats
105-
if r.FormValue("stats") != "" {
106-
qs = stats.NewQueryStats(qry.Stats())
107-
}
108-
respond(w, &queryData{
109-
Result: res.Value,
110-
ResultType: res.Value.Type(),
111-
Stats: qs,
112-
}, res.Warnings)
103+
respondQuery(w, res, res.Warnings)
113104
})
114105

115106
return gziphandler.GzipHandler(hf)

pkg/pgmodel/end_to_end_tests/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestMain(m *testing.M) {
8787

8888
func withDB(t testing.TB, DBName string, f func(db *pgxpool.Pool, t testing.TB)) {
8989
testhelpers.WithDB(t, DBName, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
90-
performMigrate(t, DBName, connectURL)
90+
performMigrate(t, connectURL)
9191

9292
//need to get a new pool after the Migrate to catch any GUC changes made during Migrate
9393
db, err := pgxpool.Connect(context.Background(), connectURL)
@@ -101,7 +101,7 @@ func withDB(t testing.TB, DBName string, f func(db *pgxpool.Pool, t testing.TB))
101101
})
102102
}
103103

104-
func performMigrate(t testing.TB, DBName string, connectURL string) {
104+
func performMigrate(t testing.TB, connectURL string) {
105105
dbStd, err := sql.Open("pgx", connectURL)
106106
defer func() {
107107
err := dbStd.Close()

pkg/pgmodel/end_to_end_tests/migrate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestMigrateTwice(t *testing.T) {
4141
t.Skip("skipping integration test")
4242
}
4343
testhelpers.WithDB(t, *testDatabase, testhelpers.NoSuperuser, func(db *pgxpool.Pool, t testing.TB, connectURL string) {
44-
performMigrate(t, *testDatabase, connectURL)
45-
performMigrate(t, *testDatabase, connectURL)
44+
performMigrate(t, connectURL)
45+
performMigrate(t, connectURL)
4646
})
4747
}

0 commit comments

Comments
 (0)