-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathcollection.go
More file actions
501 lines (428 loc) · 10.7 KB
/
collection.go
File metadata and controls
501 lines (428 loc) · 10.7 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package prometheus
import (
"hash/fnv"
"sort"
"strconv"
"strings"
"time"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
"github.com/influxdata/telegraf"
)
const helpString = "Telegraf collected metric"
type metricFamily struct {
name string
typ telegraf.ValueType
}
type promMetric struct {
labels []labelPair
time time.Time
addTime time.Time
scaler *scaler
histogram *histogram
summary *summary
}
type labelPair struct {
name string
value string
}
type scaler struct {
value float64
}
type bucket struct {
bound float64
count uint64
}
type quantile struct {
quantile float64
value float64
}
type histogram struct {
buckets []bucket
count uint64
sum float64
}
func (h *histogram) merge(b bucket) {
for i := range h.buckets {
if h.buckets[i].bound == b.bound {
h.buckets[i].count = b.count
return
}
}
h.buckets = append(h.buckets, b)
}
type summary struct {
quantiles []quantile
count uint64
sum float64
}
func (s *summary) merge(q quantile) {
for i := range s.quantiles {
if s.quantiles[i].quantile == q.quantile {
s.quantiles[i].value = q.value
return
}
}
s.quantiles = append(s.quantiles, q)
}
type metricKey uint64
func makeMetricKey(labels []labelPair) metricKey {
h := fnv.New64a()
for _, label := range labels {
h.Write([]byte(label.name))
h.Write([]byte("\x00"))
h.Write([]byte(label.value))
h.Write([]byte("\x00"))
}
return metricKey(h.Sum64())
}
type entry struct {
family metricFamily
metrics map[metricKey]*promMetric
}
// Collection is a cache of metrics that are being processed.
type Collection struct {
entries map[metricFamily]entry
config FormatConfig
}
// NewCollection creates a new Collection instance.
func NewCollection(config FormatConfig) *Collection {
cache := &Collection{
entries: make(map[metricFamily]entry),
config: config,
}
return cache
}
func (c *Collection) sanitizeMetricName(name string) (string, bool) {
return SanitizeMetricNameByEncoding(name, c.config.NameSanitization)
}
func (c *Collection) sanitizeLabelName(name string) (string, bool) {
return SanitizeLabelNameByEncoding(name, c.config.NameSanitization)
}
func hasLabel(name string, labels []labelPair) bool {
for _, label := range labels {
if name == label.name {
return true
}
}
return false
}
func (c *Collection) createLabels(metric telegraf.Metric) []labelPair {
labels := make([]labelPair, 0, len(metric.TagList()))
for _, tag := range metric.TagList() {
// Ignore special tags for histogram and summary types.
switch metric.Type() {
case telegraf.Histogram:
if tag.Key == "le" {
continue
}
case telegraf.Summary:
if tag.Key == "quantile" {
continue
}
}
name, ok := c.sanitizeLabelName(tag.Key)
if !ok {
continue
}
labels = append(labels, labelPair{name: name, value: tag.Value})
}
if !c.config.StringAsLabel {
return labels
}
addedFieldLabel := false
for _, field := range metric.FieldList() {
value, ok := field.Value.(string)
if !ok {
continue
}
name, ok := c.sanitizeLabelName(field.Key)
if !ok {
continue
}
// If there is a tag with the same name as the string field, discard
// the field and use the tag instead.
if hasLabel(name, labels) {
continue
}
labels = append(labels, labelPair{name: name, value: value})
addedFieldLabel = true
}
if addedFieldLabel {
sort.Slice(labels, func(i, j int) bool {
return labels[i].name < labels[j].name
})
}
return labels
}
// Add adds a metric to the collection. It will create a new entry if the metric is not already present.
func (c *Collection) Add(m telegraf.Metric, now time.Time) {
labels := c.createLabels(m)
for _, field := range m.FieldList() {
metricName := MetricName(m.Name(), field.Key, m.Type())
metricName, ok := c.sanitizeMetricName(metricName)
if !ok {
continue
}
metricType := c.config.TypeMappings.DetermineType(metricName, m)
family := metricFamily{
name: metricName,
typ: metricType,
}
singleEntry, ok := c.entries[family]
if !ok {
singleEntry = entry{
family: family,
metrics: make(map[metricKey]*promMetric),
}
c.entries[family] = singleEntry
}
metricKey := makeMetricKey(labels)
existingMetric, ok := singleEntry.metrics[metricKey]
if ok {
// A batch of metrics can contain multiple values for a single
// Prometheus sample. If this metric is older than the existing
// sample then we can skip over it.
if m.Time().Before(existingMetric.time) {
continue
}
}
switch m.Type() {
case telegraf.Counter:
fallthrough
case telegraf.Gauge:
fallthrough
case telegraf.Untyped:
value, ok := SampleValue(field.Value)
if !ok {
continue
}
existingMetric = &promMetric{
labels: labels,
time: m.Time(),
addTime: now,
scaler: &scaler{value: value},
}
singleEntry.metrics[metricKey] = existingMetric
case telegraf.Histogram:
if existingMetric == nil {
existingMetric = &promMetric{
labels: labels,
time: m.Time(),
addTime: now,
histogram: &histogram{},
}
} else {
existingMetric.time = m.Time()
existingMetric.addTime = now
}
switch {
case strings.HasSuffix(field.Key, "_bucket"):
le, ok := m.GetTag("le")
if !ok {
continue
}
bound, err := strconv.ParseFloat(le, 64)
if err != nil {
continue
}
count, ok := SampleCount(field.Value)
if !ok {
continue
}
existingMetric.histogram.merge(bucket{
bound: bound,
count: count,
})
case strings.HasSuffix(field.Key, "_sum"):
sum, ok := SampleSum(field.Value)
if !ok {
continue
}
existingMetric.histogram.sum = sum
case strings.HasSuffix(field.Key, "_count"):
count, ok := SampleCount(field.Value)
if !ok {
continue
}
existingMetric.histogram.count = count
default:
continue
}
singleEntry.metrics[metricKey] = existingMetric
case telegraf.Summary:
if existingMetric == nil {
existingMetric = &promMetric{
labels: labels,
time: m.Time(),
addTime: now,
summary: &summary{},
}
} else {
existingMetric.time = m.Time()
existingMetric.addTime = now
}
switch {
case strings.HasSuffix(field.Key, "_sum"):
sum, ok := SampleSum(field.Value)
if !ok {
continue
}
existingMetric.summary.sum = sum
case strings.HasSuffix(field.Key, "_count"):
count, ok := SampleCount(field.Value)
if !ok {
continue
}
existingMetric.summary.count = count
default:
quantileTag, ok := m.GetTag("quantile")
if !ok {
continue
}
singleQuantile, err := strconv.ParseFloat(quantileTag, 64)
if err != nil {
continue
}
value, ok := SampleValue(field.Value)
if !ok {
continue
}
existingMetric.summary.merge(quantile{
quantile: singleQuantile,
value: value,
})
}
singleEntry.metrics[metricKey] = existingMetric
}
}
}
// Expire removes metrics that are older than the specified age.
func (c *Collection) Expire(now time.Time, age time.Duration) {
expireTime := now.Add(-age)
for _, entry := range c.entries {
for key, metric := range entry.metrics {
if metric.addTime.Before(expireTime) {
delete(entry.metrics, key)
if len(entry.metrics) == 0 {
delete(c.entries, entry.family)
}
}
}
}
}
// GetEntries returns a slice of all entries in the collection.
func (c *Collection) GetEntries() []entry {
entries := make([]entry, 0, len(c.entries))
for _, entry := range c.entries {
entries = append(entries, entry)
}
if c.config.SortMetrics {
sort.Slice(entries, func(i, j int) bool {
lhs := entries[i].family
rhs := entries[j].family
if lhs.name != rhs.name {
return lhs.name < rhs.name
}
return lhs.typ < rhs.typ
})
}
return entries
}
// GetMetrics returns a slice of all metrics in the entry.
func (c *Collection) GetMetrics(entry entry) []*promMetric {
metrics := make([]*promMetric, 0, len(entry.metrics))
for _, metric := range entry.metrics {
metrics = append(metrics, metric)
}
if c.config.SortMetrics {
sort.Slice(metrics, func(i, j int) bool {
lhs := metrics[i].labels
rhs := metrics[j].labels
if len(lhs) != len(rhs) {
return len(lhs) < len(rhs)
}
for index := range lhs {
l := lhs[index]
r := rhs[index]
if l.name != r.name {
return l.name < r.name
}
if l.value != r.value {
return l.value < r.value
}
}
return false
})
}
return metrics
}
// GetProto returns a slice of all metrics in the collection as protobuf messages.
func (c *Collection) GetProto() []*dto.MetricFamily {
result := make([]*dto.MetricFamily, 0, len(c.entries))
for _, entry := range c.GetEntries() {
mf := &dto.MetricFamily{
Name: proto.String(entry.family.name),
Type: metricType(entry.family.typ),
}
if !c.config.CompactEncoding {
mf.Help = proto.String(helpString)
}
for _, metric := range c.GetMetrics(entry) {
l := make([]*dto.LabelPair, 0, len(metric.labels))
for _, label := range metric.labels {
l = append(l, &dto.LabelPair{
Name: proto.String(label.name),
Value: proto.String(label.value),
})
}
m := &dto.Metric{
Label: l,
}
if c.config.ExportTimestamp {
m.TimestampMs = proto.Int64(metric.time.UnixNano() / int64(time.Millisecond))
}
switch entry.family.typ {
case telegraf.Gauge:
m.Gauge = &dto.Gauge{Value: proto.Float64(metric.scaler.value)}
case telegraf.Counter:
m.Counter = &dto.Counter{Value: proto.Float64(metric.scaler.value)}
case telegraf.Untyped:
m.Untyped = &dto.Untyped{Value: proto.Float64(metric.scaler.value)}
case telegraf.Histogram:
buckets := make([]*dto.Bucket, 0, len(metric.histogram.buckets))
for _, bucket := range metric.histogram.buckets {
buckets = append(buckets, &dto.Bucket{
UpperBound: proto.Float64(bucket.bound),
CumulativeCount: proto.Uint64(bucket.count),
})
}
m.Histogram = &dto.Histogram{
Bucket: buckets,
SampleCount: proto.Uint64(metric.histogram.count),
SampleSum: proto.Float64(metric.histogram.sum),
}
case telegraf.Summary:
quantiles := make([]*dto.Quantile, 0, len(metric.summary.quantiles))
for _, quantile := range metric.summary.quantiles {
quantiles = append(quantiles, &dto.Quantile{
Quantile: proto.Float64(quantile.quantile),
Value: proto.Float64(quantile.value),
})
}
m.Summary = &dto.Summary{
Quantile: quantiles,
SampleCount: proto.Uint64(metric.summary.count),
SampleSum: proto.Float64(metric.summary.sum),
}
default:
panic("unknown telegraf.ValueType")
}
mf.Metric = append(mf.Metric, m)
}
if len(mf.Metric) != 0 {
result = append(result, mf)
}
}
return result
}