-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
transformer.go
90 lines (76 loc) · 2.06 KB
/
transformer.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
package prometheus
import (
"sort"
dto "github.com/prometheus/client_model/go"
)
// Transformer modifies prometheus metrics families.
type Transformer interface {
// Transform updates the metrics family
Transform(mfs []*dto.MetricFamily) []*dto.MetricFamily
}
var _ Transformer = (*AddLabels)(nil)
// AddLabels adds labels to all metrics. It will overwrite
// the label if it already exists.
type AddLabels struct {
Labels map[string]string
}
// Transform adds labels to the metrics.
func (a *AddLabels) Transform(mfs []*dto.MetricFamily) []*dto.MetricFamily {
for i := range mfs {
for j, m := range mfs[i].Metric {
// Filter out labels to add
labels := m.Label[:0]
for _, l := range m.Label {
if _, ok := a.Labels[l.GetName()]; !ok {
labels = append(labels, l)
}
}
// Add all new labels to the metric
for k, v := range a.Labels {
labels = append(labels, L(k, v))
}
sort.Sort(labelPairSorter(labels))
mfs[i].Metric[j].Label = labels
}
}
return mfs
}
var _ Transformer = (*RemoveLabels)(nil)
// RemoveLabels adds labels to all metrics. It will overwrite
// the label if it already exists.
type RemoveLabels struct {
Labels map[string]struct{}
}
// Transform removes labels from the metrics.
func (r *RemoveLabels) Transform(mfs []*dto.MetricFamily) []*dto.MetricFamily {
for i := range mfs {
for j, m := range mfs[i].Metric {
// Filter out labels
labels := m.Label[:0]
for _, l := range m.Label {
if _, ok := r.Labels[l.GetName()]; !ok {
labels = append(labels, l)
}
}
mfs[i].Metric[j].Label = labels
}
}
return mfs
}
var _ Transformer = (*RenameFamilies)(nil)
// RenameFamilies changes the name of families to another name
type RenameFamilies struct {
FromTo map[string]string
}
// Transform renames metric families names.
func (r *RenameFamilies) Transform(mfs []*dto.MetricFamily) []*dto.MetricFamily {
renamed := mfs[:0]
for _, mf := range mfs {
if to, ok := r.FromTo[mf.GetName()]; ok {
mf.Name = &to
}
renamed = append(renamed, mf)
}
sort.Sort(familySorter(renamed))
return renamed
}