forked from infinityworks/prometheus-rancher-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
152 lines (125 loc) · 4.9 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
package main
import (
"strings"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
// addMetrics - Add's all of the GuageVecs to the `guageVecs` map, returns the map.
func addMetrics() map[string]*prometheus.GaugeVec {
gaugeVecs := make(map[string]*prometheus.GaugeVec)
// Stack Metrics
gaugeVecs["stacksHealth"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: "stack_health_status",
Help: "HealthState of defined stack as reported by Rancher",
}, []string{"name", "health_state"})
gaugeVecs["stacksState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: "stack_state",
Help: "State of defined stack as reported by Rancher",
}, []string{"name", "state"})
// Service Metrics
gaugeVecs["servicesScale"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: "service_scale",
Help: "scale of defined service as reported by Rancher",
}, []string{"name", "stack_name"})
gaugeVecs["servicesHealth"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: "service_health_status",
Help: "HealthState of the service, as reported by the Rancher API. Either (1) or (0)",
}, []string{"name", "stack_name", "health_state"})
gaugeVecs["servicesState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: "service_state",
Help: "State of the service, as reported by the Rancher API",
}, []string{"name", "stack_name", "state"})
// Host Metrics
gaugeVecs["hostsState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: ("host_state"),
Help: "State of defined host as reported by the Rancher API",
}, []string{"name", "state"})
gaugeVecs["hostAgentsState"] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "rancher",
Name: ("host_agent_state"),
Help: "State of defined host agent as reported by the Rancher API",
}, []string{"name", "state"})
return gaugeVecs
}
// checkMetric - Checks the base type stored in the API is correct, this ensures we are setting the right metric for the right endpoint.
func checkMetric(endpoint string, baseType string) bool {
e := strings.TrimSuffix(endpoint, "s")
// Backwards compatibility fix, the API in V1 wrong, this is to cover v1 usage.
if baseType == "environment" && e == "stack" {
return true
} else if e == "service" && (baseType == "externalService" || baseType == "loadBalancerService") {
return true
} else if e != baseType {
log.Errorf("API MisMatch, expected %s metric, got %s metric", e, baseType)
return false
}
return true
}
// setServiceMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setServiceMetrics(name string, stack string, state string, health string, scale int) error {
e.gaugeVecs["servicesScale"].With(prometheus.Labels{"name": name, "stack_name": stack}).Set(float64(scale))
for _, y := range healthStates {
if health == y {
e.gaugeVecs["servicesHealth"].With(prometheus.Labels{"name": name, "stack_name": stack, "health_state": y}).Set(1)
} else {
e.gaugeVecs["servicesHealth"].With(prometheus.Labels{"name": name, "stack_name": stack, "health_state": y}).Set(0)
}
}
for _, y := range serviceStates {
if state == y {
e.gaugeVecs["servicesState"].With(prometheus.Labels{"name": name, "stack_name": stack, "state": y}).Set(1)
} else {
e.gaugeVecs["servicesState"].With(prometheus.Labels{"name": name, "stack_name": stack, "state": y}).Set(0)
}
}
return nil
}
// setStackMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setStackMetrics(name string, state string, health string) error {
for _, y := range healthStates {
if health == y {
e.gaugeVecs["stacksHealth"].With(prometheus.Labels{"name": name, "health_state": y}).Set(1)
} else {
e.gaugeVecs["stacksHealth"].With(prometheus.Labels{"name": name, "health_state": y}).Set(0)
}
}
for _, y := range stackStates {
if state == y {
e.gaugeVecs["stacksState"].With(prometheus.Labels{"name": name, "state": y}).Set(1)
} else {
e.gaugeVecs["stacksState"].With(prometheus.Labels{"name": name, "state": y}).Set(0)
}
}
return nil
}
// setHostMetrics - Logic to set the state of a system as a gauge metric
func (e *Exporter) setHostMetrics(name string, state, agentState string) error {
for _, y := range hostStates {
if state == y {
e.gaugeVecs["hostsState"].With(prometheus.Labels{"name": name, "state": y}).Set(1)
} else {
e.gaugeVecs["hostsState"].With(prometheus.Labels{"name": name, "state": y}).Set(0)
}
}
for _, y := range agentStates {
if agentState == y {
e.gaugeVecs["hostAgentsState"].With(prometheus.Labels{"name": name, "state": y}).Set(1)
} else {
e.gaugeVecs["hostAgentsState"].With(prometheus.Labels{"name": name, "state": y}).Set(0)
}
}
return nil
}