forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_labels.go
60 lines (47 loc) · 1.05 KB
/
custom_labels.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
package main
import "github.com/czerwonk/ping_exporter/config"
type customLabelSet struct {
names []string
nameMap map[string]interface{}
}
func newCustomLabelSet(targets []config.TargetConfig) *customLabelSet {
cl := &customLabelSet{
nameMap: make(map[string]interface{}),
names: make([]string, 0),
}
for _, t := range targets {
cl.addLabelsForTarget(&t)
}
return cl
}
func (cl *customLabelSet) addLabelsForTarget(t *config.TargetConfig) {
if t.Labels == nil {
return
}
for name := range t.Labels {
cl.addLabel(name)
}
}
func (cl *customLabelSet) addLabel(name string) {
_, exists := cl.nameMap[name]
if exists {
return
}
cl.names = append(cl.names, name)
cl.nameMap[name] = nil
}
func (cl *customLabelSet) labelNames() []string {
return cl.names
}
func (cl *customLabelSet) labelValues(t config.TargetConfig) []string {
values := make([]string, len(cl.names))
if t.Labels == nil {
return values
}
for i, name := range cl.names {
if value, isSet := t.Labels[name]; isSet {
values[i] = value
}
}
return values
}