feat(metrics): Add util func to reset label allow lists

Adds a utility function `ResetLabelValueAllowLists` to reset the allow lists for label values.  This facilitates testing by allowing tests to clear the global state between runs and avoid unintended side effects.
This commit is contained in:
yongruilin 2024-10-29 14:13:35 -07:00
parent f087575f21
commit d2ef8a1808
2 changed files with 26 additions and 0 deletions

View File

@ -37,6 +37,14 @@ var (
allowListLock sync.RWMutex
)
// ResetLabelValueAllowLists resets the allow lists for label values.
// NOTE: This should only be used in test.
func ResetLabelValueAllowLists() {
allowListLock.Lock()
defer allowListLock.Unlock()
labelValueAllowLists = map[string]*MetricLabelAllowList{}
}
// KubeOpts is superset struct for prometheus.Opts. The prometheus Opts structure
// is purposefully not embedded here because that would change struct initialization
// in the manner which people are currently accustomed.

View File

@ -208,3 +208,21 @@ metric2,label2: v3`,
})
}
}
func TestResetLabelValueAllowLists(t *testing.T) {
labelValueAllowLists = map[string]*MetricLabelAllowList{
"metric1": {
labelToAllowList: map[string]sets.Set[string]{
"label1": sets.New[string]("v1", "v2"),
},
},
"metric2": {
labelToAllowList: map[string]sets.Set[string]{
"label2": sets.New[string]("v3"),
},
},
}
ResetLabelValueAllowLists()
assert.Empty(t, labelValueAllowLists)
}