forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
96 lines (75 loc) · 1.98 KB
/
data.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
package collector
import (
"math"
"strconv"
"github.com/elastic/beats/libbeat/common"
dto "github.com/prometheus/client_model/go"
)
type PromEvent struct {
key string
value common.MapStr
labels common.MapStr
labelHash string
}
func GetPromEventsFromMetricFamily(mf *dto.MetricFamily) []PromEvent {
var events []PromEvent
name := *mf.Name
metrics := mf.Metric
for _, metric := range metrics {
event := PromEvent{
key: name,
labelHash: "#",
}
value := common.MapStr{}
labels := metric.Label
if len(labels) != 0 {
tagsMap := common.MapStr{}
for _, label := range labels {
if label.GetName() != "" && label.GetValue() != "" {
tagsMap[label.GetName()] = label.GetValue()
}
}
event.labels = tagsMap
event.labelHash = tagsMap.String()
}
counter := metric.GetCounter()
if counter != nil {
value["value"] = int64(counter.GetValue())
}
gauge := metric.GetGauge()
if gauge != nil {
value["value"] = gauge.GetValue()
}
summary := metric.GetSummary()
if summary != nil {
value["sum"] = summary.GetSampleSum()
value["count"] = summary.GetSampleCount()
quantiles := summary.GetQuantile()
percentileMap := common.MapStr{}
for _, quantile := range quantiles {
key := strconv.FormatFloat((100 * quantile.GetQuantile()), 'f', -1, 64)
if math.IsNaN(quantile.GetValue()) == false {
percentileMap[key] = quantile.GetValue()
}
}
if len(percentileMap) != 0 {
value["percentile"] = percentileMap
}
}
histogram := metric.GetHistogram()
if histogram != nil {
value["sum"] = histogram.GetSampleSum()
value["count"] = histogram.GetSampleCount()
buckets := histogram.GetBucket()
bucketMap := common.MapStr{}
for _, bucket := range buckets {
key := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64)
bucketMap[key] = bucket.GetCumulativeCount()
}
value["bucket"] = bucketMap
}
event.value = value
events = append(events, event)
}
return events
}