From 89644c5a79b3744463092afcbacfdb798e1058b1 Mon Sep 17 00:00:00 2001 From: Mike Tonks Date: Thu, 7 Dec 2023 18:09:21 +0000 Subject: [PATCH] chore: Add name label to chaos_experiments metric We need to be careful about cardinality, but since we won't have hundreds of chaos experiments it should be ok to add name, given that is really useful for dashboards and alerting Removed the expCache to simplify code --- pkg/metrics/chaos-controller-manager.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/pkg/metrics/chaos-controller-manager.go b/pkg/metrics/chaos-controller-manager.go index 33d6873487..79b3a1ff5a 100644 --- a/pkg/metrics/chaos-controller-manager.go +++ b/pkg/metrics/chaos-controller-manager.go @@ -59,7 +59,7 @@ func NewChaosControllerManagerMetricsCollector(manager ctrl.Manager, registerer chaosExperiments: prometheus.NewGaugeVec(prometheus.GaugeOpts{ Name: "chaos_controller_manager_chaos_experiments", Help: "Total number of chaos experiments and their phases", - }, []string{"namespace", "kind", "phase"}), + }, []string{"namespace", "kind", "phase", "name"}), SidecarTemplates: prometheus.NewGauge(prometheus.GaugeOpts{ Name: "chaos_mesh_templates", Help: "Total number of injection templates", @@ -153,7 +153,6 @@ func (collector *ChaosControllerManagerMetricsCollector) collectChaosExperiments collector.chaosExperiments.Reset() for kind, obj := range v1alpha1.AllKinds() { - expCache := map[string]map[string]int{} chaosList := obj.SpawnList() if err := collector.store.List(context.TODO(), chaosList); err != nil { collector.logger.Error(err, "failed to list chaos", "kind", kind) @@ -162,18 +161,11 @@ func (collector *ChaosControllerManagerMetricsCollector) collectChaosExperiments items := chaosList.GetItems() for _, item := range items { - if _, ok := expCache[item.GetNamespace()]; !ok { - // There is only 4 supported phases - expCache[item.GetNamespace()] = make(map[string]int, 4) - } innerObject := reflect.ValueOf(item).Interface().(v1alpha1.InnerObject) - expCache[item.GetNamespace()][string(status.GetChaosStatus(innerObject))]++ - } - - for ns, v := range expCache { - for phase, count := range v { - collector.chaosExperiments.WithLabelValues(ns, kind, phase).Set(float64(count)) - } + ns := item.GetNamespace() + name := item.GetName() + phase := string(status.GetChaosStatus(innerObject)) + collector.chaosExperiments.WithLabelValues(ns, kind, phase, name).Inc() } } }