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: support wal replay status api #944

Merged
merged 1 commit into from Jan 5, 2022
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
27 changes: 27 additions & 0 deletions api/prometheus/v1/api.go
Expand Up @@ -139,6 +139,7 @@ const (
epBuildinfo = apiPrefix + "/status/buildinfo"
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
epTSDB = apiPrefix + "/status/tsdb"
epWalReplay = apiPrefix + "/status/walreplay"
)

// AlertState models the state of an alert.
Expand Down Expand Up @@ -261,6 +262,8 @@ type API interface {
Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error)
// TSDB returns the cardinality statistics.
TSDB(ctx context.Context) (TSDBResult, error)
// WalReplay returns the current replay status of the wal.
WalReplay(ctx context.Context) (WalReplayStatus, error)
}

// AlertsResult contains the result from querying the alerts endpoint.
Expand Down Expand Up @@ -437,6 +440,13 @@ type TSDBResult struct {
SeriesCountByLabelValuePair []Stat `json:"seriesCountByLabelValuePair"`
}

// WalReplayStatus represents the wal replay status.
type WalReplayStatus struct {
Min int `json:"min"`
Max int `json:"max"`
Current int `json:"current"`
}

// Stat models information about statistic value.
type Stat struct {
Name string `json:"name"`
Expand Down Expand Up @@ -984,6 +994,23 @@ func (h *httpAPI) TSDB(ctx context.Context) (TSDBResult, error) {
return res, json.Unmarshal(body, &res)
}

func (h *httpAPI) WalReplay(ctx context.Context) (WalReplayStatus, error) {
u := h.client.URL(epWalReplay, nil)

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

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

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

func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error) {
u := h.client.URL(epQueryExemplars, nil)
q := u.Query()
Expand Down
31 changes: 31 additions & 0 deletions api/prometheus/v1/api_test.go
Expand Up @@ -230,6 +230,13 @@ func TestAPIs(t *testing.T) {
}
}

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

doQueryExemplars := func(query string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.QueryExemplars(context.Background(), query, startTime, endTime)
Expand Down Expand Up @@ -1198,6 +1205,30 @@ func TestAPIs(t *testing.T) {
},
},

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

{
do: doWalReply(),
reqMethod: "GET",
reqPath: "/api/v1/status/walreplay",
inRes: map[string]interface{}{
"min": 2,
"max": 5,
"current": 40,
},
res: WalReplayStatus{
Min: 2,
Max: 5,
Current: 40,
},
},

{
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
reqMethod: "GET",
Expand Down