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

api/prometheus/v1/api.go: Add support for status/runtimeinfo endpoint #755

Merged
merged 2 commits into from Jun 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
// RuntimeInfo returns the various runtime information properties about the Prometheus server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this, as the endpoint is /runtimeinfo not /runtime_info, at least that was the logic I used here. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, not a blocker, but this looks weird (:

Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
RuntimeInfo(ctx context.Context) (RuntimeinfoResult, error)
Suggested change
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like matches the other methods, but why we discard the status code completely? No point in marshaling if we don't check 2** status. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you mentioned this just uses what the other functions use, but the custom .Do does check for status :) https://github.com/prometheus/client_golang/blob/master/api/prometheus/v1/api.go#L880

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is something to fix in follow up PRs if any, just it feels wrong, will add issue to follow up (:

Copy link
Contributor Author

@lilic lilic Jun 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we should fix it for all the functions! If you open issue can you ping me on it, thanks!

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