From 6abe9731f229bf7cdd4493c5e5b8b1aea966635f Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Mon, 4 Mar 2024 12:28:01 +0100 Subject: [PATCH] remove summary metric Signed-off-by: Frank Jogeleit --- pkg/listener/metrics.go | 16 +- pkg/listener/metrics/cluster_policy_report.go | 50 ----- .../metrics/cluster_policy_report_test.go | 173 ------------------ pkg/listener/metrics/policy_report.go | 50 ----- pkg/listener/metrics/policy_report_test.go | 143 --------------- 5 files changed, 2 insertions(+), 430 deletions(-) delete mode 100644 pkg/listener/metrics/cluster_policy_report.go delete mode 100644 pkg/listener/metrics/cluster_policy_report_test.go delete mode 100644 pkg/listener/metrics/policy_report.go delete mode 100644 pkg/listener/metrics/policy_report_test.go diff --git a/pkg/listener/metrics.go b/pkg/listener/metrics.go index fa870ab9..6d4ac7dd 100644 --- a/pkg/listener/metrics.go +++ b/pkg/listener/metrics.go @@ -96,20 +96,8 @@ func ResultListeners( } } - prCallback := metrics.CreateDetailedResultMetricListener(filter, metrics.RegisterDetailedResultGauge(ResultGaugeName)) - pCallback := metrics.CreatePolicyReportMetricsListener(reportFilter) - - crCallback := metrics.CreateDetailedClusterResultMetricListener(filter, metrics.RegisterDetailedClusterResultGauge(ClusterResultGaugeName)) - cCallback := metrics.CreateClusterPolicyReportMetricsListener(reportFilter) - return []report.PolicyReportListener{ - func(event report.LifecycleEvent) { - pCallback(event) - prCallback(event) - }, - func(event report.LifecycleEvent) { - cCallback(event) - crCallback(event) - }, + metrics.CreateDetailedResultMetricListener(filter, metrics.RegisterDetailedResultGauge(ResultGaugeName)), + metrics.CreateDetailedClusterResultMetricListener(filter, metrics.RegisterDetailedClusterResultGauge(ClusterResultGaugeName)), } } diff --git a/pkg/listener/metrics/cluster_policy_report.go b/pkg/listener/metrics/cluster_policy_report.go deleted file mode 100644 index e80daf9c..00000000 --- a/pkg/listener/metrics/cluster_policy_report.go +++ /dev/null @@ -1,50 +0,0 @@ -package metrics - -import ( - "strings" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - - "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" - "github.com/kyverno/policy-reporter/pkg/report" -) - -var clusterPolicyGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Name: "cluster_policy_report_summary", - Help: "Summary of all ClusterPolicyReports", -}, []string{"name", "status"}) - -func CreateClusterPolicyReportMetricsListener(filter *report.ReportFilter) report.PolicyReportListener { - prometheus.Register(clusterPolicyGauge) - - var newReport v1alpha2.ReportInterface - - return func(event report.LifecycleEvent) { - newReport = event.PolicyReport - if !filter.Validate(newReport) { - return - } - - switch event.Type { - case report.Added: - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusSkip)).Set(float64(newReport.GetSummary().Skip)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusPass)).Set(float64(newReport.GetSummary().Pass)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusWarn)).Set(float64(newReport.GetSummary().Warn)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusFail)).Set(float64(newReport.GetSummary().Fail)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusError)).Set(float64(newReport.GetSummary().Error)) - case report.Updated: - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusSkip)).Set(float64(newReport.GetSummary().Skip)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusPass)).Set(float64(newReport.GetSummary().Pass)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusWarn)).Set(float64(newReport.GetSummary().Warn)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusFail)).Set(float64(newReport.GetSummary().Fail)) - clusterPolicyGauge.WithLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusError)).Set(float64(newReport.GetSummary().Error)) - case report.Deleted: - clusterPolicyGauge.DeleteLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusSkip)) - clusterPolicyGauge.DeleteLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusPass)) - clusterPolicyGauge.DeleteLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusWarn)) - clusterPolicyGauge.DeleteLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusFail)) - clusterPolicyGauge.DeleteLabelValues(newReport.GetName(), strings.Title(v1alpha2.StatusError)) - } - } -} diff --git a/pkg/listener/metrics/cluster_policy_report_test.go b/pkg/listener/metrics/cluster_policy_report_test.go deleted file mode 100644 index c10e29e1..00000000 --- a/pkg/listener/metrics/cluster_policy_report_test.go +++ /dev/null @@ -1,173 +0,0 @@ -package metrics_test - -import ( - "fmt" - "testing" - - "github.com/prometheus/client_golang/prometheus" - ioprometheusclient "github.com/prometheus/client_model/go" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" - "github.com/kyverno/policy-reporter/pkg/listener/metrics" - "github.com/kyverno/policy-reporter/pkg/report" - "github.com/kyverno/policy-reporter/pkg/validate" -) - -func Test_ClusterPolicyReportMetricGeneration(t *testing.T) { - report1 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "cpolr-test", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 1, Fail: 1}, - } - - report2 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "cpolr-test", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 0, Fail: 1}, - } - - report3 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "cpolr-test", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 0, Fail: 1}, - Results: []v1alpha2.PolicyReportResult{{Source: "Kube Bench"}}, - } - - filter := metrics.NewReportFilter(validate.RuleSets{}, validate.RuleSets{Exclude: []string{"Kube Bench"}}) - handler := metrics.CreateClusterPolicyReportMetricsListener(filter) - - t.Run("Added Metric", func(t *testing.T) { - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "cluster_policy_report_summary") - if summary == nil { - t.Fatalf("Metric not found: cluster_policy_report_summary") - } - - metrics := summary.GetMetric() - - if err = testClusterSummaryMetricLabels(metrics[0], report1, "Error", 0); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[1], report1, "Fail", 1); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[2], report1, "Pass", 1); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[3], report1, "Skip", 0); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[4], report1, "Warn", 0); err != nil { - t.Error(err) - } - }) - - t.Run("Modified Metric", func(t *testing.T) { - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - handler(report.LifecycleEvent{Type: report.Updated, PolicyReport: report2}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "cluster_policy_report_summary") - if summary == nil { - t.Fatalf("Metric not found: cluster_policy_report_summary") - } - - metrics := summary.GetMetric() - - if err = testClusterSummaryMetricLabels(metrics[0], report2, "Error", 0); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[1], report2, "Fail", 1); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[2], report2, "Pass", 0); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[3], report2, "Skip", 0); err != nil { - t.Error(err) - } - if err = testClusterSummaryMetricLabels(metrics[4], report2, "Warn", 0); err != nil { - t.Error(err) - } - }) - - t.Run("Deleted Metric", func(t *testing.T) { - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - handler(report.LifecycleEvent{Type: report.Updated, PolicyReport: report2}) - handler(report.LifecycleEvent{Type: report.Deleted, PolicyReport: report2}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "cluster_policy_report_summary") - if summary != nil { - t.Error("cluster_policy_report_summary should no longer exist", *summary.Name) - } - - results := metricFam[0] - - if *results.Name == "cluster_policy_report_result" { - t.Error("cluster_policy_report_result should no longer exist", *results.Name) - } - }) - - t.Run("Filtered Report", func(t *testing.T) { - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report3}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "cluster_policy_report_summary") - if summary != nil { - t.Error("cluster_policy_report_summary should not be created", *summary.Name) - } - }) -} - -func testClusterSummaryMetricLabels( - metric *ioprometheusclient.Metric, - preport v1alpha2.ReportInterface, - status string, - gauge float64, -) error { - if name := *metric.Label[0].Name; name != "name" { - return fmt.Errorf("unexpected Name Label: %s", name) - } - if value := *metric.Label[0].Value; value != preport.GetName() { - return fmt.Errorf("unexpected Name Label Value: %s", value) - } - - if name := *metric.Label[1].Name; name != "status" { - return fmt.Errorf("unexpected Name Label: %s", name) - } - if value := *metric.Label[1].Value; value != status { - return fmt.Errorf("unexpected Status Label Value: %s", value) - } - - if value := metric.Gauge.GetValue(); value != gauge { - return fmt.Errorf("unexpected Metric Value: %v", value) - } - - return nil -} diff --git a/pkg/listener/metrics/policy_report.go b/pkg/listener/metrics/policy_report.go deleted file mode 100644 index 7960ab45..00000000 --- a/pkg/listener/metrics/policy_report.go +++ /dev/null @@ -1,50 +0,0 @@ -package metrics - -import ( - "strings" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - - "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" - "github.com/kyverno/policy-reporter/pkg/report" -) - -var policyGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ - Name: "policy_report_summary", - Help: "Summary of all PolicyReports", -}, []string{"namespace", "name", "status"}) - -func CreatePolicyReportMetricsListener(filter *report.ReportFilter) report.PolicyReportListener { - prometheus.Register(policyGauge) - - var newReport v1alpha2.ReportInterface - - return func(event report.LifecycleEvent) { - newReport = event.PolicyReport - if !filter.Validate(newReport) { - return - } - - switch event.Type { - case report.Added: - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusSkip)).Set(float64(newReport.GetSummary().Skip)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusPass)).Set(float64(newReport.GetSummary().Pass)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusWarn)).Set(float64(newReport.GetSummary().Warn)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusFail)).Set(float64(newReport.GetSummary().Fail)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusError)).Set(float64(newReport.GetSummary().Error)) - case report.Updated: - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusSkip)).Set(float64(newReport.GetSummary().Skip)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusPass)).Set(float64(newReport.GetSummary().Pass)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusWarn)).Set(float64(newReport.GetSummary().Warn)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusFail)).Set(float64(newReport.GetSummary().Fail)) - policyGauge.WithLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusError)).Set(float64(newReport.GetSummary().Error)) - case report.Deleted: - policyGauge.DeleteLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusSkip)) - policyGauge.DeleteLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusPass)) - policyGauge.DeleteLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusWarn)) - policyGauge.DeleteLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusFail)) - policyGauge.DeleteLabelValues(newReport.GetNamespace(), newReport.GetName(), strings.Title(v1alpha2.StatusError)) - } - } -} diff --git a/pkg/listener/metrics/policy_report_test.go b/pkg/listener/metrics/policy_report_test.go deleted file mode 100644 index ccacfe11..00000000 --- a/pkg/listener/metrics/policy_report_test.go +++ /dev/null @@ -1,143 +0,0 @@ -package metrics_test - -import ( - "testing" - - "github.com/prometheus/client_golang/prometheus" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2" - "github.com/kyverno/policy-reporter/pkg/listener/metrics" - "github.com/kyverno/policy-reporter/pkg/report" - "github.com/kyverno/policy-reporter/pkg/validate" -) - -func Test_PolicyReportMetricGeneration(t *testing.T) { - report1 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "polr-test", - Namespace: "test", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 2, Fail: 1}, - } - - report2 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "polr-test", - Namespace: "test", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 3, Fail: 4}, - } - - report3 := &v1alpha2.PolicyReport{ - ObjectMeta: v1.ObjectMeta{ - Name: "polr-dev", - Namespace: "dev", - CreationTimestamp: v1.Now(), - }, - Summary: v1alpha2.PolicyReportSummary{Pass: 0, Fail: 1, Warn: 3}, - } - - filter := metrics.NewReportFilter(validate.RuleSets{Exclude: []string{"dev"}}, validate.RuleSets{Exclude: []string{"Test"}}) - - t.Run("Added Metric", func(t *testing.T) { - handler := metrics.CreatePolicyReportMetricsListener(filter) - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "policy_report_summary") - if summary == nil { - t.Fatalf("Metric not found: policy_report_summary") - } - - metrics := summary.GetMetric() - - if err = testSummaryMetricLabels(metrics[0], report1, "Error", 0); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[1], report1, "Fail", 1); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[2], report1, "Pass", 2); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[3], report1, "Skip", 0); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[4], report1, "Warn", 0); err != nil { - t.Error(err) - } - }) - - t.Run("Modified Metric", func(t *testing.T) { - handler := metrics.CreatePolicyReportMetricsListener(filter) - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - handler(report.LifecycleEvent{Type: report.Updated, PolicyReport: report2}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "policy_report_summary") - if summary == nil { - t.Fatalf("Metric not found: policy_report_summary") - } - - metrics := summary.GetMetric() - - if err = testSummaryMetricLabels(metrics[0], preport, "Error", 0); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[1], preport, "Fail", 4); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[2], preport, "Pass", 3); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[3], preport, "Skip", 0); err != nil { - t.Error(err) - } - if err = testSummaryMetricLabels(metrics[4], preport, "Warn", 0); err != nil { - t.Error(err) - } - }) - - t.Run("Deleted Metric", func(t *testing.T) { - handler := metrics.CreatePolicyReportMetricsListener(filter) - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report1}) - handler(report.LifecycleEvent{Type: report.Updated, PolicyReport: report2}) - handler(report.LifecycleEvent{Type: report.Deleted, PolicyReport: report2}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "policy_report_summary") - if summary != nil { - t.Error("policy_report_summary should no longer exist", *summary.Name) - } - }) - - t.Run("Validate Metric Filter", func(t *testing.T) { - handler := metrics.CreatePolicyReportMetricsListener(filter) - handler(report.LifecycleEvent{Type: report.Added, PolicyReport: report3}) - - metricFam, err := prometheus.DefaultGatherer.Gather() - if err != nil { - t.Errorf("unexpected Error: %s", err) - } - - summary := findMetric(metricFam, "policy_report_summary") - if summary != nil { - t.Error("policy_report_summary should not created", *summary.Name) - } - }) -}