-
Notifications
You must be signed in to change notification settings - Fork 2k
/
v1.go
143 lines (123 loc) · 4.37 KB
/
v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package status
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/tsdb"
"github.com/go-kit/log"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/tsdb/index"
v1 "github.com/prometheus/prometheus/web/api/v1"
"github.com/thanos-io/thanos/pkg/api"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
"github.com/thanos-io/thanos/pkg/logging"
)
// Stat holds the information about individual cardinality.
type Stat struct {
Name string `json:"name"`
Value uint64 `json:"value"`
}
func convertStats(stats []index.Stat) []Stat {
result := make([]Stat, 0, len(stats))
for _, item := range stats {
item := Stat{Name: item.Name, Value: item.Count}
result = append(result, item)
}
return result
}
type TenantStats struct {
Tenant string
Stats *tsdb.Stats
}
// TSDBStatus has information of cardinality statistics from postings.
// TODO(fpetkovski): replace with upstream struct after dependency update.
type TSDBStatus struct {
Tenant string `json:"tenant"`
HeadStats v1.HeadStats `json:"headStats"`
SeriesCountByMetricName []Stat `json:"seriesCountByMetricName"`
LabelValueCountByLabelName []Stat `json:"labelValueCountByLabelName"`
MemoryInBytesByLabelName []Stat `json:"memoryInBytesByLabelName"`
SeriesCountByLabelValuePair []Stat `json:"seriesCountByLabelValuePair"`
}
type GetStatsFunc func(r *http.Request, statsByLabelName string) ([]TenantStats, *api.ApiError)
type Options struct {
GetStats GetStatsFunc
Registry *prometheus.Registry
}
type StatusAPI struct {
getTSDBStats GetStatsFunc
registry *prometheus.Registry
}
func New(opts Options) *StatusAPI {
return &StatusAPI{
getTSDBStats: opts.GetStats,
registry: opts.Registry,
}
}
func (sapi *StatusAPI) Register(r *route.Router, tracer opentracing.Tracer, logger log.Logger, ins extpromhttp.InstrumentationMiddleware, logMiddleware *logging.HTTPServerMiddleware) {
instr := api.GetInstr(tracer, logger, ins, logMiddleware, false)
r.Get("/api/v1/status/tsdb", instr("tsdb_status", sapi.httpServeStats))
}
func (sapi *StatusAPI) httpServeStats(r *http.Request) (interface{}, []error, *api.ApiError) {
stats, sterr := sapi.getTSDBStats(r, labels.MetricName)
if sterr != nil {
return nil, nil, sterr
}
result := make([]TSDBStatus, 0, len(stats))
if len(stats) == 0 {
return result, nil, nil
}
metrics, err := sapi.registry.Gather()
if err != nil {
return nil, []error{err}, nil
}
tenantChunks := make(map[string]int64)
for _, mF := range metrics {
if *mF.Name != "prometheus_tsdb_head_chunks" {
continue
}
for _, metric := range mF.Metric {
for _, lbl := range metric.Label {
if *lbl.Name == "tenant" {
tenantChunks[*lbl.Value] = int64(metric.Gauge.GetValue())
}
}
}
}
for _, s := range stats {
var chunkCount int64
if c, ok := tenantChunks[s.Tenant]; ok {
chunkCount = c
}
result = append(result, TSDBStatus{
Tenant: s.Tenant,
HeadStats: v1.HeadStats{
NumSeries: s.Stats.NumSeries,
ChunkCount: chunkCount,
MinTime: s.Stats.MinTime,
MaxTime: s.Stats.MaxTime,
NumLabelPairs: s.Stats.IndexPostingStats.NumLabelPairs,
},
SeriesCountByMetricName: convertStats(s.Stats.IndexPostingStats.CardinalityMetricsStats),
LabelValueCountByLabelName: convertStats(s.Stats.IndexPostingStats.CardinalityLabelStats),
MemoryInBytesByLabelName: convertStats(s.Stats.IndexPostingStats.LabelValueStats),
SeriesCountByLabelValuePair: convertStats(s.Stats.IndexPostingStats.LabelValuePairsStats),
})
}
return result, nil, nil
}