Skip to content

Commit 6cd29bd

Browse files
authored
Add support for tsdb endpoint (#773)
Signed-off-by: Hima Varsha <[email protected]>
1 parent fe7bd95 commit 6cd29bd

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ const (
138138
epConfig = apiPrefix + "/status/config"
139139
epFlags = apiPrefix + "/status/flags"
140140
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
141+
epTSDB = apiPrefix + "/status/tsdb"
141142
)
142143

143144
// AlertState models the state of an alert.
@@ -254,6 +255,8 @@ type API interface {
254255
TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, error)
255256
// Metadata returns metadata about metrics currently scraped by the metric name.
256257
Metadata(ctx context.Context, metric string, limit string) (map[string][]Metadata, error)
258+
// TSDB returns the cardinality statistics.
259+
TSDB(ctx context.Context) (TSDBResult, error)
257260
}
258261

259262
// AlertsResult contains the result from querying the alerts endpoint.
@@ -404,6 +407,20 @@ type queryResult struct {
404407
v model.Value
405408
}
406409

410+
// TSDBResult contains the result from querying the tsdb endpoint.
411+
type TSDBResult struct {
412+
SeriesCountByMetricName []Stat `json:"seriesCountByMetricName"`
413+
LabelValueCountByLabelName []Stat `json:"labelValueCountByLabelName"`
414+
MemoryInBytesByLabelName []Stat `json:"memoryInBytesByLabelName"`
415+
SeriesCountByLabelValuePair []Stat `json:"seriesCountByLabelValuePair"`
416+
}
417+
418+
// Stat models information about statistic value.
419+
type Stat struct {
420+
Name string `json:"name"`
421+
Value uint64 `json:"value"`
422+
}
423+
407424
func (rg *RuleGroup) UnmarshalJSON(b []byte) error {
408425
v := struct {
409426
Name string `json:"name"`
@@ -883,6 +900,24 @@ func (h *httpAPI) Metadata(ctx context.Context, metric string, limit string) (ma
883900
return res, json.Unmarshal(body, &res)
884901
}
885902

903+
func (h *httpAPI) TSDB(ctx context.Context) (TSDBResult, error) {
904+
u := h.client.URL(epTSDB, nil)
905+
906+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
907+
if err != nil {
908+
return TSDBResult{}, err
909+
}
910+
911+
_, body, _, err := h.client.Do(ctx, req)
912+
if err != nil {
913+
return TSDBResult{}, err
914+
}
915+
916+
var res TSDBResult
917+
return res, json.Unmarshal(body, &res)
918+
919+
}
920+
886921
// Warnings is an array of non critical errors
887922
type Warnings []string
888923

api/prometheus/v1/api_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ func TestAPIs(t *testing.T) {
216216
}
217217
}
218218

219+
doTSDB := func() func() (interface{}, Warnings, error) {
220+
return func() (interface{}, Warnings, error) {
221+
v, err := promAPI.TSDB(context.Background())
222+
return v, nil, err
223+
}
224+
}
225+
219226
queryTests := []apiTest{
220227
{
221228
do: doQuery("2", testTime),
@@ -953,6 +960,72 @@ func TestAPIs(t *testing.T) {
953960
},
954961
err: fmt.Errorf("some error"),
955962
},
963+
964+
{
965+
do: doTSDB(),
966+
reqMethod: "GET",
967+
reqPath: "/api/v1/status/tsdb",
968+
inErr: fmt.Errorf("some error"),
969+
err: fmt.Errorf("some error"),
970+
},
971+
972+
{
973+
do: doTSDB(),
974+
reqMethod: "GET",
975+
reqPath: "/api/v1/status/tsdb",
976+
inRes: map[string]interface{}{
977+
"seriesCountByMetricName": []interface{}{
978+
map[string]interface{}{
979+
"name": "kubelet_http_requests_duration_seconds_bucket",
980+
"value": 1000,
981+
},
982+
},
983+
"labelValueCountByLabelName": []interface{}{
984+
map[string]interface{}{
985+
"name": "__name__",
986+
"value": 200,
987+
},
988+
},
989+
"memoryInBytesByLabelName": []interface{}{
990+
map[string]interface{}{
991+
"name": "id",
992+
"value": 4096,
993+
},
994+
},
995+
"seriesCountByLabelValuePair": []interface{}{
996+
map[string]interface{}{
997+
"name": "job=kubelet",
998+
"value": 30000,
999+
},
1000+
},
1001+
},
1002+
res: TSDBResult{
1003+
SeriesCountByMetricName: []Stat{
1004+
{
1005+
Name: "kubelet_http_requests_duration_seconds_bucket",
1006+
Value: 1000,
1007+
},
1008+
},
1009+
LabelValueCountByLabelName: []Stat{
1010+
{
1011+
Name: "__name__",
1012+
Value: 200,
1013+
},
1014+
},
1015+
MemoryInBytesByLabelName: []Stat{
1016+
{
1017+
Name: "id",
1018+
Value: 4096,
1019+
},
1020+
},
1021+
SeriesCountByLabelValuePair: []Stat{
1022+
{
1023+
Name: "job=kubelet",
1024+
Value: 30000,
1025+
},
1026+
},
1027+
},
1028+
},
9561029
}
9571030

9581031
var tests []apiTest

0 commit comments

Comments
 (0)