Skip to content

Commit

Permalink
policyfiltermetrics: Use pkg/metrics helpers to define metrics
Browse files Browse the repository at this point in the history
This lets us constrain label values (preventing growing cardinality) and drop
the explicit initialization function.

Signed-off-by: Anna Kapuscinska <[email protected]>
  • Loading branch information
lambdanis committed Aug 13, 2024
1 parent 03563e2 commit 5ead220
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 22 additions & 19 deletions pkg/metrics/policyfiltermetrics/policyfiltermetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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()
}
Expand Down
1 change: 0 additions & 1 deletion pkg/metricsconfig/healthmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ead220

Please sign in to comment.