From 7de264273facf6cdeee0a4258380a38d7121fc1c Mon Sep 17 00:00:00 2001 From: Dale Haiducek Date: Fri, 4 Nov 2022 18:01:29 -0400 Subject: [PATCH] Migrate metrics to `metric.go` Signed-off-by: Dale Haiducek --- controllers/configurationpolicy_controller.go | 54 ----------------- controllers/metric.go | 60 +++++++++++++++++++ 2 files changed, 60 insertions(+), 54 deletions(-) create mode 100644 controllers/metric.go diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index ee79bd91..b2796b9c 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -38,7 +38,6 @@ import ( "k8s.io/kubectl/pkg/validation" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/reconcile" policyv1 "open-cluster-management.io/config-policy-controller/api/v1" @@ -68,59 +67,6 @@ var ( reasonCleanupError = "Error cleaning up child objects" ) -var evalLoopHistogram = prometheus.NewHistogram( - prometheus.HistogramOpts{ - Name: "config_policies_evaluation_duration_seconds", - Help: "The seconds that it takes to evaluate all configuration policies on the cluster", - Buckets: []float64{1, 3, 9, 10.5, 15, 30, 60, 90, 120, 180, 300, 450, 600}, - }, -) - -var policyEvalSecondsCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "config_policy_evaluation_seconds_total", - Help: "The total seconds taken while evaluating the configuration policy. Use this alongside " + - "config_policy_evaluation_total.", - }, - []string{"name"}, -) - -var policyEvalCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "config_policy_evaluation_total", - Help: "The total number of evaluations of the configuration policy. Use this alongside " + - "config_policy_evaluation_seconds_total.", - }, - []string{"name"}, -) - -var compareObjSecondsCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "compare_objects_seconds_total", - Help: "The total seconds taken while comparing policy objects. Use this alongside " + - "compare_objects_evaluation_total.", - }, - []string{"config_policy_name", "namespace", "object"}, -) - -var compareObjEvalCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "compare_objects_evaluation_total", - Help: "The total number of times the comparison algorithm is run on an object. " + - "Use this alongside compare_objects_seconds_total.", - }, - []string{"config_policy_name", "namespace", "object"}, -) - -func init() { - // Register custom metrics with the global Prometheus registry - metrics.Registry.MustRegister(evalLoopHistogram) - metrics.Registry.MustRegister(policyEvalSecondsCounter) - metrics.Registry.MustRegister(policyEvalCounter) - metrics.Registry.MustRegister(compareObjSecondsCounter) - metrics.Registry.MustRegister(compareObjEvalCounter) -} - // SetupWithManager sets up the controller with the Manager. func (r *ConfigurationPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). diff --git a/controllers/metric.go b/controllers/metric.go new file mode 100644 index 00000000..fdcebb38 --- /dev/null +++ b/controllers/metric.go @@ -0,0 +1,60 @@ +package controllers + +import ( + "fmt" + + "github.com/prometheus/client_golang/prometheus" + "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +var ( + evalLoopHistogram = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Name: "config_policies_evaluation_duration_seconds", + Help: "The seconds that it takes to evaluate all configuration policies on the cluster", + Buckets: []float64{1, 3, 9, 10.5, 15, 30, 60, 90, 120, 180, 300, 450, 600}, + }, + ) + policyEvalSecondsCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "config_policy_evaluation_seconds_total", + Help: "The total seconds taken while evaluating the configuration policy. Use this alongside " + + "config_policy_evaluation_total.", + }, + []string{"name"}, + ) + policyEvalCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "config_policy_evaluation_total", + Help: "The total number of evaluations of the configuration policy. Use this alongside " + + "config_policy_evaluation_seconds_total.", + }, + []string{"name"}, + ) + compareObjSecondsCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "compare_objects_seconds_total", + Help: "The total seconds taken while comparing policy objects. Use this alongside " + + "compare_objects_evaluation_total.", + }, + []string{"config_policy_name", "namespace", "object"}, + ) + compareObjEvalCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "compare_objects_evaluation_total", + Help: "The total number of times the comparison algorithm is run on an object. " + + "Use this alongside compare_objects_seconds_total.", + }, + []string{"config_policy_name", "namespace", "object"}, + ) +) + +func init() { + // Register custom metrics with the global Prometheus registry + metrics.Registry.MustRegister(evalLoopHistogram) + metrics.Registry.MustRegister(policyEvalSecondsCounter) + metrics.Registry.MustRegister(policyEvalCounter) + metrics.Registry.MustRegister(compareObjSecondsCounter) + metrics.Registry.MustRegister(compareObjEvalCounter) + metrics.Registry.MustRegister(policyRelatedObjectGauge) +}