From fbacb1b1413208d2f1e7a4aa59e7d9f78c93d8c4 Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Thu, 25 Apr 2024 19:05:13 +0100 Subject: [PATCH] metrics: Return a copy of LabelFilter in WithEnabledLabels(nil) LabelFilter.WithEnabledLabels behaved in the same way when passed nil or an empty slice - it disabled all labels. However, it's convenient to have a "no-op" option and just return a copy of the original filter. Let's do that when the passed slice is nil. This change doesn't affect users. Currently the method is always called with a non-nil slice. While here, I documented LabelFilter.WithEnabledLabels with edge cases. Signed-off-by: Anna Kapuscinska --- pkg/metrics/labelfilter.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/metrics/labelfilter.go b/pkg/metrics/labelfilter.go index c0dc1bbe057..931f237e7d1 100644 --- a/pkg/metrics/labelfilter.go +++ b/pkg/metrics/labelfilter.go @@ -3,10 +3,22 @@ package metrics +import ( + "maps" +) + type LabelFilter map[string]bool +// WithEnabledLabels returns a new LabelFilter with only the labels in enabledLabels enabled. +// If enabledLabels is nil, a copy of the original LabelFilter is returned. +// If enabledLabels is empty, all labels are disabled. +// If enabledLabels contains labels that are not in the original LabelFilter, they are ignored. func (f LabelFilter) WithEnabledLabels(enabledLabels []string) LabelFilter { - labelFilter := make(LabelFilter) + labelFilter := maps.Clone(f) + if enabledLabels == nil { + return labelFilter + } + // disable all configurable labels for l := range f { labelFilter[l] = false