Skip to content

Commit

Permalink
Merge pull request #944 from yeya24/add-wal-replay-status
Browse files Browse the repository at this point in the history
API: support wal replay status api
  • Loading branch information
kakkoyun committed Jan 5, 2022
2 parents 98fbd99 + 440c09d commit e6e54e8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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

0 comments on commit e6e54e8

Please sign in to comment.