forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_v1.go
163 lines (154 loc) · 5.04 KB
/
metric_v1.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
package openmetrics
import (
"math"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
)
func (p *Parser) extractMetricsV1(ometrics *MetricFamily) []telegraf.Metric {
now := time.Now()
// Convert each prometheus metrics to the corresponding telegraf metrics.
// You will get one telegraf metric with one field per prometheus metric
// for "simple" types like Gauge and Counter but a telegraf metric with
// multiple fields for "complex" types like Summary or Histogram.
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
}
// Iterate over the metric points and construct a metric for each
for _, omp := range om.GetMetricPoints() {
if omp.Timestamp != nil {
t = omp.GetTimestamp().AsTime()
}
// Construct the metrics
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{}{"value": value}
metrics = append(metrics, metric.New(metricName, 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{}{"gauge": value}
metrics = append(metrics, metric.New(metricName, 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{}{"counter": value}
metrics = append(metrics, metric.New(metricName, tags, fields, t, telegraf.Counter))
case MetricType_STATE_SET:
stateset := omp.GetStateSetValue()
// Collect the fields
fields := make(map[string]interface{}, len(stateset.States))
for _, state := range stateset.GetStates() {
fname := strings.ReplaceAll(state.GetName(), " ", "_")
fields[fname] = state.GetEnabled()
}
metrics = append(metrics, metric.New(metricName, tags, fields, t, telegraf.Untyped))
case MetricType_INFO:
info := omp.GetInfoValue().GetInfo()
fields := map[string]interface{}{"info": uint64(1)}
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
}
metrics = append(metrics, metric.New(metricName, mptags, fields, t, telegraf.Untyped))
case MetricType_HISTOGRAM, MetricType_GAUGE_HISTOGRAM:
histogram := omp.GetHistogramValue()
// Collect the fields
fields := make(map[string]interface{}, len(histogram.Buckets)+3)
fields["count"] = float64(histogram.GetCount())
if s := histogram.GetSum(); s != nil {
switch v := s.(type) {
case *HistogramValue_DoubleValue:
fields["sum"] = v.DoubleValue
case *HistogramValue_IntValue:
fields["sum"] = float64(v.IntValue)
}
}
if ts := histogram.GetCreated(); ts != nil {
fields["created"] = float64(ts.Seconds) + float64(ts.Nanos)/float64(time.Nanosecond)
}
for _, b := range histogram.Buckets {
fname := strconv.FormatFloat(b.GetUpperBound(), 'g', -1, 64)
fields[fname] = float64(b.GetCount())
}
metrics = append(metrics, metric.New(metricName, tags, fields, t, telegraf.Histogram))
case MetricType_SUMMARY:
summary := omp.GetSummaryValue()
// Collect the fields
fields := make(map[string]interface{}, len(summary.Quantile)+2)
fields["count"] = float64(summary.GetCount())
if s := summary.GetSum(); s != nil {
switch v := s.(type) {
case *SummaryValue_DoubleValue:
fields["sum"] = v.DoubleValue
case *SummaryValue_IntValue:
fields["sum"] = float64(v.IntValue)
}
}
if ts := summary.GetCreated(); ts != nil {
fields["created"] = float64(ts.Seconds) + float64(ts.Nanos)/float64(time.Second)
}
for _, q := range summary.GetQuantile() {
if v := q.GetValue(); !math.IsNaN(v) {
fname := strconv.FormatFloat(q.GetQuantile(), 'g', -1, 64)
fields[fname] = v
}
}
metrics = append(metrics, metric.New(metricName, tags, fields, t, telegraf.Summary))
}
}
}
return metrics
}