forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus_formatter.go
419 lines (363 loc) · 11.5 KB
/
prometheus_formatter.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2020, OpenTelemetry 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 sumologicexporter
import (
"fmt"
"regexp"
"strings"
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
type dataPoint interface {
Timestamp() pcommon.Timestamp
Attributes() pcommon.Map
}
type prometheusFormatter struct {
sanitNameRegex *regexp.Regexp
replacer *strings.Replacer
}
type prometheusTags string
const (
prometheusLeTag string = "le"
prometheusQuantileTag string = "quantile"
prometheusInfValue string = "+Inf"
)
func newPrometheusFormatter() (prometheusFormatter, error) {
sanitNameRegex, err := regexp.Compile(`[^0-9a-zA-Z\./_:\-]`)
if err != nil {
return prometheusFormatter{}, err
}
return prometheusFormatter{
sanitNameRegex: sanitNameRegex,
// `\`, `"` and `\n` should be escaped, everything else should be left as-is
// see: https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#line-format
replacer: strings.NewReplacer(`\`, `\\`, `"`, `\"`, "\n", `\n`),
}, nil
}
// PrometheusLabels returns all attributes as sanitized prometheus labels string
func (f *prometheusFormatter) tags2String(attr pcommon.Map, labels pcommon.Map) prometheusTags {
attrsPlusLabelsLen := attr.Len() + labels.Len()
if attrsPlusLabelsLen == 0 {
return ""
}
mergedAttributes := pcommon.NewMap()
mergedAttributes.EnsureCapacity(attrsPlusLabelsLen)
attr.CopyTo(mergedAttributes)
labels.Range(func(k string, v pcommon.Value) bool {
mergedAttributes.PutStr(k, v.Str())
return true
})
length := mergedAttributes.Len()
returnValue := make([]string, 0, length)
mergedAttributes.Range(func(k string, v pcommon.Value) bool {
key := f.sanitizeKeyBytes([]byte(k))
value := f.sanitizeValue(v.AsString())
returnValue = append(
returnValue,
formatKeyValuePair(key, value),
)
return true
})
return prometheusTags(stringsJoinAndSurround(returnValue, ",", "{", "}"))
}
func formatKeyValuePair(key []byte, value string) string {
const (
quoteSign = `"`
equalSign = `=`
)
// Use strings.Builder and not fmt.Sprintf as it uses significantly less
// allocations.
sb := strings.Builder{}
// We preallocate space for key, value, equal sign and quotes.
sb.Grow(len(key) + len(equalSign) + 2*len(quoteSign) + len(value))
sb.Write(key)
sb.WriteString(equalSign)
sb.WriteString(quoteSign)
sb.WriteString(value)
sb.WriteString(quoteSign)
return sb.String()
}
// stringsJoinAndSurround joins the strings in s slice using the separator adds front
// to the front of the resulting string and back at the end.
//
// This has a benefit over using the strings.Join() of using just one strings.Buidler
// instance and hence using less allocations to produce the final string.
func stringsJoinAndSurround(s []string, separator, front, back string) string {
switch len(s) {
case 0:
return ""
case 1:
var b strings.Builder
b.Grow(len(s[0]) + len(front) + len(back))
b.WriteString(front)
b.WriteString(s[0])
b.WriteString(back)
return b.String()
}
// Count the total strings summarized length for the preallocation.
n := len(front) + len(s[0])
for i := 1; i < len(s); i++ {
n += len(separator) + len(s[i])
}
n += len(back)
var b strings.Builder
// We preallocate space for all the entires in the provided slice together with
// the separator as well as the surrounding characters.
b.Grow(n)
b.WriteString(front)
b.WriteString(s[0])
for _, s := range s[1:] {
b.WriteString(separator)
b.WriteString(s)
}
b.WriteString(back)
return b.String()
}
// sanitizeKeyBytes returns sanitized key byte slice by replacing
// all non-allowed chars with `_`
func (f *prometheusFormatter) sanitizeKeyBytes(s []byte) []byte {
return f.sanitNameRegex.ReplaceAll(s, []byte{'_'})
}
// sanitizeKey returns sanitized value string performing the following substitutions:
// `/` -> `//`
// `"` -> `\"`
// "\n" -> `\n`
func (f *prometheusFormatter) sanitizeValue(s string) string {
return f.replacer.Replace(s)
}
// doubleLine builds metric based on the given arguments where value is float64
func (f *prometheusFormatter) doubleLine(name string, attributes prometheusTags, value float64, timestamp pcommon.Timestamp) string {
return fmt.Sprintf(
"%s%s %g %d",
f.sanitizeKeyBytes([]byte(name)),
attributes,
value,
timestamp/pcommon.Timestamp(time.Millisecond),
)
}
// intLine builds metric based on the given arguments where value is int64
func (f *prometheusFormatter) intLine(name string, attributes prometheusTags, value int64, timestamp pcommon.Timestamp) string {
return fmt.Sprintf(
"%s%s %d %d",
f.sanitizeKeyBytes([]byte(name)),
attributes,
value,
timestamp/pcommon.Timestamp(time.Millisecond),
)
}
// uintLine builds metric based on the given arguments where value is uint64
func (f *prometheusFormatter) uintLine(name string, attributes prometheusTags, value uint64, timestamp pcommon.Timestamp) string {
return fmt.Sprintf(
"%s%s %d %d",
f.sanitizeKeyBytes([]byte(name)),
attributes,
value,
timestamp/pcommon.Timestamp(time.Millisecond),
)
}
// doubleValueLine returns prometheus line with given value
func (f *prometheusFormatter) doubleValueLine(name string, value float64, dp dataPoint, attributes pcommon.Map) string {
return f.doubleLine(
name,
f.tags2String(attributes, dp.Attributes()),
value,
dp.Timestamp(),
)
}
// uintValueLine returns prometheus line with given value
func (f *prometheusFormatter) uintValueLine(name string, value uint64, dp dataPoint, attributes pcommon.Map) string {
return f.uintLine(
name,
f.tags2String(attributes, dp.Attributes()),
value,
dp.Timestamp(),
)
}
// numberDataPointValueLine returns prometheus line with value from pmetric.NumberDataPoint
func (f *prometheusFormatter) numberDataPointValueLine(name string, dp pmetric.NumberDataPoint, attributes pcommon.Map) string {
switch dp.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
return f.doubleValueLine(
name,
dp.DoubleValue(),
dp,
attributes,
)
case pmetric.NumberDataPointValueTypeInt:
return f.intLine(
name,
f.tags2String(attributes, dp.Attributes()),
dp.IntValue(),
dp.Timestamp(),
)
}
return ""
}
// sumMetric returns _sum suffixed metric name
func (f *prometheusFormatter) sumMetric(name string) string {
return fmt.Sprintf("%s_sum", name)
}
// countMetric returns _count suffixed metric name
func (f *prometheusFormatter) countMetric(name string) string {
return fmt.Sprintf("%s_count", name)
}
// bucketMetric returns _bucket suffixed metric name
func (f *prometheusFormatter) bucketMetric(name string) string {
return fmt.Sprintf("%s_bucket", name)
}
// mergeAttributes gets two pcommon.Maps and returns new which contains values from both of them
func (f *prometheusFormatter) mergeAttributes(attributes pcommon.Map, additionalAttributes pcommon.Map) pcommon.Map {
mergedAttributes := pcommon.NewMap()
mergedAttributes.EnsureCapacity(attributes.Len() + additionalAttributes.Len())
attributes.CopyTo(mergedAttributes)
additionalAttributes.Range(func(k string, v pcommon.Value) bool {
v.CopyTo(mergedAttributes.PutEmpty(k))
return true
})
return mergedAttributes
}
// doubleGauge2Strings converts DoubleGauge record to a list of strings (one per dataPoint)
func (f *prometheusFormatter) gauge2Strings(metric pmetric.Metric, attributes pcommon.Map) []string {
dps := metric.Gauge().DataPoints()
lines := make([]string, 0, dps.Len())
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
line := f.numberDataPointValueLine(
metric.Name(),
dp,
attributes,
)
lines = append(lines, line)
}
return lines
}
// doubleSum2Strings converts Sum record to a list of strings (one per dataPoint)
func (f *prometheusFormatter) sum2Strings(metric pmetric.Metric, attributes pcommon.Map) []string {
dps := metric.Sum().DataPoints()
lines := make([]string, 0, dps.Len())
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
line := f.numberDataPointValueLine(
metric.Name(),
dp,
attributes,
)
lines = append(lines, line)
}
return lines
}
// summary2Strings converts Summary record to a list of strings
// n+2 where n is number of quantiles and 2 stands for sum and count metrics per each data point
func (f *prometheusFormatter) summary2Strings(metric pmetric.Metric, attributes pcommon.Map) []string {
dps := metric.Summary().DataPoints()
var lines []string
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
qs := dp.QuantileValues()
additionalAttributes := pcommon.NewMap()
for i := 0; i < qs.Len(); i++ {
q := qs.At(i)
additionalAttributes.PutDouble(prometheusQuantileTag, q.Quantile())
line := f.doubleValueLine(
metric.Name(),
q.Value(),
dp,
f.mergeAttributes(attributes, additionalAttributes),
)
lines = append(lines, line)
}
line := f.doubleValueLine(
f.sumMetric(metric.Name()),
dp.Sum(),
dp,
attributes,
)
lines = append(lines, line)
line = f.uintValueLine(
f.countMetric(metric.Name()),
dp.Count(),
dp,
attributes,
)
lines = append(lines, line)
}
return lines
}
// histogram2Strings converts Histogram record to a list of strings,
// (n+1) where n is number of bounds plus two for sum and count per each data point
func (f *prometheusFormatter) histogram2Strings(metric pmetric.Metric, attributes pcommon.Map) []string {
dps := metric.Histogram().DataPoints()
var lines []string
for i := 0; i < dps.Len(); i++ {
dp := dps.At(i)
explicitBounds := dp.ExplicitBounds()
if explicitBounds.Len() == 0 {
continue
}
var cumulative uint64
additionalAttributes := pcommon.NewMap()
for i := 0; i < explicitBounds.Len(); i++ {
bound := explicitBounds.At(i)
cumulative += dp.BucketCounts().At(i)
additionalAttributes.PutDouble(prometheusLeTag, bound)
line := f.uintValueLine(
f.bucketMetric(metric.Name()),
cumulative,
dp,
f.mergeAttributes(attributes, additionalAttributes),
)
lines = append(lines, line)
}
cumulative += dp.BucketCounts().At(explicitBounds.Len())
additionalAttributes.PutStr(prometheusLeTag, prometheusInfValue)
line := f.uintValueLine(
f.bucketMetric(metric.Name()),
cumulative,
dp,
f.mergeAttributes(attributes, additionalAttributes),
)
lines = append(lines, line)
line = f.doubleValueLine(
f.sumMetric(metric.Name()),
dp.Sum(),
dp,
attributes,
)
lines = append(lines, line)
line = f.uintValueLine(
f.countMetric(metric.Name()),
dp.Count(),
dp,
attributes,
)
lines = append(lines, line)
}
return lines
}
// metric2String returns stringified metricPair
func (f *prometheusFormatter) metric2String(metric pmetric.Metric, attributes pcommon.Map) string {
var lines []string
switch metric.Type() {
case pmetric.MetricTypeGauge:
lines = f.gauge2Strings(metric, attributes)
case pmetric.MetricTypeSum:
lines = f.sum2Strings(metric, attributes)
case pmetric.MetricTypeSummary:
lines = f.summary2Strings(metric, attributes)
case pmetric.MetricTypeHistogram:
lines = f.histogram2Strings(metric, attributes)
}
return strings.Join(lines, "\n")
}