forked from v3io/v3io-tsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promtsdb.go
300 lines (249 loc) · 8.22 KB
/
promtsdb.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package promtsdb
import (
"context"
"fmt"
"strings"
"github.com/nuclio/logger"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/v3io/v3io-go-http"
"github.com/v3io/v3io-tsdb/pkg/aggregate"
"github.com/v3io/v3io-tsdb/pkg/appender"
"github.com/v3io/v3io-tsdb/pkg/config"
"github.com/v3io/v3io-tsdb/pkg/pquerier"
"github.com/v3io/v3io-tsdb/pkg/tsdb"
"github.com/v3io/v3io-tsdb/pkg/utils"
)
type V3ioPromAdapter struct {
db *tsdb.V3ioAdapter
logger logger.Logger
}
func NewV3ioProm(cfg *config.V3ioConfig, container *v3io.Container, logger logger.Logger) (*V3ioPromAdapter, error) {
if logger == nil {
newLogger, err := utils.NewLogger(cfg.LogLevel)
if err != nil {
return nil, errors.Wrap(err, "Unable to initialize logger.")
}
logger = newLogger
}
adapter, err := tsdb.NewV3ioAdapter(cfg, container, logger)
newAdapter := V3ioPromAdapter{db: adapter, logger: logger.GetChild("v3io-prom-adapter")}
return &newAdapter, err
}
func (a *V3ioPromAdapter) Appender() (storage.Appender, error) {
err := a.db.InitAppenderCache()
if err != nil {
return nil, err
}
newAppender := v3ioAppender{metricsCache: a.db.MetricsCache}
return newAppender, nil
}
func (a *V3ioPromAdapter) StartTime() (int64, error) {
return a.db.StartTime()
}
func (a *V3ioPromAdapter) Close() error {
return nil
}
func (a *V3ioPromAdapter) Querier(_ context.Context, mint, maxt int64) (storage.Querier, error) {
v3ioQuerier, err := a.db.QuerierV2()
promQuerier := V3ioPromQuerier{v3ioQuerier: v3ioQuerier, logger: a.logger.GetChild("v3io-prom-query"), mint: mint, maxt: maxt}
return &promQuerier, err
}
type V3ioPromQuerier struct {
v3ioQuerier *pquerier.V3ioQuerier
logger logger.Logger
mint, maxt int64
}
// Select returns a set of series that matches the given label matchers.
func (promQuery *V3ioPromQuerier) Select(params *storage.SelectParams, oms ...*labels.Matcher) (storage.SeriesSet, error) {
name, filter, functions := match2filter(oms, promQuery.logger)
noAggr := false
// if a nil params is passed we assume it's a metadata query, so we fetch only the different labelsets withtout data.
if params == nil {
labelSets, err := promQuery.v3ioQuerier.GetLabelSets(name, filter)
if err != nil {
return nil, err
}
return &V3ioPromSeriesSet{newMetadataSeriesSet(labelSets)}, nil
}
promQuery.logger.Debug("SelectParams: %+v", params)
if params.Func != "" {
// only pass xx_over_time functions (just the xx part)
// TODO: support count/stdxx, require changes in Prometheus: promql/functions.go, not calc aggregate twice
if strings.HasSuffix(params.Func, "_over_time") {
f := params.Func[0:3]
if params.Step == 0 && (f == "min" || f == "max" || f == "sum" || f == "avg") {
functions = f
} else {
noAggr = true
}
}
}
selectParams := &pquerier.SelectParams{Name: name,
Functions: functions,
Step: params.Step,
Filter: filter,
From: promQuery.mint,
To: promQuery.maxt}
set, err := promQuery.v3ioQuerier.SelectProm(selectParams, noAggr)
return &V3ioPromSeriesSet{s: set}, err
}
// LabelValues returns all potential values for a label name.
func (promQuery *V3ioPromQuerier) LabelValues(name string) ([]string, error) {
return promQuery.v3ioQuerier.LabelValues(name)
}
// Close releases the resources of the Querier.
func (promQuery *V3ioPromQuerier) Close() error {
return nil
}
func match2filter(oms []*labels.Matcher, logger logger.Logger) (string, string, string) {
var filter []string
agg := ""
name := ""
for _, matcher := range oms {
logger.Debug("Matcher: %+v", matcher)
if matcher.Name == aggregate.AggregateLabel {
agg = matcher.Value
} else if matcher.Name == "__name__" && matcher.Type == labels.MatchEqual {
name = matcher.Value
} else {
switch matcher.Type {
case labels.MatchEqual:
filter = append(filter, fmt.Sprintf("%s=='%s'", matcher.Name, matcher.Value))
case labels.MatchNotEqual:
filter = append(filter, fmt.Sprintf("%s!='%s'", matcher.Name, matcher.Value))
case labels.MatchRegexp:
filter = append(filter, fmt.Sprintf("regexp_instr(%s,'%s') == 0", matcher.Name, matcher.Value))
case labels.MatchNotRegexp:
filter = append(filter, fmt.Sprintf("regexp_instr(%s,'%s') != 0", matcher.Name, matcher.Value))
}
}
}
filterExp := strings.Join(filter, " and ")
return name, filterExp, agg
}
type V3ioPromSeriesSet struct {
s utils.SeriesSet
}
func (s *V3ioPromSeriesSet) Next() bool { return s.s.Next() }
func (s *V3ioPromSeriesSet) Err() error { return s.s.Err() }
func (s *V3ioPromSeriesSet) At() storage.Series {
series := s.s.At()
return &V3ioPromSeries{series}
}
// Series represents a single time series.
type V3ioPromSeries struct {
s utils.Series
}
// Labels returns the complete set of labels identifying the series.
func (s *V3ioPromSeries) Labels() labels.Labels {
lbls := labels.Labels{}
for _, l := range s.s.Labels() {
lbls = append(lbls, labels.Label{Name: l.Name, Value: l.Value})
}
return lbls
}
// Iterator returns a new iterator of the data of the series.
func (s *V3ioPromSeries) Iterator() storage.SeriesIterator {
return &V3ioPromSeriesIterator{s: s.s.Iterator()}
}
// SeriesIterator iterates over the data of a time series.
type V3ioPromSeriesIterator struct {
s utils.SeriesIterator
}
// Seek advances the iterator forward to the given timestamp.
// If there's no value exactly at t, it advances to the first value
// after t.
func (s *V3ioPromSeriesIterator) Seek(t int64) bool { return s.s.Seek(t) }
// Next advances the iterator by one.
func (s *V3ioPromSeriesIterator) Next() bool { return s.s.Next() }
// At returns the current timestamp/value pair.
func (s *V3ioPromSeriesIterator) At() (t int64, v float64) { return s.s.At() }
// error returns the current error.
func (s *V3ioPromSeriesIterator) Err() error { return s.s.Err() }
type v3ioAppender struct {
metricsCache *appender.MetricsCache
}
func (a v3ioAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
lbls := Labels{lbls: &lset}
return a.metricsCache.Add(lbls, t, v)
}
func (a v3ioAppender) AddFast(lset labels.Labels, ref uint64, t int64, v float64) error {
return a.metricsCache.AddFast(ref, t, v)
}
func (a v3ioAppender) Commit() error { return nil }
func (a v3ioAppender) Rollback() error { return nil }
type Labels struct {
lbls *labels.Labels
}
// convert Label set to a string in the form key1=v1,key2=v2.. + name + hash
func (ls Labels) GetKey() (string, string, uint64) {
key := ""
name := ""
for _, lbl := range *ls.lbls {
if lbl.Name == "__name__" {
name = lbl.Value
} else {
key = key + lbl.Name + "=" + lbl.Value + ","
}
}
if len(key) == 0 {
return name, "", ls.lbls.Hash()
}
return name, key[:len(key)-1], ls.lbls.Hash()
}
// create update expression
func (ls Labels) GetExpr() string {
lblexpr := ""
for _, lbl := range *ls.lbls {
if lbl.Name != "__name__" {
lblexpr = lblexpr + fmt.Sprintf("%s='%s'; ", lbl.Name, lbl.Value)
} else {
lblexpr = lblexpr + fmt.Sprintf("_name='%s'; ", lbl.Value)
}
}
return lblexpr
}
func (ls Labels) LabelNames() []string {
var res []string
for _, l := range *ls.lbls {
res = append(res, l.Name)
}
return res
}
func newMetadataSeriesSet(labels []utils.Labels) utils.SeriesSet {
return &metadataSeriesSet{labels: labels, currentIndex: -1, size: len(labels)}
}
type metadataSeriesSet struct {
labels []utils.Labels
currentIndex int
size int
}
func (ss *metadataSeriesSet) Next() bool {
ss.currentIndex++
return ss.currentIndex < ss.size
}
func (ss *metadataSeriesSet) At() utils.Series {
return &metadataSeries{labels: ss.labels[ss.currentIndex]}
}
func (ss *metadataSeriesSet) Err() error {
return nil
}
type metadataSeries struct {
labels utils.Labels
}
func (s *metadataSeries) Labels() utils.Labels { return s.labels }
func (s *metadataSeries) Iterator() utils.SeriesIterator { return utils.NullSeriesIterator{} }
func (s *metadataSeries) GetKey() uint64 { return s.labels.Hash() }
func (ls Labels) Filter(keep []string) utils.LabelsIfc {
var res labels.Labels
for _, l := range *ls.lbls {
for _, keepLabel := range keep {
if l.Name == labels.MetricName || l.Name == keepLabel {
res = append(res, l)
}
}
}
return Labels{lbls: &res}
}