Skip to content

Commit

Permalink
metrics: Return a copy of LabelFilter in WithEnabledLabels(nil)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lambdanis committed Apr 26, 2024
1 parent c579051 commit fbacb1b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/metrics/labelfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbacb1b

Please sign in to comment.