-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
metric.go
451 lines (391 loc) · 13 KB
/
metric.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
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package prometheus
import (
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/elastic-agent-libs/mapstr"
)
// MetricMap defines the mapping from Prometheus metric to a Metricbeat field
type MetricMap interface {
// GetOptions returns the list of metric options
GetOptions() []MetricOption
// GetField returns the resulting field name
GetField() string
// GetValue returns the resulting value
GetValue(m *OpenMetric) interface{}
// GetConfiguration returns the configuration for the metric
GetConfiguration() Configuration
}
// Configuration for mappings that needs extended treatment
type Configuration struct {
// StoreNonMappedLables indicates if labels found at the metric that are
// not found at the label map should be part of the resulting event.
// This setting should be used when the label name is not known beforehand
StoreNonMappedLabels bool
// NonMappedLabelsPlacement is used when StoreNonMappedLabels is set to true, and
// defines the key path at the event under which to store the dynamically found labels.
// This key path will be added to the events that match this metric along with a subset of
// key/value pairs will be created under it, one for each non mapped label found.
//
// Example:
//
// given a metric family in a prometheus resource in the form:
// metric1{label1="value1",label2="value2"} 1
// and not mapping labels but using this entry on a the MetriMap definition:
// "metric1": ExtendedInfoMetric(Configuration{StoreNonMappedLabels: true, NonMappedLabelsPlacement: "mypath"}),
// would output an event that contains a metricset field as follows
// "mypath": {"label1":"value1","label2":"value2"}
//
NonMappedLabelsPlacement string
// MetricProcessing options are a set of functions that will be
// applied to metrics after they are retrieved
MetricProcessingOptions []MetricOption
// ExtraFields is used to add fields to the
// event where this metric is included
ExtraFields mapstr.M
}
// MetricOption adds settings to Metric objects behavior
type MetricOption interface {
// Process a tuple of field, value and labels from a metric, return the same tuple updated
Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M)
}
// OpFilterMap only processes metrics matching the given filter
func OpFilterMap(label string, filterMap map[string]string) MetricOption {
return opFilterMap{
label: label,
filterMap: filterMap,
}
}
// OpLowercaseValue lowercases the value if it's a string
func OpLowercaseValue() MetricOption {
return opLowercaseValue{}
}
// OpUnixTimestampValue parses a value into a Unix timestamp
func OpUnixTimestampValue() MetricOption {
return opUnixTimestampValue{}
}
// OpMultiplyBuckets multiplies bucket labels in histograms, useful to change units
func OpMultiplyBuckets(multiplier float64) MetricOption {
return opMultiplyBuckets{
multiplier: multiplier,
}
}
// OpSetSuffix extends the field's name with the given suffix if the value of the metric
// is numeric (and not histogram or quantile), otherwise does nothing
func OpSetNumericMetricSuffix(suffix string) MetricOption {
return opSetNumericMetricSuffix{
suffix: suffix,
}
}
// Metric directly maps a Prometheus metric to a Metricbeat field
func Metric(field string, options ...MetricOption) MetricMap {
return &commonMetric{
field: field,
config: Configuration{MetricProcessingOptions: options},
}
}
// KeywordMetric maps a Prometheus metric to a Metricbeat field, stores the
// given keyword when source metric value is 1
func KeywordMetric(field, keyword string, options ...MetricOption) MetricMap {
return &keywordMetric{
commonMetric{
field: field,
config: Configuration{MetricProcessingOptions: options},
},
keyword,
}
}
// BooleanMetric maps a Prometheus metric to a Metricbeat field of bool type
func BooleanMetric(field string, options ...MetricOption) MetricMap {
return &booleanMetric{
commonMetric{
field: field,
config: Configuration{MetricProcessingOptions: options},
},
}
}
// LabelMetric maps a Prometheus metric to a Metricbeat field, stores the value
// of a given label on it if the gauge value is 1
func LabelMetric(field, label string, options ...MetricOption) MetricMap {
return &labelMetric{
commonMetric{
field: field,
config: Configuration{MetricProcessingOptions: options},
},
label,
}
}
// InfoMetric obtains info labels from the given metric and puts them
// into events matching all the key labels present in the metric
func InfoMetric(options ...MetricOption) MetricMap {
return &infoMetric{
commonMetric{
config: Configuration{MetricProcessingOptions: options},
},
}
}
// ExtendedInfoMetric obtains info labels from the given metric and puts them
// into events matching all the key labels present in the metric
func ExtendedInfoMetric(configuration Configuration) MetricMap {
return &infoMetric{
commonMetric{
config: configuration,
},
}
}
// ExtendedMetric is a metric item that allows extended behaviour
// through configuration
func ExtendedMetric(field string, configuration Configuration) MetricMap {
return &commonMetric{
field: field,
config: configuration,
}
}
type commonMetric struct {
field string
config Configuration
}
// GetOptions returns the list of metric options
func (m *commonMetric) GetOptions() []MetricOption {
return m.config.MetricProcessingOptions
}
// GetField returns the resulting field name
func (m *commonMetric) GetField() string {
return m.field
}
// GetConfiguration returns the configuration for the metric
func (m *commonMetric) GetConfiguration() Configuration {
return m.config
}
// GetValue returns the resulting value
func (m *commonMetric) GetValue(metric *OpenMetric) interface{} {
counter := metric.GetCounter()
if counter != nil {
if !math.IsNaN(counter.GetValue()) && !math.IsInf(counter.GetValue(), 0) {
return int64(counter.GetValue())
}
}
gauge := metric.GetGauge()
if gauge != nil {
if !math.IsNaN(gauge.GetValue()) && !math.IsInf(gauge.GetValue(), 0) {
return gauge.GetValue()
}
}
summary := metric.GetSummary()
if summary != nil {
value := mapstr.M{}
if !math.IsNaN(summary.GetSampleSum()) && !math.IsInf(summary.GetSampleSum(), 0) {
value["sum"] = summary.GetSampleSum()
value["count"] = summary.GetSampleCount()
}
quantiles := summary.GetQuantile()
percentileMap := mapstr.M{}
for _, quantile := range quantiles {
if !math.IsNaN(quantile.GetValue()) && !math.IsInf(quantile.GetValue(), 0) {
key := strconv.FormatFloat(100*quantile.GetQuantile(), 'f', -1, 64)
percentileMap[key] = quantile.GetValue()
}
}
if len(percentileMap) != 0 {
value["percentile"] = percentileMap
}
return value
}
histogram := metric.GetHistogram()
if histogram != nil {
value := mapstr.M{}
if !math.IsNaN(histogram.GetSampleSum()) && !math.IsInf(histogram.GetSampleSum(), 0) {
value["sum"] = histogram.GetSampleSum()
value["count"] = histogram.GetSampleCount()
}
buckets := histogram.GetBucket()
bucketMap := mapstr.M{}
for _, bucket := range buckets {
if bucket.GetCumulativeCount() != uint64(math.NaN()) && bucket.GetCumulativeCount() != uint64(math.Inf(0)) {
key := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64)
bucketMap[key] = bucket.GetCumulativeCount()
}
}
if len(bucketMap) != 0 {
value["bucket"] = bucketMap
}
return value
}
// Other types are not supported here
return nil
}
type keywordMetric struct {
commonMetric
keyword string
}
// GetValue returns the resulting value
func (m *keywordMetric) GetValue(metric *OpenMetric) interface{} {
if gauge := metric.GetGauge(); gauge != nil && gauge.GetValue() == 1 {
return m.keyword
}
return nil
}
type booleanMetric struct {
commonMetric
}
// GetValue returns the resulting value
func (m *booleanMetric) GetValue(metric *OpenMetric) interface{} {
if gauge := metric.GetGauge(); gauge != nil {
return gauge.GetValue() == 1
}
return nil
}
type labelMetric struct {
commonMetric
label string
}
// GetValue returns the resulting value
func (m *labelMetric) GetValue(metric *OpenMetric) interface{} {
if gauge := metric.GetGauge(); gauge != nil && gauge.GetValue() == 1 {
return getLabel(metric, m.label)
}
return nil
}
func getLabel(metric *OpenMetric, name string) string {
for _, label := range metric.GetLabel() {
if label.Name == name {
return label.Value
}
}
return ""
}
type infoMetric struct {
commonMetric
}
// GetValue returns the resulting value
func (m *infoMetric) GetValue(metric *OpenMetric) interface{} {
return ""
}
// GetField returns the resulting field name
func (m *infoMetric) GetField() string {
return ""
}
type opFilterMap struct {
label string
filterMap map[string]string
}
// Called by the Prometheus helper to apply extra options on retrieved metrics
// Check whether the value of the specified label is allowed and, if yes, return the metric via the specified mapped field
// Else, if the specified label does not match the filter, return nil
// This is useful in cases where multiple Metricbeat fields need to be defined per Prometheus metric, based on label values
func (o opFilterMap) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
for k, v := range o.filterMap {
if labels[o.label] == k {
if field == "" {
return v, value, labels
} else {
return fmt.Sprintf("%v.%v", field, v), value, labels
}
}
}
return "", nil, nil
}
type opLowercaseValue struct{}
// Process will lowercase the given value if it's a string
func (o opLowercaseValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
if val, ok := value.(string); ok {
value = strings.ToLower(val)
}
return field, value, labels
}
type opMultiplyBuckets struct {
multiplier float64
}
// Process will multiply the bucket labels if it is an histogram with numeric labels
func (o opMultiplyBuckets) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
histogram, ok := value.(mapstr.M)
if !ok {
return field, value, labels
}
bucket, ok := histogram["bucket"].(mapstr.M)
if !ok {
return field, value, labels
}
sum, ok := histogram["sum"].(float64)
if !ok {
return field, value, labels
}
multiplied := mapstr.M{}
for k, v := range bucket {
if f, err := strconv.ParseFloat(k, 64); err == nil {
key := strconv.FormatFloat(f*o.multiplier, 'f', -1, 64)
multiplied[key] = v
} else {
multiplied[k] = v
}
}
histogram["bucket"] = multiplied
histogram["sum"] = sum * o.multiplier
return field, histogram, labels
}
type opSetNumericMetricSuffix struct {
suffix string
}
// Process will extend the field's name with the given suffix
func (o opSetNumericMetricSuffix) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
_, ok := value.(float64)
if !ok {
return field, value, labels
}
field = fmt.Sprintf("%v.%v", field, o.suffix)
return field, value, labels
}
type opUnixTimestampValue struct {
}
// Process converts a value in seconds into an unix time
func (o opUnixTimestampValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
return field, common.Time(time.Unix(int64(value.(float64)), 0)), labels
}
// OpLabelKeyPrefixRemover removes prefix from label keys
func OpLabelKeyPrefixRemover(prefix string) MetricOption {
return opLabelKeyPrefixRemover{prefix}
}
// opLabelKeyPrefixRemover is a metric option processor that removes a prefix from the key of a label set
type opLabelKeyPrefixRemover struct {
Prefix string
}
// Process modifies the labels map, removing a prefix when found at keys of the labels set.
// For each label, if the key is found a new key will be created hosting the same value and the
// old key will be deleted.
// Fields, values and not prefixed labels will remain unmodified.
func (o opLabelKeyPrefixRemover) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) {
renameKeys := []string{}
for k := range labels {
if len(k) < len(o.Prefix) {
continue
}
if k[:6] == o.Prefix {
renameKeys = append(renameKeys, k)
}
}
for i := range renameKeys {
v := labels[renameKeys[i]]
delete(labels, renameKeys[i])
labels[renameKeys[i][len(o.Prefix):]] = v
}
return "", value, labels
}