From 96b33f62004630ab974e4ec7902a152aefe76bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 29 Apr 2022 19:33:08 +0200 Subject: [PATCH] refactor: metrics package logger (#3734) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/kyverno/main.go | 2 +- pkg/metrics/admissionrequests/admissionRequests.go | 4 ++-- .../admissionReviewDuration.go | 4 ++-- pkg/metrics/log.go | 12 ++++++++++++ pkg/metrics/metrics.go | 13 +++++-------- pkg/metrics/policychanges/policyChanges.go | 4 ++-- .../policyExecutionDuration.go | 4 ++-- pkg/metrics/policyresults/policyResults.go | 4 ++-- pkg/metrics/policyruleinfo/policyRuleInfo.go | 4 ++-- 9 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 pkg/metrics/log.go diff --git a/cmd/kyverno/main.go b/cmd/kyverno/main.go index dfe75dcfb2..d89b64bd5d 100755 --- a/cmd/kyverno/main.go +++ b/cmd/kyverno/main.go @@ -260,7 +260,7 @@ func main() { } if !disableMetricsExport { - promConfig, err = metrics.NewPromConfig(metricsConfigData, log.Log.WithName("MetricsConfig")) + promConfig, err = metrics.NewPromConfig(metricsConfigData) if err != nil { setupLog.Error(err, "failed to setup Prometheus metric configuration") os.Exit(1) diff --git a/pkg/metrics/admissionrequests/admissionRequests.go b/pkg/metrics/admissionrequests/admissionRequests.go index e31f808183..d9b78aa536 100644 --- a/pkg/metrics/admissionrequests/admissionRequests.go +++ b/pkg/metrics/admissionrequests/admissionRequests.go @@ -16,11 +16,11 @@ func registerAdmissionRequestsMetric( ) error { includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (resourceNamespace != "" && resourceNamespace != "-") && utils.ContainsString(excludeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_admission_requests_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_admission_requests_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) return nil } if (resourceNamespace != "" && resourceNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_admission_requests_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_admission_requests_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) return nil } pc.Metrics.AdmissionRequests.With(prom.Labels{ diff --git a/pkg/metrics/admissionreviewduration/admissionReviewDuration.go b/pkg/metrics/admissionreviewduration/admissionReviewDuration.go index 55d2e70a69..b40eea307f 100644 --- a/pkg/metrics/admissionreviewduration/admissionReviewDuration.go +++ b/pkg/metrics/admissionreviewduration/admissionReviewDuration.go @@ -17,11 +17,11 @@ func registerAdmissionReviewDurationMetric( ) error { includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (resourceNamespace != "" && resourceNamespace != "-") && utils.ContainsString(excludeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_admission_review_duration_seconds metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_admission_review_duration_seconds metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) return nil } if (resourceNamespace != "" && resourceNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_admission_review_duration_seconds metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_admission_review_duration_seconds metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) return nil } pc.Metrics.AdmissionReviewDuration.With(prom.Labels{ diff --git a/pkg/metrics/log.go b/pkg/metrics/log.go new file mode 100644 index 0000000000..0c9fe0c3bc --- /dev/null +++ b/pkg/metrics/log.go @@ -0,0 +1,12 @@ +package metrics + +import ( + "github.com/go-logr/logr" + "sigs.k8s.io/controller-runtime/pkg/log" +) + +var logger = log.Log.WithName("metrics") + +func Logger() logr.Logger { + return logger +} diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index feb23a9285..f09706602c 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -3,7 +3,6 @@ package metrics import ( "fmt" - "github.com/go-logr/logr" "github.com/kyverno/kyverno/pkg/config" prom "github.com/prometheus/client_golang/prometheus" "github.com/robfig/cron/v3" @@ -13,7 +12,6 @@ type PromConfig struct { MetricsRegistry *prom.Registry Metrics *PromMetrics Config *config.MetricsConfigData - Log logr.Logger cron *cron.Cron } @@ -26,12 +24,11 @@ type PromMetrics struct { AdmissionRequests *prom.CounterVec } -func NewPromConfig(metricsConfigData *config.MetricsConfigData, log logr.Logger) (*PromConfig, error) { +func NewPromConfig(metricsConfigData *config.MetricsConfigData) (*PromConfig, error) { pc := new(PromConfig) pc.Config = metricsConfigData pc.cron = cron.New() pc.MetricsRegistry = prom.NewRegistry() - pc.Log = log policyResultsLabels := []string{ "policy_validation_mode", "policy_type", "policy_background_mode", "policy_name", "policy_namespace", @@ -122,10 +119,10 @@ func NewPromConfig(metricsConfigData *config.MetricsConfigData, log logr.Logger) // configuring metrics periodic refresh if pc.Config.GetMetricsRefreshInterval() != 0 { if len(pc.cron.Entries()) > 0 { - pc.Log.Info("Skipping the configuration of metrics refresh. Already found cron expiration to be set.") + logger.Info("Skipping the configuration of metrics refresh. Already found cron expiration to be set.") } else { _, err := pc.cron.AddFunc(fmt.Sprintf("@every %s", pc.Config.GetMetricsRefreshInterval()), func() { - pc.Log.Info("Resetting the metrics as per their periodic refresh") + logger.Info("Resetting the metrics as per their periodic refresh") pc.Metrics.PolicyResults.Reset() pc.Metrics.PolicyRuleInfo.Reset() pc.Metrics.PolicyChanges.Reset() @@ -136,11 +133,11 @@ func NewPromConfig(metricsConfigData *config.MetricsConfigData, log logr.Logger) if err != nil { return nil, err } - log.Info(fmt.Sprintf("Configuring metrics refresh at a periodic rate of %s", pc.Config.GetMetricsRefreshInterval())) + logger.Info(fmt.Sprintf("Configuring metrics refresh at a periodic rate of %s", pc.Config.GetMetricsRefreshInterval())) pc.cron.Start() } } else { - pc.Log.Info("Skipping the configuration of metrics refresh as 'metricsRefreshInterval' wasn't specified in values.yaml at the time of installing kyverno") + logger.Info("Skipping the configuration of metrics refresh as 'metricsRefreshInterval' wasn't specified in values.yaml at the time of installing kyverno") } return pc, nil } diff --git a/pkg/metrics/policychanges/policyChanges.go b/pkg/metrics/policychanges/policyChanges.go index c271912112..4a1bc1365e 100644 --- a/pkg/metrics/policychanges/policyChanges.go +++ b/pkg/metrics/policychanges/policyChanges.go @@ -22,11 +22,11 @@ func registerPolicyChangesMetric( } includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (policyNamespace != "" && policyNamespace != "-") && utils.ContainsString(excludeNamespaces, policyNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_changes_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", policyNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_changes_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", policyNamespace, excludeNamespaces)) return nil } if (policyNamespace != "" && policyNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, policyNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_changes_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", policyNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_changes_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", policyNamespace, includeNamespaces)) return nil } pc.Metrics.PolicyChanges.With(prom.Labels{ diff --git a/pkg/metrics/policyexecutionduration/policyExecutionDuration.go b/pkg/metrics/policyexecutionduration/policyExecutionDuration.go index 961eab3d25..04020fa979 100644 --- a/pkg/metrics/policyexecutionduration/policyExecutionDuration.go +++ b/pkg/metrics/policyexecutionduration/policyExecutionDuration.go @@ -33,11 +33,11 @@ func registerPolicyExecutionDurationMetric( } includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (resourceNamespace != "" && resourceNamespace != "-") && utils.ContainsString(excludeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_execution_duration_seconds metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_execution_duration_seconds metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) return nil } if (resourceNamespace != "" && resourceNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_execution_duration_seconds metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_execution_duration_seconds metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) return nil } pc.Metrics.PolicyExecutionDuration.With(prom.Labels{ diff --git a/pkg/metrics/policyresults/policyResults.go b/pkg/metrics/policyresults/policyResults.go index b1358f1fff..42bb592108 100644 --- a/pkg/metrics/policyresults/policyResults.go +++ b/pkg/metrics/policyresults/policyResults.go @@ -28,11 +28,11 @@ func registerPolicyResultsMetric( } includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (resourceNamespace != "" && resourceNamespace != "-") && utils.ContainsString(excludeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_results_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_results_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", resourceNamespace, excludeNamespaces)) return nil } if (resourceNamespace != "" && resourceNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, resourceNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_results_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_results_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", resourceNamespace, includeNamespaces)) return nil } pc.Metrics.PolicyResults.With(prom.Labels{ diff --git a/pkg/metrics/policyruleinfo/policyRuleInfo.go b/pkg/metrics/policyruleinfo/policyRuleInfo.go index b45527f2e3..8939527ab5 100644 --- a/pkg/metrics/policyruleinfo/policyRuleInfo.go +++ b/pkg/metrics/policyruleinfo/policyRuleInfo.go @@ -31,11 +31,11 @@ func registerPolicyRuleInfoMetric( } includeNamespaces, excludeNamespaces := pc.Config.GetIncludeNamespaces(), pc.Config.GetExcludeNamespaces() if (policyNamespace != "" && policyNamespace != "-") && utils.ContainsString(excludeNamespaces, policyNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", policyNamespace, excludeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml", policyNamespace, excludeNamespaces)) return nil } if (policyNamespace != "" && policyNamespace != "-") && len(includeNamespaces) > 0 && !utils.ContainsString(includeNamespaces, policyNamespace) { - pc.Log.Info(fmt.Sprintf("Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", policyNamespace, includeNamespaces)) + metrics.Logger().Info(fmt.Sprintf("Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml", policyNamespace, includeNamespaces)) return nil } if policyType == metrics.Cluster {