/
metrics.go
42 lines (38 loc) · 1.83 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
package status
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"github.com/grafana/tempo-operator/apis/tempo/v1alpha1"
)
var (
metricTempoStackStatusCondition = promauto.With(metrics.Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "tempostack",
Name: "status_condition",
Help: "The status condition of a TempoStack instance.",
}, []string{"stack_namespace", "stack_name", "condition"})
metricTempoMonolithicStatusCondition = promauto.With(metrics.Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "tempomonolithic",
Name: "status_condition",
Help: "The status condition of a TempoMonolithic instance.",
}, []string{"stack_namespace", "stack_name", "condition"})
)
func updateMetrics(metric *prometheus.GaugeVec, conditions []metav1.Condition, namespace string, name string) {
// Update all status condition metrics.
// In some cases not all status conditions are present in the status.Conditions list, for example:
// A TempoStack CR gets created with an invalid storage secret (creating an ConfigurationError status condition).
// Later this CR is deleted, a storage secret is created and a new TempoStack instance is created.
// Then this TempoStack instance doesn't have the ConfigurationError condition in the status.Conditions list.
activeConditions := map[string]float64{}
for _, cond := range conditions {
if cond.Status == metav1.ConditionTrue {
activeConditions[cond.Type] = 1
}
}
for _, cond := range v1alpha1.AllStatusConditions {
condStr := string(cond)
isActive := activeConditions[condStr] // isActive will be 0 if the condition is not found in the map
metric.WithLabelValues(namespace, name, condStr).Set(isActive)
}
}