From 873cd667c9ee063dd2255a4d2a1c3915cf941d23 Mon Sep 17 00:00:00 2001 From: Chunxi Luo Date: Wed, 23 Nov 2022 16:41:56 -0500 Subject: [PATCH] Add a metric to measure how long resolving policy templates take Signed-off-by: Chunxi Luo --- controllers/configurationpolicy_controller.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index 34d3af0b..0ae15566 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -94,6 +94,24 @@ var policyEvalCounter = prometheus.NewCounterVec( []string{"name"}, ) +var plcTempsProcessSecondsCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "config_policy_templates_process_seconds_total", + Help: "The total seconds taken while processing the configuration policy templates. Use this alongside " + + "config_policy_templates_process_total.", + }, + []string{"name"}, +) + +var plcTempsProcessCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "config_policy_templates_process_total", + Help: "The total number of processes of the configuration policy templates. Use this alongside " + + "config_policy_templates_process_seconds_total.", + }, + []string{"name"}, +) + var compareObjSecondsCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "compare_objects_seconds_total", @@ -117,6 +135,8 @@ func init() { metrics.Registry.MustRegister(evalLoopHistogram) metrics.Registry.MustRegister(policyEvalSecondsCounter) metrics.Registry.MustRegister(policyEvalCounter) + metrics.Registry.MustRegister(plcTempsProcessSecondsCounter) + metrics.Registry.MustRegister(plcTempsProcessCounter) metrics.Registry.MustRegister(compareObjSecondsCounter) metrics.Registry.MustRegister(compareObjEvalCounter) } @@ -179,6 +199,8 @@ func (r *ConfigurationPolicyReconciler) Reconcile(ctx context.Context, request c // If the metric was not deleted, that means the policy was never evaluated so it can be ignored. _ = policyEvalSecondsCounter.DeleteLabelValues(request.Name) _ = policyEvalCounter.DeleteLabelValues(request.Name) + _ = plcTempsProcessSecondsCounter.DeleteLabelValues(request.Name) + _ = plcTempsProcessCounter.DeleteLabelValues(request.Name) _ = compareObjEvalCounter.DeletePartialMatch(prometheus.Labels{"config_policy_name": request.Name}) _ = compareObjSecondsCounter.DeletePartialMatch(prometheus.Labels{"config_policy_name": request.Name}) } @@ -760,6 +782,8 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi log.V(2).Info("Processing the object templates", "count", len(plc.Spec.ObjectTemplates)) if !disableTemplates { + startTime := time.Now().UTC() + for _, objectT := range plc.Spec.ObjectTemplates { // first check to make sure there are no hub-templates with delimiter - {{hub // if one exists, it means the template resolution on the hub did not succeed. @@ -853,6 +877,11 @@ func (r *ConfigurationPolicyReconciler) handleObjectTemplates(plc policyv1.Confi objectT.ObjectDefinition.Raw = resolvedTemplate.ResolvedJSON } } + + endTime := time.Now().UTC() + durationSeconds := float64(endTime.Sub(startTime)) / float64(time.Second) + plcTempsProcessSecondsCounter.WithLabelValues(plc.GetName()).Add(durationSeconds) + plcTempsProcessCounter.WithLabelValues(plc.GetName()).Inc() } // Parse and fetch details from each object in each objectTemplate, and gather namespaces if required