Skip to content

Commit

Permalink
Migrate metrics to metric.go
Browse files Browse the repository at this point in the history
Signed-off-by: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com>
  • Loading branch information
dhaiducek authored and openshift-merge-robot committed Dec 5, 2022
1 parent ca155f5 commit ce005fc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
54 changes: 0 additions & 54 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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).
Expand Down
60 changes: 60 additions & 0 deletions controllers/metric.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ce005fc

Please sign in to comment.