/
metrics.go
186 lines (178 loc) · 4.14 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
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
package floatingip
import (
"math"
"github.com/prometheus/client_golang/prometheus"
)
const (
metricNamespace = "flipop"
metricSubsystem = "floatingippoolcontroller"
)
var (
nodeStatusDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "node_status"),
`The total count of available nodes by status.`,
[]string{
"namespace",
"name",
"provider",
"dns",
"status",
},
nil,
)
ipAssignmentErrorsDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "ip_assignment_errors"),
`The total count of errors encountered while assigning an IP.`,
[]string{
"namespace",
"name",
"ip",
"provider",
"dns",
}, nil,
)
ipAssignmentsDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "ip_assignments"),
`The total count of assignments by IP.`,
[]string{
"namespace",
"name",
"ip",
"provider",
"dns",
}, nil,
)
ipNodeDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "ip_node"),
`Mapping of IPs to Nodes.`,
[]string{
"namespace",
"name",
"ip",
"provider",
"dns",
"provider_id",
"node",
}, nil,
)
ipStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "ip_state"),
`The state ("active", "in-progress", "error", "no-match", "unassigned", or "disabled") of a managed IP.`,
[]string{
"namespace",
"name",
"ip",
"provider",
"dns",
"state",
}, nil,
)
unfulfilledIPsDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricNamespace, metricSubsystem, "unfulfilled_ips"),
`The number of desired IP addresses which have not yet been acquired.`,
[]string{
"namespace",
"name",
"provider",
"dns",
}, nil,
)
)
// Describe implements prometheus.Collector.
func (c *Controller) Describe(ch chan<- *prometheus.Desc) {
ch <- nodeStatusDesc
ch <- ipAssignmentErrorsDesc
ch <- ipAssignmentsDesc
ch <- ipNodeDesc
ch <- ipStateDesc
ch <- unfulfilledIPsDesc
}
// Collect implements prometheus.Collector.
func (c *Controller) Collect(ch chan<- prometheus.Metric) {
c.poolLock.Lock()
defer c.poolLock.Unlock()
for _, p := range c.pools {
p.collect(ch)
}
}
func (p *floatingIPPool) collect(ch chan<- prometheus.Metric) {
p.ipController.lock.Lock()
defer p.ipController.lock.Unlock()
var dnsName string
if p.ipController.dns != nil && p.ipController.dnsProvider != nil {
dnsName = p.ipController.dnsProvider.RecordNameAndZoneToFQDN(
p.ipController.dns.Zone, p.ipController.dns.RecordName)
}
var assignedNodes int
for ip, s := range p.ipController.ipToStatus {
labels := []string{
p.namespace,
p.name,
ip,
p.ipController.provider.GetProviderName(),
dnsName,
}
ch <- prometheus.MustNewConstMetric(
ipAssignmentErrorsDesc,
prometheus.CounterValue,
float64(s.assignmentErrors),
labels...,
)
ch <- prometheus.MustNewConstMetric(
ipAssignmentsDesc,
prometheus.CounterValue,
float64(s.assignments),
labels...,
)
if s.nodeProviderID != "" {
assignedNodes++
nodeLabels := append(
append([]string{}, labels...),
s.nodeProviderID,
p.ipController.providerIDToNodeName[s.nodeProviderID],
)
ch <- prometheus.MustNewConstMetric(
ipNodeDesc,
prometheus.GaugeValue,
float64(1),
nodeLabels...,
)
}
ch <- prometheus.MustNewConstMetric(
ipStateDesc,
prometheus.GaugeValue,
float64(1),
append(labels, string(s.state))...,
)
}
unfulfilledIPs := math.Max(0, float64(p.ipController.desiredIPs-len(p.ipController.ips)))
ch <- prometheus.MustNewConstMetric(
unfulfilledIPsDesc,
prometheus.GaugeValue,
unfulfilledIPs,
p.namespace,
p.name,
p.ipController.provider.GetProviderName(),
dnsName,
)
ch <- prometheus.MustNewConstMetric(
nodeStatusDesc,
prometheus.GaugeValue,
float64(p.ipController.assignableNodes.Len()),
p.namespace,
p.name,
p.ipController.provider.GetProviderName(),
dnsName,
"available",
)
ch <- prometheus.MustNewConstMetric(
nodeStatusDesc,
prometheus.GaugeValue,
float64(assignedNodes),
p.namespace,
p.name,
p.ipController.provider.GetProviderName(),
dnsName,
"assigned",
)
}