-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
224 lines (188 loc) · 5.32 KB
/
parse.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
package prometheus
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/sirupsen/logrus"
)
func ParsedToMetric(labels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
if raw.Type == nil {
return nil, errors.New("Invalid metric type?")
}
switch *raw.Type {
case io_prometheus_client.MetricType_COUNTER:
return parseCounter(labels, raw)
case io_prometheus_client.MetricType_GAUGE:
return parseGauge(labels, raw)
case io_prometheus_client.MetricType_SUMMARY:
return parseSummary(labels, raw)
case io_prometheus_client.MetricType_UNTYPED:
return parseUntyped(labels, raw)
case io_prometheus_client.MetricType_HISTOGRAM:
return parseHistogram(labels, raw)
default:
logrus.Debugf("Unknown type: %+v", raw)
}
panic("This shouldn't be reachable..")
}
func parseLabelDefinitions(labels map[string]string, raw *io_prometheus_client.Metric) []string {
out := make([]string, 0)
for key := range labels {
out = append(out, key)
}
for _, label := range raw.Label {
out = append(out, label.GetName())
}
return out
}
func parseLabelValues(order []string, labels map[string]string, raw *io_prometheus_client.Metric) []string {
// prometheus expects the values in the same order as keys
// sadly iterating over a map in golang does NOT always mean
// the results are returned in the same order, so instead we pass
// both the extra label values (labels) and the order that we want everything in.
out := make([]string, 0)
for _, key := range order {
value, ok := labels[key]
if ok {
out = append(out, value)
} else {
for _, label := range raw.Label {
if label.GetName() == key {
out = append(out, label.GetValue())
break
}
}
}
}
return out
}
func parseCounter(extraLabels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
out := make([]prometheus.Metric, 0)
for _, metric := range raw.Metric {
labels := parseLabelDefinitions(extraLabels, metric)
m, err := prometheus.NewConstMetric(
prometheus.NewDesc(
raw.GetName(),
raw.GetHelp(),
labels,
nil),
prometheus.CounterValue,
metric.Counter.GetValue(),
parseLabelValues(labels, extraLabels, metric)...,
)
if err != nil {
return nil, err
}
out = append(out, m)
}
return out, nil
}
func parseGauge(extraLabels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
out := make([]prometheus.Metric, 0)
for _, metric := range raw.Metric {
labels := parseLabelDefinitions(extraLabels, metric)
m, err := prometheus.NewConstMetric(
prometheus.NewDesc(
raw.GetName(),
raw.GetHelp(),
labels,
nil),
prometheus.GaugeValue,
metric.Gauge.GetValue(),
parseLabelValues(labels, extraLabels, metric)...,
)
if err != nil {
return nil, err
}
out = append(out, m)
}
return out, nil
}
func parseQuantiles(summary *io_prometheus_client.Summary) (map[float64]float64, error) {
out := make(map[float64]float64)
for _, value := range summary.Quantile {
out[value.GetQuantile()] = value.GetValue()
}
return out, nil
}
func parseSummary(extraLabels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
out := make([]prometheus.Metric, 0)
for _, metric := range raw.Metric {
labels := parseLabelDefinitions(extraLabels, metric)
quantiles, err := parseQuantiles(metric.Summary)
if err != nil {
return nil, err
}
m, err := prometheus.NewConstSummary(
prometheus.NewDesc(
raw.GetName(),
raw.GetHelp(),
labels,
nil),
metric.Summary.GetSampleCount(),
metric.Summary.GetSampleSum(),
quantiles,
parseLabelValues(labels, extraLabels, metric)...,
)
if err != nil {
return nil, err
}
out = append(out, m)
}
return out, nil
}
func parseUntyped(extraLabels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
out := make([]prometheus.Metric, 0)
for _, metric := range raw.Metric {
labels := parseLabelDefinitions(extraLabels, metric)
m, err := prometheus.NewConstMetric(
prometheus.NewDesc(
raw.GetName(),
raw.GetHelp(),
labels,
nil),
prometheus.UntypedValue,
metric.Untyped.GetValue(),
parseLabelValues(labels, extraLabels, metric)...,
)
if err != nil {
return nil, err
}
out = append(out, m)
}
return out, nil
}
func parseBuckets(histogram *io_prometheus_client.Histogram) (map[float64]uint64, error) {
out := make(map[float64]uint64)
for _, value := range histogram.Bucket {
out[value.GetUpperBound()] = value.GetCumulativeCount()
}
return out, nil
}
func parseHistogram(extraLabels map[string]string, raw *io_prometheus_client.MetricFamily) ([]prometheus.Metric, error) {
out := make([]prometheus.Metric, 0)
for _, metric := range raw.Metric {
labels := parseLabelDefinitions(extraLabels, metric)
buckets, err := parseBuckets(metric.Histogram)
if err != nil {
return nil, err
}
m, err := prometheus.NewConstHistogram(
prometheus.NewDesc(
raw.GetName(),
raw.GetHelp(),
labels,
nil,
),
metric.Histogram.GetSampleCount(),
metric.Histogram.GetSampleSum(),
buckets,
parseLabelValues(labels, extraLabels, metric)...,
)
if err != nil {
return nil, err
}
out = append(out, m)
}
return out, nil
}