diff --git a/scope_registry.go b/scope_registry.go index d1481da..fc1b50b 100644 --- a/scope_registry.go +++ b/scope_registry.go @@ -103,7 +103,7 @@ func newScopeRegistryWithShardCount( } r.subscopes[i].s[scopeRegistryKey(root.prefix, root.tags)] = root } - if r.root.cachedReporter != nil { + if r.root.cachedReporter != nil && !omitCardinalityMetrics { r.cachedCounterCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedCounterCardinalityName, r.cardinalityMetricsTags) r.cachedGaugeCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedGaugeCardinalityName, r.cardinalityMetricsTags) r.cachedHistogramCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedHistogramCardinalityName, r.cardinalityMetricsTags) diff --git a/scope_registry_test.go b/scope_registry_test.go index 5339fc2..6ddc2c2 100644 --- a/scope_registry_test.go +++ b/scope_registry_test.go @@ -221,3 +221,22 @@ func TestForEachScopeConcurrent(t *testing.T) { <-done } + +func TestCachedReporterNoAllocOnOmitInternalMetrics(t *testing.T) { + r := newTestStatsReporter() + root, closer := NewRootScope(ScopeOptions{CachedReporter: r, OmitCardinalityMetrics: true}, 0) + wantGauges := 1 + + s := root.(*scope) + + r.gg.Add(wantGauges) + s.Gauge("gauge-foo").Update(3) + + closer.Close() + r.WaitAll() + + assert.Equal( + t, wantGauges, len(r.gauges), "expected %d gauges, got %d gauges", wantGauges, + len(r.gauges), + ) +}