forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_v2.go
189 lines (174 loc) · 6.06 KB
/
metric_v2.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
package openmetrics
import (
"math"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
func (p *Parser) extractMetricsV2(ometrics *MetricFamily) []telegraf.Metric {
now := time.Now()
// Convert each openmetric metric to a corresponding telegraf metric
// with one field each. The process will filter NaNs in values and skip
// the corresponding metrics.
var metrics []telegraf.Metric
metricName := ometrics.GetName()
metricType := ometrics.GetType()
for _, om := range ometrics.GetMetrics() {
// Extract the timestamp of the metric if it exists and should
// not be ignored.
t := now
// Convert the labels to tags
tags := getTagsFromLabels(om, p.DefaultTags)
if ometrics.Unit != "" {
tags["unit"] = ometrics.Unit
}
// Construct the metrics
for _, omp := range om.GetMetricPoints() {
if omp.Timestamp != nil {
t = omp.GetTimestamp().AsTime()
}
switch metricType {
case MetricType_UNKNOWN:
x := omp.GetUnknownValue().GetValue()
if x == nil {
continue
}
var value float64
switch v := x.(type) {
case *UnknownValue_DoubleValue:
value = v.DoubleValue
case *UnknownValue_IntValue:
value = float64(v.IntValue)
}
if math.IsNaN(value) {
continue
}
fields := map[string]interface{}{metricName: value}
metrics = append(metrics, metric.New("openmetric", tags, fields, t, telegraf.Untyped))
case MetricType_GAUGE:
x := omp.GetGaugeValue().GetValue()
if x == nil {
continue
}
var value float64
switch v := x.(type) {
case *GaugeValue_DoubleValue:
value = v.DoubleValue
case *GaugeValue_IntValue:
value = float64(v.IntValue)
}
if math.IsNaN(value) {
continue
}
fields := map[string]interface{}{metricName: value}
metrics = append(metrics, metric.New("openmetric", tags, fields, t, telegraf.Gauge))
case MetricType_COUNTER:
x := omp.GetCounterValue().GetTotal()
if x == nil {
continue
}
var value float64
switch v := x.(type) {
case *CounterValue_DoubleValue:
value = v.DoubleValue
case *CounterValue_IntValue:
value = float64(v.IntValue)
}
if math.IsNaN(value) {
continue
}
fields := map[string]interface{}{metricName: value}
metrics = append(metrics, metric.New("openmetric", tags, fields, t, telegraf.Counter))
case MetricType_STATE_SET:
stateset := omp.GetStateSetValue()
// Add one metric per state
for _, state := range stateset.GetStates() {
sn := strings.ReplaceAll(state.GetName(), " ", "_")
fields := map[string]interface{}{metricName + "_" + sn: state.GetEnabled()}
metrics = append(metrics, metric.New("openmetric", tags, fields, t, telegraf.Untyped))
}
case MetricType_INFO:
info := omp.GetInfoValue().GetInfo()
mptags := make(map[string]string, len(tags)+len(info))
for k, v := range tags {
mptags[k] = v
}
for _, itag := range info {
mptags[itag.Name] = itag.Value
}
fields := map[string]interface{}{metricName + "_info": uint64(1)}
metrics = append(metrics, metric.New("openmetric", mptags, fields, t, telegraf.Untyped))
case MetricType_HISTOGRAM, MetricType_GAUGE_HISTOGRAM:
histogram := omp.GetHistogramValue()
// Add an overall metric containing the number of samples and and its sum
histFields := make(map[string]interface{})
histFields[metricName+"_count"] = float64(histogram.GetCount())
if s := histogram.GetSum(); s != nil {
switch v := s.(type) {
case *HistogramValue_DoubleValue:
histFields[metricName+"_sum"] = v.DoubleValue
case *HistogramValue_IntValue:
histFields[metricName+"_sum"] = float64(v.IntValue)
}
}
if ts := histogram.GetCreated(); ts != nil {
histFields[metricName+"_created"] = float64(ts.Seconds) + float64(ts.Nanos)/float64(time.Nanosecond)
}
metrics = append(metrics, metric.New("openmetric", tags, histFields, t, telegraf.Histogram))
// Add one metric per histogram bucket
var infSeen bool
for _, b := range histogram.GetBuckets() {
bucketTags := tags
bucketTags["le"] = strconv.FormatFloat(b.GetUpperBound(), 'g', -1, 64)
bucketFields := map[string]interface{}{
metricName + "_bucket": float64(b.GetCount()),
}
m := metric.New("openmetric", bucketTags, bucketFields, t, telegraf.Histogram)
metrics = append(metrics, m)
// Record if any of the buckets marks an infinite upper bound
infSeen = infSeen || math.IsInf(b.GetUpperBound(), +1)
}
// Infinity bucket is required for proper function of histogram in openmetric
if !infSeen {
infTags := tags
infTags["le"] = "+Inf"
infFields := map[string]interface{}{
metricName + "_bucket": float64(histogram.GetCount()),
}
m := metric.New("openmetric", infTags, infFields, t, telegraf.Histogram)
metrics = append(metrics, m)
}
case MetricType_SUMMARY:
summary := omp.GetSummaryValue()
// Add an overall metric containing the number of samples and and its sum
summaryFields := make(map[string]interface{})
summaryFields[metricName+"_count"] = float64(summary.GetCount())
if s := summary.GetSum(); s != nil {
switch v := s.(type) {
case *SummaryValue_DoubleValue:
summaryFields[metricName+"_sum"] = v.DoubleValue
case *SummaryValue_IntValue:
summaryFields[metricName+"_sum"] = float64(v.IntValue)
}
}
if ts := summary.GetCreated(); ts != nil {
summaryFields[metricName+"_created"] = float64(ts.Seconds) + float64(ts.Nanos)/float64(time.Nanosecond)
}
metrics = append(metrics, metric.New("openmetric", tags, summaryFields, t, telegraf.Summary))
// Add one metric per quantile
for _, q := range summary.Quantile {
quantileTags := tags
quantileTags["quantile"] = strconv.FormatFloat(q.GetQuantile(), 'g', -1, 64)
quantileFields := map[string]interface{}{
metricName: q.GetValue(),
}
m := metric.New("openmetric", quantileTags, quantileFields, t, telegraf.Summary)
metrics = append(metrics, m)
}
}
}
}
return metrics
}