Skip to content

Commit

Permalink
Merge pull request #755 from lilic/add-runtimeinfo
Browse files Browse the repository at this point in the history
api/prometheus/v1/api.go: Add support for status/runtimeinfo endpoint
  • Loading branch information
beorn7 committed Jun 3, 2020
2 parents 59508bb + 6c43f2e commit 056e8af
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
36 changes: 36 additions & 0 deletions api/prometheus/v1/api.go
Expand Up @@ -137,6 +137,7 @@ const (
epCleanTombstones = apiPrefix + "/admin/tsdb/clean_tombstones"
epConfig = apiPrefix + "/status/config"
epFlags = apiPrefix + "/status/flags"
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
)

// AlertState models the state of an alert.
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)
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
// Series finds series by label matchers.
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, Warnings, error)
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
Expand Down Expand Up @@ -277,6 +280,22 @@ type ConfigResult struct {
// FlagsResult contains the result from querying the flag endpoint.
type FlagsResult map[string]string

// RuntimeinfoResult contains the result from querying the runtimeinfo endpoint.
type RuntimeinfoResult struct {
StartTime string `json:"startTime"`
CWD string `json:"CWD"`
ReloadConfigSuccess bool `json:"reloadConfigSuccess"`
LastConfigTime string `json:"lastConfigTime"`
ChunkCount int `json:"chunkCount"`
TimeSeriesCount int `json:"timeSeriesCount"`
CorruptionCount int `json:"corruptionCount"`
GoroutineCount int `json:"goroutineCount"`
GOMAXPROCS int `json:"GOMAXPROCS"`
GOGC string `json:"GOGC"`
GODEBUG string `json:"GODEBUG"`
StorageRetention string `json:"storageRetention"`
}

// SnapshotResult contains the result from querying the snapshot endpoint.
type SnapshotResult struct {
Name string `json:"name"`
Expand Down Expand Up @@ -640,6 +659,23 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
return res, json.Unmarshal(body, &res)
}

func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
u := h.client.URL(epRuntimeinfo, nil)

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return RuntimeinfoResult{}, err
}

_, body, _, err := h.client.Do(ctx, req)
if err != nil {
return RuntimeinfoResult{}, err
}

var res RuntimeinfoResult
return res, json.Unmarshal(body, &res)
}

func (h *httpAPI) LabelNames(ctx context.Context) ([]string, Warnings, error) {
u := h.client.URL(epLabels, nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
Expand Down
49 changes: 49 additions & 0 deletions api/prometheus/v1/api_test.go
Expand Up @@ -144,6 +144,13 @@ func TestAPIs(t *testing.T) {
}
}

doRuntimeinfo := func() func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.Runtimeinfo(context.Background())
return v, nil, err
}
}

doLabelNames := func(label string) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.LabelNames(context.Background())
Expand Down Expand Up @@ -605,6 +612,48 @@ func TestAPIs(t *testing.T) {
err: fmt.Errorf("some error"),
},

{
do: doRuntimeinfo(),
reqMethod: "GET",
reqPath: "/api/v1/status/runtimeinfo",
inErr: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

{
do: doRuntimeinfo(),
reqMethod: "GET",
reqPath: "/api/v1/status/runtimeinfo",
inRes: map[string]interface{}{
"startTime": "2020-05-18T15:52:53.4503113Z",
"CWD": "/prometheus",
"reloadConfigSuccess": true,
"lastConfigTime": "2020-05-18T15:52:56Z",
"chunkCount": 72692,
"timeSeriesCount": 18476,
"corruptionCount": 0,
"goroutineCount": 217,
"GOMAXPROCS": 2,
"GOGC": "100",
"GODEBUG": "allocfreetrace",
"storageRetention": "1d",
},
res: RuntimeinfoResult{
StartTime: "2020-05-18T15:52:53.4503113Z",
CWD: "/prometheus",
ReloadConfigSuccess: true,
LastConfigTime: "2020-05-18T15:52:56Z",
ChunkCount: 72692,
TimeSeriesCount: 18476,
CorruptionCount: 0,
GoroutineCount: 217,
GOMAXPROCS: 2,
GOGC: "100",
GODEBUG: "allocfreetrace",
StorageRetention: "1d",
},
},

{
do: doAlertManagers(),
reqMethod: "GET",
Expand Down

0 comments on commit 056e8af

Please sign in to comment.