/
metrics_collector.go
107 lines (92 loc) · 2.74 KB
/
metrics_collector.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
package stats
import (
"github.com/prometheus/client_golang/prometheus"
)
// ensure Stats implements the prometheus Collector interface
var _ prometheus.Collector = Stats{}
// Descriptors used to create the metrics
var (
requestCountDesc = prometheus.NewDesc(
"marin3r_xdss_discovery_requests_total",
"Number of discovery requests",
[]string{"node_id", "resource_type", "pod_name"}, nil,
)
ackCountDesc = prometheus.NewDesc(
"marin3r_xdss_discovery_ack_total",
"Number of discovery ACK responses",
[]string{"node_id", "resource_type", "pod_name"}, nil,
)
nackCountDesc = prometheus.NewDesc(
"marin3r_xdss_discovery_nack_total",
"Number of discovery NACK responses",
[]string{"node_id", "resource_type", "pod_name"}, nil,
)
infoDesc = prometheus.NewDesc(
"marin3r_xdss_discovery_info",
"Information about the version a certain resource type is at",
[]string{"node_id", "resource_type", "pod_name", "version"}, nil,
)
)
// Describe is implemented with DescribeByCollect. That's possible because the
// Collect method will always return the same 4 metrics with the same 4
// descriptors.
func (xmc Stats) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(xmc, ch)
}
// Collect dumps all the keys in the stats cache. Then it
// creates constant metrics for each modeID/resourceType/pod on the fly based on the
// dumped returned data.
func (s Stats) Collect(ch chan<- prometheus.Metric) {
items := s.DumpAll()
for k, v := range items {
key := NewKeyFromString(k)
switch metric := key.StatName + "/" + key.Version; metric {
case "request_counter/*":
ch <- prometheus.MustNewConstMetric(
requestCountDesc,
prometheus.CounterValue,
float64(v.Object.(int64)),
key.NodeID, key.ResourceType, key.PodID,
)
case "ack_counter/*":
ch <- prometheus.MustNewConstMetric(
ackCountDesc,
prometheus.CounterValue,
float64(v.Object.(int64)),
key.NodeID, key.ResourceType, key.PodID,
)
case "nack_counter/*":
ch <- prometheus.MustNewConstMetric(
nackCountDesc,
prometheus.CounterValue,
float64(v.Object.(int64)),
key.NodeID, key.ResourceType, key.PodID,
)
}
}
// expose info metrics
type currentVersion struct {
version string
ts int64
}
info := map[string]currentVersion{}
for k, v := range items {
key := NewKeyFromString(k)
if key.StatName == "info" {
version := key.Version
key.Version = "*"
if cv, ok := info[key.String()]; !ok || v.Object.(int64) > cv.ts {
info[key.String()] = currentVersion{version, v.Object.(int64)}
}
}
}
for k, v := range info {
key := NewKeyFromString(k)
ch <- prometheus.MustNewConstMetric(
infoDesc,
prometheus.UntypedValue,
float64(0),
key.NodeID, key.ResourceType, key.PodID, v.version,
)
}
}