From 55aaf4aac0db76bba135ea5e4b91e231b99bbeaa Mon Sep 17 00:00:00 2001 From: Todd Treece <360020+toddtreece@users.noreply.github.com> Date: Wed, 19 Feb 2025 16:11:26 -0500 Subject: [PATCH] Metrics: Add deny list in MultiRegistry (#101010) --- pkg/infra/metrics/service.go | 12 ++++++++++-- pkg/infra/metrics/service_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pkg/infra/metrics/service.go b/pkg/infra/metrics/service.go index 99c1e048d4f..599c2c5ff8d 100644 --- a/pkg/infra/metrics/service.go +++ b/pkg/infra/metrics/service.go @@ -124,11 +124,16 @@ func (g *addPrefixWrapper) Gather() ([]*dto.MetricFamily, error) { var _ prometheus.Gatherer = (*multiRegistry)(nil) type multiRegistry struct { + denyList map[string]struct{} gatherers []prometheus.Gatherer } func NewMultiRegistry(gatherers ...prometheus.Gatherer) *multiRegistry { + denyList := map[string]struct{}{ + "grafana_apiserver_request_slo_duration_seconds_bucket": {}, + } return &multiRegistry{ + denyList: denyList, gatherers: gatherers, } } @@ -143,9 +148,12 @@ func (r *multiRegistry) Gather() (mfs []*dto.MetricFamily, err error) { for i := 0; i < len(mf); i++ { m := mf[i] - _, exists := names[*m.Name] + // skip metrics in the deny list + if _, denied := r.denyList[*m.Name]; denied { + continue + } // prevent duplicate metric names - if exists { + if _, exists := names[*m.Name]; exists { // we can skip go_ and process_ metrics without returning an error // because they are known to be duplicates in both // the k8s and prometheus gatherers. diff --git a/pkg/infra/metrics/service_test.go b/pkg/infra/metrics/service_test.go index 4ee13be81b2..5275ee0ba89 100644 --- a/pkg/infra/metrics/service_test.go +++ b/pkg/infra/metrics/service_test.go @@ -131,6 +131,30 @@ func TestMultiRegistry_Gather(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedMF, mf) }) + + t.Run("denied metrics are not included", func(t *testing.T) { + one.GatherFunc = func() ([]*dto.MetricFamily, error) { + return []*dto.MetricFamily{ + {Name: strptr("grafana_apiserver_request_slo_duration_seconds_bucket")}, + }, nil + } + + two.GatherFunc = func() ([]*dto.MetricFamily, error) { + return []*dto.MetricFamily{ + {Name: strptr("b")}, + {Name: strptr("a")}, + }, nil + } + + expectedMF := []*dto.MetricFamily{ + {Name: strptr("a")}, + {Name: strptr("b")}, + } + + mf, err := g.Gather() + require.NoError(t, err) + require.Equal(t, expectedMF, mf) + }) } type mockGatherer struct {