From 4c24ae8b133eacf6753e0862a40005cd8352ca04 Mon Sep 17 00:00:00 2001 From: Kien Nguyen Date: Sat, 6 Mar 2021 14:46:30 +0700 Subject: [PATCH] Add buildinfo method Co-authored-by: Lili Cosic Signed-off-by: Kien Nguyen --- api/prometheus/v1/api.go | 30 ++++++++++++++++++++++++++++ api/prometheus/v1/api_test.go | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 20205b9e6..44309df7d 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -135,6 +135,7 @@ const ( epCleanTombstones = apiPrefix + "/admin/tsdb/clean_tombstones" epConfig = apiPrefix + "/status/config" epFlags = apiPrefix + "/status/flags" + epBuildinfo = apiPrefix + "/status/buildinfo" epRuntimeinfo = apiPrefix + "/status/runtimeinfo" epTSDB = apiPrefix + "/status/tsdb" ) @@ -238,6 +239,8 @@ type API interface { Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error) // QueryRange performs a query for the given range. QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error) + // Buildinfo returns various build information properties about the Prometheus server + Buildinfo(ctx context.Context) (BuildinfoResult, error) // Runtimeinfo returns the various runtime information properties about the Prometheus server. Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) // Series finds series by label matchers. @@ -281,6 +284,16 @@ type ConfigResult struct { // FlagsResult contains the result from querying the flag endpoint. type FlagsResult map[string]string +// BuildinfoResult contains the results from querying the buildinfo endpoint. +type BuildinfoResult struct { + Version string `json:"version"` + Revision string `json:"revision"` + Branch string `json:"branch"` + BuildUser string `json:"buildUser"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` +} + // RuntimeinfoResult contains the result from querying the runtimeinfo endpoint. type RuntimeinfoResult struct { StartTime time.Time `json:"startTime"` @@ -674,6 +687,23 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) { return res, json.Unmarshal(body, &res) } +func (h *httpAPI) Buildinfo(ctx context.Context) (BuildinfoResult, error) { + u := h.client.URL(epBuildinfo, nil) + + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + return BuildinfoResult{}, err + } + + _, body, _, err := h.client.Do(ctx, req) + if err != nil { + return BuildinfoResult{}, err + } + + var res BuildinfoResult + return res, json.Unmarshal(body, &res) +} + func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) { u := h.client.URL(epRuntimeinfo, nil) diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index 0529adb3a..a1f051544 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -144,6 +144,13 @@ func TestAPIs(t *testing.T) { } } + doBuildinfo := func() func() (interface{}, Warnings, error) { + return func() (interface{}, Warnings, error) { + v, err := promAPI.Buildinfo(context.Background()) + return v, nil, err + } + } + doRuntimeinfo := func() func() (interface{}, Warnings, error) { return func() (interface{}, Warnings, error) { v, err := promAPI.Runtimeinfo(context.Background()) @@ -635,6 +642,36 @@ func TestAPIs(t *testing.T) { err: fmt.Errorf("some error"), }, + { + do: doBuildinfo(), + reqMethod: "GET", + reqPath: "/api/v1/status/buildinfo", + inErr: fmt.Errorf("some error"), + err: fmt.Errorf("some error"), + }, + + { + do: doBuildinfo(), + reqMethod: "GET", + reqPath: "/api/v1/status/buildinfo", + inRes: map[string]interface{}{ + "version": "2.23.0", + "revision": "26d89b4b0776fe4cd5a3656dfa520f119a375273", + "branch": "HEAD", + "buildUser": "root@37609b3a0a21", + "buildDate": "20201126-10:56:17", + "goVersion": "go1.15.5", + }, + res: BuildinfoResult{ + Version: "2.23.0", + Revision: "26d89b4b0776fe4cd5a3656dfa520f119a375273", + Branch: "HEAD", + BuildUser: "root@37609b3a0a21", + BuildDate: "20201126-10:56:17", + GoVersion: "go1.15.5", + }, + }, + { do: doRuntimeinfo(), reqMethod: "GET",