From 5ead220322cd41697cd652b772213cf621c2d36d Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Fri, 9 Aug 2024 02:19:20 +0100 Subject: [PATCH] policyfiltermetrics: Use pkg/metrics helpers to define metrics This lets us constrain label values (preventing growing cardinality) and drop the explicit initialization function. Signed-off-by: Anna Kapuscinska --- go.mod | 2 +- .../policyfiltermetrics.go | 41 ++++++++++--------- pkg/metricsconfig/healthmetrics.go | 1 - 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index 1fad6f0d1a8..231da01e66d 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( github.com/vishvananda/netlink v1.2.1-beta.2.0.20240524165444-4d4ba1473f21 go.uber.org/atomic v1.11.0 go.uber.org/multierr v1.11.0 + golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb golang.org/x/sync v0.8.0 golang.org/x/sys v0.22.0 golang.org/x/time v0.6.0 @@ -177,7 +178,6 @@ require ( go.uber.org/dig v1.17.1 // indirect go.uber.org/zap v1.26.0 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect - golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect golang.org/x/mod v0.19.0 // indirect golang.org/x/net v0.26.0 // indirect golang.org/x/oauth2 v0.20.0 // indirect diff --git a/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go b/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go index d06c4151323..9e3c5350f1e 100644 --- a/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go +++ b/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go @@ -4,6 +4,8 @@ package policyfiltermetrics import ( + "golang.org/x/exp/maps" + "github.com/cilium/tetragon/pkg/metrics" "github.com/cilium/tetragon/pkg/metrics/consts" "github.com/prometheus/client_golang/prometheus" @@ -64,15 +66,29 @@ func (s OperationErr) String() string { } var ( - PolicyFilterOpMetrics = prometheus.NewCounterVec(prometheus.CounterOpts{ - Namespace: consts.MetricsNamespace, - Name: "policyfilter_metrics_total", - Help: "Policy filter metrics. For internal use only.", - ConstLabels: nil, - }, []string{"subsys", "op", "error"}) + subsysLabel = metrics.ConstrainedLabel{ + Name: "subsys", + Values: maps.Values(subsysLabelValues), + } + + operationLabel = metrics.ConstrainedLabel{ + Name: "op", + Values: maps.Values(operationLabelValues), + } + + errorLabel = metrics.ConstrainedLabel{ + Name: "error", + Values: maps.Values(operationErrLabels), + } ) var ( + PolicyFilterOpMetrics = metrics.MustNewCounter(metrics.NewOpts( + consts.MetricsNamespace, "", "policyfilter_metrics_total", + "Policy filter metrics. For internal use only.", + nil, []metrics.ConstrainedLabel{subsysLabel, operationLabel, errorLabel}, nil, + ), nil) + PolicyFilterHookContainerNameMissingMetrics = prometheus.NewCounter(prometheus.CounterOpts{ Namespace: consts.MetricsNamespace, Name: "policyfilter_hook_container_name_missing_total", @@ -85,19 +101,6 @@ func RegisterMetrics(group metrics.Group) { group.MustRegister(PolicyFilterOpMetrics, PolicyFilterHookContainerNameMissingMetrics) } -func InitMetrics() { - // Initialize metrics with labels - for _, subsys := range subsysLabelValues { - for _, op := range operationLabelValues { - for _, err := range operationErrLabels { - PolicyFilterOpMetrics.WithLabelValues( - subsys, op, err, - ).Add(0) - } - } - } -} - func OpInc(subsys Subsys, op Operation, err string) { PolicyFilterOpMetrics.WithLabelValues(subsys.String(), op.String(), err).Inc() } diff --git a/pkg/metricsconfig/healthmetrics.go b/pkg/metricsconfig/healthmetrics.go index 70182dc5fc4..17ba97ead91 100644 --- a/pkg/metricsconfig/healthmetrics.go +++ b/pkg/metricsconfig/healthmetrics.go @@ -73,7 +73,6 @@ func registerHealthMetrics(group metrics.Group) { group.ExtendInit(opcodemetrics.InitMetrics) // policy filter metrics policyfiltermetrics.RegisterMetrics(group) - group.ExtendInit(policyfiltermetrics.InitMetrics) // process metrics process.RegisterMetrics(group) // ringbuf metrics