-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
84 lines (70 loc) · 1.97 KB
/
metrics.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
package king
import (
"fmt"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
// Register registers prometheus flag collectors that display configured flags as
// metrics. The genereated metrics are of the form:
//
// kong_flag{program="progname", name="flagname", value="flagvalue"} 1
func (m Map) Register(program string, registerer prometheus.Registerer) Map {
bi, ok := m[buildInfoKey]
if ok {
b := bi.(*BuildInfo)
b.register(program, registerer)
delete(m, buildInfoKey)
}
for _, c := range m.collectors(program) {
registerer.MustRegister(c)
}
return m
}
func (m Map) collectors(program string) []prometheus.Collector {
collectors := []prometheus.Collector{}
for name, value := range m {
if isRedacted(value) || strings.HasPrefix(name, "buildinfo-") {
continue
}
collectors = append(collectors, newFlagCollector(program, name, fmt.Sprintf("%v", value)))
}
return collectors
}
func (b *BuildInfo) register(program string, reg prometheus.Registerer) {
buildInfoGauge := prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "kong_build_info",
Help: "A metric with a constant '1' value labeled by program, version, go, date and revision",
ConstLabels: prometheus.Labels{
"program": program,
"version": b.version,
"go": b.goVersion,
"date": b.date.String(),
"revision": b.revision,
},
},
func() float64 { return 1 },
)
reg.MustRegister(buildInfoGauge)
}
func newFlagCollector(program string, flags ...string) prometheus.Collector {
return prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "kong_flag",
Help: "A metric with a constant '1' value labeled by program, flag name and value",
ConstLabels: prometheus.Labels{
"program": program,
"name": flags[0],
"value": flags[1],
},
},
func() float64 { return 1 },
)
}
func isRedacted(value interface{}) bool {
v, ok := value.(string)
if !ok {
return false
}
return v == strings.Repeat(string(redactChar), len(v))
}