Skip to content

Commit e6e54e8

Browse files
authored
Merge pull request #944 from yeya24/add-wal-replay-status
API: support wal replay status api
2 parents 98fbd99 + 440c09d commit e6e54e8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const (
139139
epBuildinfo = apiPrefix + "/status/buildinfo"
140140
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
141141
epTSDB = apiPrefix + "/status/tsdb"
142+
epWalReplay = apiPrefix + "/status/walreplay"
142143
)
143144

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

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

443+
// WalReplayStatus represents the wal replay status.
444+
type WalReplayStatus struct {
445+
Min int `json:"min"`
446+
Max int `json:"max"`
447+
Current int `json:"current"`
448+
}
449+
440450
// Stat models information about statistic value.
441451
type Stat struct {
442452
Name string `json:"name"`
@@ -984,6 +994,23 @@ func (h *httpAPI) TSDB(ctx context.Context) (TSDBResult, error) {
984994
return res, json.Unmarshal(body, &res)
985995
}
986996

997+
func (h *httpAPI) WalReplay(ctx context.Context) (WalReplayStatus, error) {
998+
u := h.client.URL(epWalReplay, nil)
999+
1000+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
1001+
if err != nil {
1002+
return WalReplayStatus{}, err
1003+
}
1004+
1005+
_, body, _, err := h.client.Do(ctx, req)
1006+
if err != nil {
1007+
return WalReplayStatus{}, err
1008+
}
1009+
1010+
var res WalReplayStatus
1011+
return res, json.Unmarshal(body, &res)
1012+
}
1013+
9871014
func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error) {
9881015
u := h.client.URL(epQueryExemplars, nil)
9891016
q := u.Query()

api/prometheus/v1/api_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ func TestAPIs(t *testing.T) {
230230
}
231231
}
232232

233+
doWalReply := func() func() (interface{}, Warnings, error) {
234+
return func() (interface{}, Warnings, error) {
235+
v, err := promAPI.WalReplay(context.Background())
236+
return v, nil, err
237+
}
238+
}
239+
233240
doQueryExemplars := func(query string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
234241
return func() (interface{}, Warnings, error) {
235242
v, err := promAPI.QueryExemplars(context.Background(), query, startTime, endTime)
@@ -1198,6 +1205,30 @@ func TestAPIs(t *testing.T) {
11981205
},
11991206
},
12001207

1208+
{
1209+
do: doWalReply(),
1210+
reqMethod: "GET",
1211+
reqPath: "/api/v1/status/walreplay",
1212+
inErr: fmt.Errorf("some error"),
1213+
err: fmt.Errorf("some error"),
1214+
},
1215+
1216+
{
1217+
do: doWalReply(),
1218+
reqMethod: "GET",
1219+
reqPath: "/api/v1/status/walreplay",
1220+
inRes: map[string]interface{}{
1221+
"min": 2,
1222+
"max": 5,
1223+
"current": 40,
1224+
},
1225+
res: WalReplayStatus{
1226+
Min: 2,
1227+
Max: 5,
1228+
Current: 40,
1229+
},
1230+
},
1231+
12011232
{
12021233
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
12031234
reqMethod: "GET",

0 commit comments

Comments
 (0)