Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildinfo method #841

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
ntk148v marked this conversation as resolved.
Show resolved Hide resolved
GoVersion string `json:"goVersion"`
}

// RuntimeinfoResult contains the result from querying the runtimeinfo endpoint.
type RuntimeinfoResult struct {
StartTime time.Time `json:"startTime"`
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 37 additions & 0 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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",
Expand Down