Skip to content

Commit

Permalink
do not allocate gauge if cardinality metrics are turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
brawndou committed Feb 14, 2024
1 parent 83f9861 commit f8dac6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scope_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions scope_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

0 comments on commit f8dac6f

Please sign in to comment.