refactor: Defer metrics label value allow list initialization

Defer the initialization of label value allow lists until the first invocation of `WithLabelValues` or `With`. This fixes the issue that metrics initialized before flag applied which results in label value allow list is not honored.
This commit is contained in:
yongruilin
2024-10-15 10:26:46 -07:00
parent 55b83c92b3
commit 67f3128cb9
6 changed files with 142 additions and 72 deletions

View File

@@ -119,11 +119,6 @@ func NewCounterVec(opts *CounterOpts, labels []string) *CounterVec {
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
cv := &CounterVec{
CounterVec: noopCounterVec,
@@ -176,7 +171,17 @@ func (v *CounterVec) WithLabelValues(lvs ...string) CounterMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainToAllowedList(v.originalLabels, lvs)
}
allowListLock.RUnlock()
})
}
return v.CounterVec.WithLabelValues(lvs...)
}
@@ -190,6 +195,15 @@ func (v *CounterVec) With(labels map[string]string) CounterMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainLabelMap(labels)
}
allowListLock.RUnlock()
})
}
return v.CounterVec.With(labels)
}

View File

@@ -105,11 +105,6 @@ func NewGaugeVec(opts *GaugeOpts, labels []string) *GaugeVec {
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
cv := &GaugeVec{
GaugeVec: noopGaugeVec,
@@ -149,6 +144,15 @@ func (v *GaugeVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric, error) {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainToAllowedList(v.originalLabels, lvs)
}
allowListLock.RUnlock()
})
}
elt, err := v.GaugeVec.GetMetricWithLabelValues(lvs...)
return elt, err
@@ -186,6 +190,15 @@ func (v *GaugeVec) WithChecked(labels map[string]string) (GaugeMetric, error) {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainLabelMap(labels)
}
allowListLock.RUnlock()
})
}
elt, err := v.GaugeVec.GetMetricWith(labels)
return elt, err

View File

@@ -96,11 +96,6 @@ func NewHistogramVec(opts *HistogramOpts, labels []string) *HistogramVec {
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
v := &HistogramVec{
HistogramVec: noopHistogramVec,
@@ -148,6 +143,15 @@ func (v *HistogramVec) WithLabelValues(lvs ...string) ObserverMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainToAllowedList(v.originalLabels, lvs)
}
allowListLock.RUnlock()
})
}
return v.HistogramVec.WithLabelValues(lvs...)
}
@@ -162,6 +166,15 @@ func (v *HistogramVec) With(labels map[string]string) ObserverMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainLabelMap(labels)
}
allowListLock.RUnlock()
})
}
return v.HistogramVec.With(labels)
}

View File

@@ -53,6 +53,7 @@ type KubeOpts struct {
deprecateOnce sync.Once
annotateOnce sync.Once
StabilityLevel StabilityLevel
initializeLabelAllowListsOnce sync.Once
LabelValueAllowLists *MetricLabelAllowList
}
@@ -170,6 +171,7 @@ type HistogramOpts struct {
deprecateOnce sync.Once
annotateOnce sync.Once
StabilityLevel StabilityLevel
initializeLabelAllowListsOnce sync.Once
LabelValueAllowLists *MetricLabelAllowList
}
@@ -217,6 +219,7 @@ type TimingHistogramOpts struct {
deprecateOnce sync.Once
annotateOnce sync.Once
StabilityLevel StabilityLevel
initializeLabelAllowListsOnce sync.Once
LabelValueAllowLists *MetricLabelAllowList
}
@@ -268,6 +271,7 @@ type SummaryOpts struct {
deprecateOnce sync.Once
annotateOnce sync.Once
StabilityLevel StabilityLevel
initializeLabelAllowListsOnce sync.Once
LabelValueAllowLists *MetricLabelAllowList
}

View File

@@ -109,11 +109,6 @@ func NewSummaryVec(opts *SummaryOpts, labels []string) *SummaryVec {
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
v := &SummaryVec{
SummaryOpts: opts,
@@ -160,6 +155,15 @@ func (v *SummaryVec) WithLabelValues(lvs ...string) ObserverMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainToAllowedList(v.originalLabels, lvs)
}
allowListLock.RUnlock()
})
}
return v.SummaryVec.WithLabelValues(lvs...)
}
@@ -174,6 +178,15 @@ func (v *SummaryVec) With(labels map[string]string) ObserverMetric {
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainLabelMap(labels)
}
allowListLock.RUnlock()
})
}
return v.SummaryVec.With(labels)
}

View File

@@ -125,11 +125,6 @@ func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts *TimingHistogr
opts.StabilityLevel.setDefaults()
fqName := BuildFQName(opts.Namespace, opts.Subsystem, opts.Name)
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[fqName]; ok {
opts.LabelValueAllowLists = allowList
}
allowListLock.RUnlock()
v := &TimingHistogramVec{
TimingHistogramVec: noopTimingHistogramVec,
@@ -175,6 +170,15 @@ func (v *TimingHistogramVec) WithLabelValuesChecked(lvs ...string) (GaugeMetric,
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainToAllowedList(v.originalLabels, lvs)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainToAllowedList(v.originalLabels, lvs)
}
allowListLock.RUnlock()
})
}
ops, err := v.TimingHistogramVec.GetMetricWithLabelValues(lvs...)
if err != nil {
@@ -214,6 +218,15 @@ func (v *TimingHistogramVec) WithChecked(labels map[string]string) (GaugeMetric,
}
if v.LabelValueAllowLists != nil {
v.LabelValueAllowLists.ConstrainLabelMap(labels)
} else {
v.initializeLabelAllowListsOnce.Do(func() {
allowListLock.RLock()
if allowList, ok := labelValueAllowLists[v.FQName()]; ok {
v.LabelValueAllowLists = allowList
allowList.ConstrainLabelMap(labels)
}
allowListLock.RUnlock()
})
}
ops, err := v.TimingHistogramVec.GetMetricWith(labels)
return ops.(GaugeMetric), err