-
Notifications
You must be signed in to change notification settings - Fork 451
/
converter.go
396 lines (346 loc) · 11.4 KB
/
converter.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package storage
import (
"bytes"
"fmt"
"sort"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/query/cost"
"github.com/m3db/m3/src/query/generated/proto/prompb"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/ts"
xcost "github.com/m3db/m3/src/x/cost"
xsync "github.com/m3db/m3/src/x/sync"
xtime "github.com/m3db/m3/src/x/time"
)
const (
xTimeUnit = xtime.Millisecond
initRawFetchAllocSize = 32
)
// PromWriteTSToM3 converts a prometheus write query to an M3 one
func PromWriteTSToM3(
timeseries *prompb.TimeSeries,
opts models.TagOptions,
) *WriteQuery {
tags := PromLabelsToM3Tags(timeseries.Labels, opts)
datapoints := PromSamplesToM3Datapoints(timeseries.Samples)
return &WriteQuery{
Tags: tags,
Datapoints: datapoints,
Unit: xTimeUnit,
Annotation: nil,
}
}
// The default name for the name and bucket tags in Prometheus metrics.
// This can be overwritten by setting tagOptions in the config.
var (
promDefaultName = []byte("__name__")
promDefaultBucketName = []byte("le")
)
// PromLabelsToM3Tags converts Prometheus labels to M3 tags
func PromLabelsToM3Tags(
labels []*prompb.Label,
tagOptions models.TagOptions,
) models.Tags {
tags := models.NewTags(len(labels), tagOptions)
tagList := make([]models.Tag, 0, len(labels))
for _, label := range labels {
name := label.Name
// If this label corresponds to the Prometheus name or bucket name,
// instead set it as the given name tag from the config file.
if bytes.Equal(promDefaultName, name) {
tags = tags.SetName(label.Value)
} else if bytes.Equal(promDefaultBucketName, name) {
tags = tags.SetBucket(label.Value)
} else {
tagList = append(tagList, models.Tag{
Name: name,
Value: label.Value,
})
}
}
return tags.AddTags(tagList)
}
// PromSamplesToM3Datapoints converts Prometheus samples to M3 datapoints
func PromSamplesToM3Datapoints(samples []*prompb.Sample) ts.Datapoints {
datapoints := make(ts.Datapoints, 0, len(samples))
for _, sample := range samples {
timestamp := PromTimestampToTime(sample.Timestamp)
datapoints = append(datapoints, ts.Datapoint{Timestamp: timestamp, Value: sample.Value})
}
return datapoints
}
// PromReadQueryToM3 converts a prometheus read query to m3 read query
func PromReadQueryToM3(query *prompb.Query) (*FetchQuery, error) {
tagMatchers, err := PromMatchersToM3(query.Matchers)
if err != nil {
return nil, err
}
return &FetchQuery{
TagMatchers: tagMatchers,
Start: PromTimestampToTime(query.StartTimestampMs),
End: PromTimestampToTime(query.EndTimestampMs),
}, nil
}
// PromMatchersToM3 converts prometheus label matchers to m3 matchers
func PromMatchersToM3(matchers []*prompb.LabelMatcher) (models.Matchers, error) {
tagMatchers := make(models.Matchers, len(matchers))
var err error
for idx, matcher := range matchers {
tagMatchers[idx], err = PromMatcherToM3(matcher)
if err != nil {
return nil, err
}
}
return tagMatchers, nil
}
// PromMatcherToM3 converts a prometheus label matcher to m3 matcher
func PromMatcherToM3(matcher *prompb.LabelMatcher) (models.Matcher, error) {
matchType, err := PromTypeToM3(matcher.Type)
if err != nil {
return models.Matcher{}, err
}
return models.NewMatcher(matchType, matcher.Name, matcher.Value)
}
// PromTypeToM3 converts a prometheus label type to m3 matcher type
func PromTypeToM3(labelType prompb.LabelMatcher_Type) (models.MatchType, error) {
switch labelType {
case prompb.LabelMatcher_EQ:
return models.MatchEqual, nil
case prompb.LabelMatcher_NEQ:
return models.MatchNotEqual, nil
case prompb.LabelMatcher_RE:
return models.MatchRegexp, nil
case prompb.LabelMatcher_NRE:
return models.MatchNotRegexp, nil
default:
return 0, fmt.Errorf("unknown match type: %v", labelType)
}
}
// PromTimestampToTime converts a prometheus timestamp to time.Time.
func PromTimestampToTime(timestampMS int64) time.Time {
return time.Unix(0, timestampMS*int64(time.Millisecond))
}
// TimeToPromTimestamp converts a time.Time to prometheus timestamp.
func TimeToPromTimestamp(timestamp time.Time) int64 {
// Significantly faster than time.Truncate()
return timestamp.UnixNano() / int64(time.Millisecond)
}
// FetchResultToPromResult converts fetch results from M3 to Prometheus result.
// TODO(rartoul): We should pool all of these intermediary datastructures, or
// at least the []*prompb.Sample (as thats the most heavily allocated object)
// since we have full control over the lifecycle.
func FetchResultToPromResult(
result *FetchResult,
keepEmpty bool,
) *prompb.QueryResult {
// Perform bulk allocation upfront then convert to pointers afterwards
// to reduce total number of allocations. See BenchmarkFetchResultToPromResult
// if modifying.
timeseries := make([]prompb.TimeSeries, 0, len(result.SeriesList))
for _, series := range result.SeriesList {
if !keepEmpty && series.Len() == 0 {
continue
}
promTs := SeriesToPromTS(series)
timeseries = append(timeseries, promTs)
}
timeSeriesPointers := make([]*prompb.TimeSeries, 0, len(result.SeriesList))
for i := range timeseries {
timeSeriesPointers = append(timeSeriesPointers, ×eries[i])
}
return &prompb.QueryResult{
Timeseries: timeSeriesPointers,
}
}
// SeriesToPromTS converts a series to prometheus timeseries.
func SeriesToPromTS(series *ts.Series) prompb.TimeSeries {
labels := TagsToPromLabels(series.Tags)
samples := SeriesToPromSamples(series)
return prompb.TimeSeries{Labels: labels, Samples: samples}
}
type sortableLabels []prompb.Label
func (t sortableLabels) Len() int { return len(t) }
func (t sortableLabels) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t sortableLabels) Less(i, j int) bool {
return bytes.Compare(t[i].Name, t[j].Name) == -1
}
// TagsToPromLabels converts tags to prometheus labels.
func TagsToPromLabels(tags models.Tags) []*prompb.Label {
// Perform bulk allocation upfront then convert to pointers afterwards
// to reduce total number of allocations. See BenchmarkFetchResultToPromResult
// if modifying.
l := tags.Len()
labels := make([]prompb.Label, 0, l)
metricName := tags.Opts.MetricName()
bucketName := tags.Opts.BucketName()
for _, t := range tags.Tags {
if bytes.Equal(t.Name, metricName) {
labels = append(labels,
prompb.Label{Name: promDefaultName, Value: t.Value})
} else if bytes.Equal(t.Name, bucketName) {
labels = append(labels,
prompb.Label{Name: promDefaultBucketName, Value: t.Value})
} else {
labels = append(labels, prompb.Label{Name: t.Name, Value: t.Value})
}
}
// Sort here since name and label may be added in a different order in tags
// if default metric name or bucket names are overridden.
sort.Sort(sortableLabels(labels))
labelsPointers := make([]*prompb.Label, 0, l)
for i := range labels {
labelsPointers = append(labelsPointers, &labels[i])
}
return labelsPointers
}
// SeriesToPromSamples series datapoints to prometheus samples.SeriesToPromSamples.
func SeriesToPromSamples(series *ts.Series) []*prompb.Sample {
var (
seriesLen = series.Len()
values = series.Values()
datapoints = values.Datapoints()
// Perform bulk allocation upfront then convert to pointers afterwards
// to reduce total number of allocations. See BenchmarkFetchResultToPromResult
// if modifying.
samples = make([]prompb.Sample, 0, seriesLen)
)
for _, dp := range datapoints {
samples = append(samples, prompb.Sample{
Timestamp: TimeToPromTimestamp(dp.Timestamp),
Value: dp.Value,
})
}
samplesPointers := make([]*prompb.Sample, 0, len(samples))
for i := range samples {
samplesPointers = append(samplesPointers, &samples[i])
}
return samplesPointers
}
func iteratorToTsSeries(
iter encoding.SeriesIterator,
enforcer cost.ChainedEnforcer,
tagOptions models.TagOptions,
) (*ts.Series, error) {
metric, err := FromM3IdentToMetric(iter.ID(), iter.Tags(), tagOptions)
if err != nil {
return nil, err
}
datapoints := make(ts.Datapoints, 0, initRawFetchAllocSize)
for iter.Next() {
dp, _, _ := iter.Current()
datapoints = append(datapoints, ts.Datapoint{Timestamp: dp.Timestamp, Value: dp.Value})
}
if err := iter.Err(); err != nil {
return nil, err
}
r := enforcer.Add(xcost.Cost(len(datapoints)))
if r.Error != nil {
return nil, r.Error
}
return ts.NewSeries(metric.ID, datapoints, metric.Tags), nil
}
// Fall back to sequential decompression if unable to decompress concurrently
func decompressSequentially(
iters []encoding.SeriesIterator,
enforcer cost.ChainedEnforcer,
tagOptions models.TagOptions,
) (*FetchResult, error) {
seriesList := make([]*ts.Series, 0, len(iters))
for _, iter := range iters {
series, err := iteratorToTsSeries(iter, enforcer, tagOptions)
if err != nil {
return nil, err
}
seriesList = append(seriesList, series)
}
return &FetchResult{
SeriesList: seriesList,
}, nil
}
func decompressConcurrently(
iters []encoding.SeriesIterator,
readWorkerPool xsync.PooledWorkerPool,
enforcer cost.ChainedEnforcer,
tagOptions models.TagOptions,
) (*FetchResult, error) {
seriesList := make([]*ts.Series, len(iters))
errorCh := make(chan error, 1)
done := make(chan struct{})
stopped := func() bool {
select {
case <-done:
return true
default:
return false
}
}
var wg sync.WaitGroup
for i, iter := range iters {
i, iter := i, iter
wg.Add(1)
readWorkerPool.Go(func() {
defer wg.Done()
if stopped() {
return
}
series, err := iteratorToTsSeries(iter, enforcer, tagOptions)
if err != nil {
// Return the first error that is encountered.
select {
case errorCh <- err:
close(done)
default:
}
return
}
seriesList[i] = series
})
}
wg.Wait()
close(errorCh)
if err := <-errorCh; err != nil {
return nil, err
}
return &FetchResult{
SeriesList: seriesList,
}, nil
}
// SeriesIteratorsToFetchResult converts SeriesIterators into a fetch result
func SeriesIteratorsToFetchResult(
seriesIterators encoding.SeriesIterators,
readWorkerPool xsync.PooledWorkerPool,
cleanupSeriesIters bool,
enforcer cost.ChainedEnforcer,
tagOptions models.TagOptions,
) (*FetchResult, error) {
if cleanupSeriesIters {
defer seriesIterators.Close()
}
iters := seriesIterators.Iters()
if readWorkerPool == nil {
return decompressSequentially(iters, enforcer, tagOptions)
}
return decompressConcurrently(iters, readWorkerPool,
enforcer, tagOptions)
}