Skip to content

Commit e8b72d0

Browse files
authored
Merge pull request #841 from ntk148v/buildinfo-api
Add buildinfo method
2 parents babeb35 + 4c24ae8 commit e8b72d0

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const (
135135
epCleanTombstones = apiPrefix + "/admin/tsdb/clean_tombstones"
136136
epConfig = apiPrefix + "/status/config"
137137
epFlags = apiPrefix + "/status/flags"
138+
epBuildinfo = apiPrefix + "/status/buildinfo"
138139
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
139140
epTSDB = apiPrefix + "/status/tsdb"
140141
)
@@ -238,6 +239,8 @@ type API interface {
238239
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
239240
// QueryRange performs a query for the given range.
240241
QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error)
242+
// Buildinfo returns various build information properties about the Prometheus server
243+
Buildinfo(ctx context.Context) (BuildinfoResult, error)
241244
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
242245
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
243246
// Series finds series by label matchers.
@@ -281,6 +284,16 @@ type ConfigResult struct {
281284
// FlagsResult contains the result from querying the flag endpoint.
282285
type FlagsResult map[string]string
283286

287+
// BuildinfoResult contains the results from querying the buildinfo endpoint.
288+
type BuildinfoResult struct {
289+
Version string `json:"version"`
290+
Revision string `json:"revision"`
291+
Branch string `json:"branch"`
292+
BuildUser string `json:"buildUser"`
293+
BuildDate string `json:"buildDate"`
294+
GoVersion string `json:"goVersion"`
295+
}
296+
284297
// RuntimeinfoResult contains the result from querying the runtimeinfo endpoint.
285298
type RuntimeinfoResult struct {
286299
StartTime time.Time `json:"startTime"`
@@ -674,6 +687,23 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
674687
return res, json.Unmarshal(body, &res)
675688
}
676689

690+
func (h *httpAPI) Buildinfo(ctx context.Context) (BuildinfoResult, error) {
691+
u := h.client.URL(epBuildinfo, nil)
692+
693+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
694+
if err != nil {
695+
return BuildinfoResult{}, err
696+
}
697+
698+
_, body, _, err := h.client.Do(ctx, req)
699+
if err != nil {
700+
return BuildinfoResult{}, err
701+
}
702+
703+
var res BuildinfoResult
704+
return res, json.Unmarshal(body, &res)
705+
}
706+
677707
func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
678708
u := h.client.URL(epRuntimeinfo, nil)
679709

api/prometheus/v1/api_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ func TestAPIs(t *testing.T) {
144144
}
145145
}
146146

147+
doBuildinfo := func() func() (interface{}, Warnings, error) {
148+
return func() (interface{}, Warnings, error) {
149+
v, err := promAPI.Buildinfo(context.Background())
150+
return v, nil, err
151+
}
152+
}
153+
147154
doRuntimeinfo := func() func() (interface{}, Warnings, error) {
148155
return func() (interface{}, Warnings, error) {
149156
v, err := promAPI.Runtimeinfo(context.Background())
@@ -635,6 +642,36 @@ func TestAPIs(t *testing.T) {
635642
err: fmt.Errorf("some error"),
636643
},
637644

645+
{
646+
do: doBuildinfo(),
647+
reqMethod: "GET",
648+
reqPath: "/api/v1/status/buildinfo",
649+
inErr: fmt.Errorf("some error"),
650+
err: fmt.Errorf("some error"),
651+
},
652+
653+
{
654+
do: doBuildinfo(),
655+
reqMethod: "GET",
656+
reqPath: "/api/v1/status/buildinfo",
657+
inRes: map[string]interface{}{
658+
"version": "2.23.0",
659+
"revision": "26d89b4b0776fe4cd5a3656dfa520f119a375273",
660+
"branch": "HEAD",
661+
"buildUser": "root@37609b3a0a21",
662+
"buildDate": "20201126-10:56:17",
663+
"goVersion": "go1.15.5",
664+
},
665+
res: BuildinfoResult{
666+
Version: "2.23.0",
667+
Revision: "26d89b4b0776fe4cd5a3656dfa520f119a375273",
668+
Branch: "HEAD",
669+
BuildUser: "root@37609b3a0a21",
670+
BuildDate: "20201126-10:56:17",
671+
GoVersion: "go1.15.5",
672+
},
673+
},
674+
638675
{
639676
do: doRuntimeinfo(),
640677
reqMethod: "GET",

0 commit comments

Comments
 (0)