forked from influxdata/influxdb
/
converters.go
278 lines (240 loc) · 7.51 KB
/
converters.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
package prometheus
import (
"errors"
"fmt"
"math"
"time"
"github.com/gogo/protobuf/types"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/prometheus/remote"
"github.com/influxdata/influxdb/services/storage"
"github.com/influxdata/platform/storage/reads/datatypes"
)
const (
// measurementName is the default name used if no Prometheus name can be found on write
measurementName = "prom_metric_not_specified"
// fieldName is the field all prometheus values get written to
fieldName = "value"
// fieldTagKey is the tag key that all field names use in the new storage processor
fieldTagKey = "_field"
// prometheusNameTag is the tag key that Prometheus uses for metric names
prometheusNameTag = "__name__"
// measurementTagKey is the tag key that all measurement names use in the new storage processor
measurementTagKey = "_measurement"
)
var ErrNaNDropped = errors.New("dropped NaN from Prometheus since they are not supported")
// WriteRequestToPoints converts a Prometheus remote write request of time series and their
// samples into Points that can be written into Influx
func WriteRequestToPoints(req *remote.WriteRequest) ([]models.Point, error) {
var maxPoints int
for _, ts := range req.Timeseries {
maxPoints += len(ts.Samples)
}
points := make([]models.Point, 0, maxPoints)
var droppedNaN error
for _, ts := range req.Timeseries {
measurement := measurementName
tags := make(map[string]string, len(ts.Labels))
for _, l := range ts.Labels {
tags[l.Name] = l.Value
if l.Name == prometheusNameTag {
measurement = l.Value
}
}
for _, s := range ts.Samples {
// skip NaN values, which are valid in Prometheus
if math.IsNaN(s.Value) {
droppedNaN = ErrNaNDropped
continue
}
// convert and append
t := time.Unix(0, s.TimestampMs*int64(time.Millisecond))
fields := map[string]interface{}{fieldName: s.Value}
p, err := models.NewPoint(measurement, models.NewTags(tags), fields, t)
if err != nil {
return nil, err
}
points = append(points, p)
}
}
return points, droppedNaN
}
// ReadRequestToInfluxStorageRequest converts a Prometheus remote read request into one using the
// new storage API that IFQL uses.
func ReadRequestToInfluxStorageRequest(req *remote.ReadRequest, db, rp string) (*datatypes.ReadRequest, error) {
if len(req.Queries) != 1 {
return nil, errors.New("Prometheus read endpoint currently only supports one query at a time")
}
q := req.Queries[0]
src, err := types.MarshalAny(&storage.ReadSource{Database: db, RetentionPolicy: rp})
if err != nil {
return nil, err
}
sreq := &datatypes.ReadRequest{
ReadSource: src,
TimestampRange: datatypes.TimestampRange{
Start: time.Unix(0, q.StartTimestampMs*int64(time.Millisecond)).UnixNano(),
End: time.Unix(0, q.EndTimestampMs*int64(time.Millisecond)).UnixNano(),
},
PointsLimit: math.MaxInt64,
}
pred, err := predicateFromMatchers(q.Matchers)
if err != nil {
return nil, err
}
sreq.Predicate = pred
return sreq, nil
}
// RemoveInfluxSystemTags will remove tags that are Influx internal (_measurement and _field)
func RemoveInfluxSystemTags(tags models.Tags) models.Tags {
var t models.Tags
for _, tt := range tags {
if string(tt.Key) == measurementTagKey || string(tt.Key) == fieldTagKey {
continue
}
t = append(t, tt)
}
return t
}
// predicateFromMatchers takes Prometheus label matchers and converts them to a storage
// predicate that works with the schema that is written in, which assumes a single field
// named value
func predicateFromMatchers(matchers []*remote.LabelMatcher) (*datatypes.Predicate, error) {
left, err := nodeFromMatchers(matchers)
if err != nil {
return nil, err
}
right := fieldNode()
return &datatypes.Predicate{
Root: &datatypes.Node{
NodeType: datatypes.NodeTypeLogicalExpression,
Value: &datatypes.Node_Logical_{Logical: datatypes.LogicalAnd},
Children: []*datatypes.Node{left, right},
},
}, nil
}
// fieldNode returns a datatypes.Node that will match that the fieldTagKey == fieldName
// which matches how Prometheus data is fed into the system
func fieldNode() *datatypes.Node {
children := []*datatypes.Node{
&datatypes.Node{
NodeType: datatypes.NodeTypeTagRef,
Value: &datatypes.Node_TagRefValue{
TagRefValue: fieldTagKey,
},
},
&datatypes.Node{
NodeType: datatypes.NodeTypeLiteral,
Value: &datatypes.Node_StringValue{
StringValue: fieldName,
},
},
}
return &datatypes.Node{
NodeType: datatypes.NodeTypeComparisonExpression,
Value: &datatypes.Node_Comparison_{Comparison: datatypes.ComparisonEqual},
Children: children,
}
}
func nodeFromMatchers(matchers []*remote.LabelMatcher) (*datatypes.Node, error) {
if len(matchers) == 0 {
return nil, errors.New("expected matcher")
} else if len(matchers) == 1 {
return nodeFromMatcher(matchers[0])
}
left, err := nodeFromMatcher(matchers[0])
if err != nil {
return nil, err
}
right, err := nodeFromMatchers(matchers[1:])
if err != nil {
return nil, err
}
children := []*datatypes.Node{left, right}
return &datatypes.Node{
NodeType: datatypes.NodeTypeLogicalExpression,
Value: &datatypes.Node_Logical_{Logical: datatypes.LogicalAnd},
Children: children,
}, nil
}
func nodeFromMatcher(m *remote.LabelMatcher) (*datatypes.Node, error) {
var op datatypes.Node_Comparison
switch m.Type {
case remote.MatchType_EQUAL:
op = datatypes.ComparisonEqual
case remote.MatchType_NOT_EQUAL:
op = datatypes.ComparisonNotEqual
case remote.MatchType_REGEX_MATCH:
op = datatypes.ComparisonRegex
case remote.MatchType_REGEX_NO_MATCH:
op = datatypes.ComparisonNotRegex
default:
return nil, fmt.Errorf("unknown match type %v", m.Type)
}
name := m.Name
if m.Name == prometheusNameTag {
name = measurementTagKey
}
left := &datatypes.Node{
NodeType: datatypes.NodeTypeTagRef,
Value: &datatypes.Node_TagRefValue{
TagRefValue: name,
},
}
var right *datatypes.Node
if op == datatypes.ComparisonRegex || op == datatypes.ComparisonNotRegex {
right = &datatypes.Node{
NodeType: datatypes.NodeTypeLiteral,
Value: &datatypes.Node_RegexValue{
RegexValue: m.Value,
},
}
} else {
right = &datatypes.Node{
NodeType: datatypes.NodeTypeLiteral,
Value: &datatypes.Node_StringValue{
StringValue: m.Value,
},
}
}
children := []*datatypes.Node{left, right}
return &datatypes.Node{
NodeType: datatypes.NodeTypeComparisonExpression,
Value: &datatypes.Node_Comparison_{Comparison: op},
Children: children,
}, nil
}
// ModelTagsToLabelPairs converts models.Tags to a slice of Prometheus label pairs
func ModelTagsToLabelPairs(tags models.Tags) []*remote.LabelPair {
pairs := make([]*remote.LabelPair, 0, len(tags))
for _, t := range tags {
if string(t.Value) == "" {
continue
}
pairs = append(pairs, &remote.LabelPair{
Name: string(t.Key),
Value: string(t.Value),
})
}
return pairs
}
// TagsToLabelPairs converts a map of Influx tags into a slice of Prometheus label pairs
func TagsToLabelPairs(tags map[string]string) []*remote.LabelPair {
pairs := make([]*remote.LabelPair, 0, len(tags))
for k, v := range tags {
if v == "" {
// If we select metrics with different sets of labels names,
// InfluxDB returns *all* possible tag names on all returned
// series, with empty tag values on series where they don't
// apply. In Prometheus, an empty label value is equivalent
// to a non-existent label, so we just skip empty ones here
// to make the result correct.
continue
}
pairs = append(pairs, &remote.LabelPair{
Name: k,
Value: v,
})
}
return pairs
}