/
metrics.go
87 lines (67 loc) · 2.24 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
package monitoring
import (
"strings"
api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
corev1 "k8s.io/api/core/v1"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
type PodStatus string
const (
PodStatusInitializing PodStatus = "Initializing"
PodStatusReady PodStatus = "Ready"
PodStatusPending PodStatus = "Pending"
PodStatusError PodStatus = "Error"
PodStatusDecommissioning PodStatus = "Decommissioning"
)
func getPodStatus(pod *corev1.Pod) PodStatus {
status := PodStatusReady
if pod.Labels[api.CassNodeState] == "Decommissioning" {
return PodStatusDecommissioning
}
switch pod.Status.Phase {
case corev1.PodPending:
return PodStatusPending
case corev1.PodFailed:
return PodStatusError
case corev1.PodRunning:
default:
}
allContainersReady := true
for _, s := range pod.Status.ContainerStatuses {
if !s.Ready {
allContainersReady = false
break
}
}
if !allContainersReady {
return PodStatusInitializing
}
return status
}
var (
PodStatusVec *prometheus.GaugeVec
)
func init() {
podVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cass_operator",
Subsystem: "datacenter_pods",
Name: "status",
Help: "Cassandra pod statuses",
}, []string{"namespace", "cluster", "datacenter", "rack", "pod", "status"})
metrics.Registry.MustRegister(podVec)
PodStatusVec = podVec
}
func UpdatePodStatusMetric(pod *corev1.Pod) {
currentState := getPodStatus(pod)
// Delete all status metrics for this pod
RemovePodStatusMetric(pod)
// Add just the current one
PodStatusVec.WithLabelValues(pod.Namespace, pod.Labels[api.ClusterLabel], pod.Labels[api.DatacenterLabel], pod.Labels[api.RackLabel], pod.Name, strings.ToLower(string(currentState))).Set(1)
}
func RemovePodStatusMetric(pod *corev1.Pod) {
PodStatusVec.DeletePartialMatch(prometheus.Labels{"namespace": pod.Namespace, "cluster": pod.Labels[api.ClusterLabel], "datacenter": pod.Labels[api.DatacenterLabel], "rack": pod.Labels[api.RackLabel], "pod": pod.Name})
}
func RemoveDatacenterPods(namespace, cluster, datacenter string) {
PodStatusVec.DeletePartialMatch(prometheus.Labels{"namespace": namespace, "cluster": cluster, "datacenter": datacenter})
}