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 <filip.petkovsky@gmail.com>
This commit is contained in:
Filip Petkovski 2022-06-07 17:13:21 +02:00 committed by GitHub
parent 2e2c014d52
commit a56731126d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {
// 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"`
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}
}