Skip to content

Commit 9e9caa7

Browse files
authored
Add a static handler for buildinfo static API (#5009)
* add a roundtripper for buildinfo static API Signed-off-by: Ben Ye <[email protected]> * use static handler rather than tripperware Signed-off-by: Ben Ye <[email protected]> Signed-off-by: Ben Ye <[email protected]>
1 parent b9beb17 commit 9e9caa7

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

pkg/api/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ func (a *API) RegisterQueryable(
366366

367367
// RegisterQueryAPI registers the Prometheus API routes with the provided handler.
368368
func (a *API) RegisterQueryAPI(handler http.Handler) {
369+
infoHandler := &buildInfoHandler{logger: a.logger}
369370
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/read"), handler, true, "POST")
370371
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/query"), handler, true, "GET", "POST")
371372
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/query_range"), handler, true, "GET", "POST")
@@ -374,7 +375,7 @@ func (a *API) RegisterQueryAPI(handler http.Handler) {
374375
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/label/{name}/values"), handler, true, "GET")
375376
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/series"), handler, true, "GET", "POST", "DELETE")
376377
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/metadata"), handler, true, "GET")
377-
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/status/buildinfo"), handler, true, "GET")
378+
a.RegisterRoute(path.Join(a.cfg.PrometheusHTTPPrefix, "/api/v1/status/buildinfo"), infoHandler, true, "GET")
378379

379380
// Register Legacy Routers
380381
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/read"), handler, true, "POST")
@@ -385,7 +386,7 @@ func (a *API) RegisterQueryAPI(handler http.Handler) {
385386
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/label/{name}/values"), handler, true, "GET")
386387
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/series"), handler, true, "GET", "POST", "DELETE")
387388
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/metadata"), handler, true, "GET")
388-
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/status/buildinfo"), handler, true, "GET")
389+
a.RegisterRoute(path.Join(a.cfg.LegacyHTTPPrefix, "/api/v1/status/buildinfo"), infoHandler, true, "GET")
389390
}
390391

391392
// RegisterQueryFrontend registers the Prometheus routes supported by the

pkg/api/handlers.go

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

33
import (
44
"context"
5+
"encoding/json"
56
"html/template"
67
"net/http"
78
"path"
89
"sync"
910

1011
"github.com/go-kit/log"
12+
"github.com/go-kit/log/level"
1113
"github.com/gorilla/mux"
1214
"github.com/grafana/regexp"
1315
"github.com/pkg/errors"
@@ -282,3 +284,36 @@ func NewQuerierHandler(
282284
// Track execution time.
283285
return stats.NewWallTimeMiddleware().Wrap(router)
284286
}
287+
288+
type buildInfoHandler struct {
289+
logger log.Logger
290+
}
291+
292+
type buildInfoResponse struct {
293+
Status string `json:"status"`
294+
Data *v1.PrometheusVersion `json:"data"`
295+
}
296+
297+
func (h *buildInfoHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) {
298+
infoResponse := buildInfoResponse{
299+
Status: "success",
300+
Data: &v1.PrometheusVersion{
301+
Version: version.Version,
302+
Branch: version.Branch,
303+
Revision: version.Revision,
304+
BuildUser: version.BuildUser,
305+
BuildDate: version.BuildDate,
306+
GoVersion: version.GoVersion,
307+
},
308+
}
309+
output, err := json.Marshal(infoResponse)
310+
if err != nil {
311+
level.Error(h.logger).Log("msg", "marshal build info response", "error", err)
312+
http.Error(writer, err.Error(), http.StatusInternalServerError)
313+
return
314+
}
315+
writer.WriteHeader(http.StatusOK)
316+
if _, err := writer.Write(output); err != nil {
317+
level.Error(h.logger).Log("msg", "write build info response", "error", err)
318+
}
319+
}

pkg/cortex/modules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ func (t *Cortex) initQueryFrontendTripperware() (serv services.Service, err erro
448448
}
449449

450450
instantQueryMiddlewares, err := instantquery.Middlewares(util_log.Logger, t.Overrides)
451-
452451
if err != nil {
453452
return nil, err
454453
}

pkg/querier/tripperware/queryrange/query_range_middlewares_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestRoundTrip(t *testing.T) {
5050
nil,
5151
nil,
5252
)
53+
require.NoError(t, err)
5354

5455
tw := tripperware.NewQueryTripperware(log.NewNopLogger(),
5556
nil,
@@ -60,10 +61,6 @@ func TestRoundTrip(t *testing.T) {
6061
nil,
6162
)
6263

63-
if err != nil {
64-
t.Fatal(err)
65-
}
66-
6764
for i, tc := range []struct {
6865
path, expectedBody string
6966
}{

pkg/querier/tripperware/roundtrip.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func NewQueryTripperware(
122122
return RoundTripFunc(func(r *http.Request) (*http.Response, error) {
123123
isQuery := strings.HasSuffix(r.URL.Path, "/query")
124124
isQueryRange := strings.HasSuffix(r.URL.Path, "/query_range")
125+
125126
op := "query"
126127
if isQueryRange {
127128
op = "query_range"

pkg/querier/tripperware/roundtrip_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func TestRoundTrip(t *testing.T) {
9595
mockCodec{},
9696
)
9797

98-
if err != nil {
99-
t.Fatal(err)
100-
}
101-
10298
for _, tc := range []struct {
10399
path, expectedBody string
104100
}{

0 commit comments

Comments
 (0)