Add buildinfo method

Co-authored-by: Lili Cosic <cosiclili@gmail.com>
Signed-off-by: Kien Nguyen <kiennt2609@gmail.com>
This commit is contained in:
Kien Nguyen 2021-03-06 14:46:30 +07:00
parent babeb356a5
commit 4c24ae8b13
2 changed files with 67 additions and 0 deletions

View File

@ -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)

View File

@ -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",