From a56731126dc02b79d7d351caa5ec1ddef275b3dd Mon Sep 17 00:00:00 2001 From: Filip Petkovski Date: Tue, 7 Jun 2022 17:13:21 +0200 Subject: [PATCH] Export TSDB status structs (#10783) We would like to implement the tsdb/status API in certain Thanos components. In order to match the Prometheus API and avoid duplicating code, this commit makes the structs used in the status API public. Signed-off-by: Filip Petkovski --- web/api/v1/api.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 2c50297f1..88bedd46a 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -1317,8 +1317,8 @@ func (api *API) serveFlags(_ *http.Request) apiFuncResult { return apiFuncResult{api.flagsMap, nil, nil, nil} } -// stat holds the information about individual cardinality. -type stat struct { +// TSDBStat holds the information about individual cardinality. +type TSDBStat struct { Name string `json:"name"` Value uint64 `json:"value"` } @@ -1332,26 +1332,27 @@ type HeadStats struct { MaxTime int64 `json:"maxTime"` } -// tsdbStatus has information of cardinality statistics from postings. -type tsdbStatus struct { - HeadStats HeadStats `json:"headStats"` - SeriesCountByMetricName []stat `json:"seriesCountByMetricName"` - LabelValueCountByLabelName []stat `json:"labelValueCountByLabelName"` - MemoryInBytesByLabelName []stat `json:"memoryInBytesByLabelName"` - SeriesCountByLabelValuePair []stat `json:"seriesCountByLabelValuePair"` +// TSDBStatus has information of cardinality statistics from postings. +type TSDBStatus struct { + HeadStats HeadStats `json:"headStats"` + SeriesCountByMetricName []TSDBStat `json:"seriesCountByMetricName"` + LabelValueCountByLabelName []TSDBStat `json:"labelValueCountByLabelName"` + MemoryInBytesByLabelName []TSDBStat `json:"memoryInBytesByLabelName"` + SeriesCountByLabelValuePair []TSDBStat `json:"seriesCountByLabelValuePair"` } -func convertStats(stats []index.Stat) []stat { - result := make([]stat, 0, len(stats)) +// TSDBStatsFromIndexStats converts a index.Stat slice to a TSDBStat slice. +func TSDBStatsFromIndexStats(stats []index.Stat) []TSDBStat { + result := make([]TSDBStat, 0, len(stats)) for _, item := range stats { - item := stat{Name: item.Name, Value: item.Count} + item := TSDBStat{Name: item.Name, Value: item.Count} result = append(result, item) } return result } func (api *API) serveTSDBStatus(*http.Request) apiFuncResult { - s, err := api.db.Stats("__name__") + s, err := api.db.Stats(labels.MetricName) if err != nil { return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil} } @@ -1369,7 +1370,7 @@ func (api *API) serveTSDBStatus(*http.Request) apiFuncResult { } } } - return apiFuncResult{tsdbStatus{ + return apiFuncResult{TSDBStatus{ HeadStats: HeadStats{ NumSeries: s.NumSeries, ChunkCount: chunkCount, @@ -1377,10 +1378,10 @@ func (api *API) serveTSDBStatus(*http.Request) apiFuncResult { MaxTime: s.MaxTime, NumLabelPairs: s.IndexPostingStats.NumLabelPairs, }, - SeriesCountByMetricName: convertStats(s.IndexPostingStats.CardinalityMetricsStats), - LabelValueCountByLabelName: convertStats(s.IndexPostingStats.CardinalityLabelStats), - MemoryInBytesByLabelName: convertStats(s.IndexPostingStats.LabelValueStats), - SeriesCountByLabelValuePair: convertStats(s.IndexPostingStats.LabelValuePairsStats), + SeriesCountByMetricName: TSDBStatsFromIndexStats(s.IndexPostingStats.CardinalityMetricsStats), + LabelValueCountByLabelName: TSDBStatsFromIndexStats(s.IndexPostingStats.CardinalityLabelStats), + MemoryInBytesByLabelName: TSDBStatsFromIndexStats(s.IndexPostingStats.LabelValueStats), + SeriesCountByLabelValuePair: TSDBStatsFromIndexStats(s.IndexPostingStats.LabelValuePairsStats), }, nil, nil, nil} }