forked from v3io/v3io-tsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promtsdb.go
199 lines (163 loc) · 5.23 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
package promtsdb
import (
"context"
"fmt"
"github.com/nuclio/logger"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/v3io/v3io-go-http"
"github.com/v3io/v3io-tsdb/config"
"github.com/v3io/v3io-tsdb/pkg/appender"
"github.com/v3io/v3io-tsdb/pkg/querier"
"github.com/v3io/v3io-tsdb/pkg/tsdb"
"strings"
)
type V3ioPromAdapter struct {
db *tsdb.V3ioAdapter
}
func NewV3ioProm(cfg *config.V3ioConfig, container *v3io.Container, logger logger.Logger) (*V3ioPromAdapter, error) {
adapter, err := tsdb.NewV3ioAdapter(cfg, container, logger)
newAdapter := V3ioPromAdapter{db: adapter}
return &newAdapter, err
}
func (a *V3ioPromAdapter) Appender() (storage.Appender, error) {
err := a.db.MetricsCache.StartIfNeeded()
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.Querier(nil, mint, maxt)
querier := V3ioPromQuerier{q: v3ioQuerier}
return &querier, err
}
type V3ioPromQuerier struct {
q *querier.V3ioQuerier
}
// Select returns a set of series that matches the given label matchers.
func (q *V3ioPromQuerier) Select(params *storage.SelectParams, oms ...*labels.Matcher) (storage.SeriesSet, error) {
name, filter, functions := match2filter(oms)
if params.Func != "" {
functions = params.Func
}
set, err := q.q.Select(name, functions, params.Step, filter)
return &V3ioPromSeriesSet{s: set}, err
}
// LabelValues returns all potential values for a label name.
func (q *V3ioPromQuerier) LabelValues(name string) ([]string, error) {
return q.q.LabelValues(name)
}
// Close releases the resources of the Querier.
func (q *V3ioPromQuerier) Close() error {
return nil
}
func match2filter(oms []*labels.Matcher) (string, string, string) {
filter := []string{}
aggregator := ""
name := ""
for _, matcher := range oms {
if matcher.Name == "Aggregator" {
aggregator = matcher.Value
} else if matcher.Name == "Aggregator" && 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))
}
}
}
return name, strings.Join(filter, " and "), aggregator
}
type V3ioPromSeriesSet struct {
s querier.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 querier.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 querier.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() }
// Err 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 (l Labels) GetKey() (string, string, uint64) {
key := ""
name := ""
for _, lbl := range *l.lbls {
if lbl.Name == "__name__" {
name = lbl.Value
} else {
key = key + lbl.Name + "=" + lbl.Value + ","
}
}
if len(key) == 0 {
return name, "", l.lbls.Hash()
}
return name, key[:len(key)-1], l.lbls.Hash()
}
// create update expression
func (l Labels) GetExpr() string {
lblexpr := ""
for _, lbl := range *l.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
}