Skip to content

Commit

Permalink
do not allocate gauge if cardinality metrics are turned off (#246)
Browse files Browse the repository at this point in the history
* do not allocate gauge if cardinality metrics are turned off

* add test cases
  • Loading branch information
brawndou authored Feb 16, 2024
1 parent 43e1579 commit e98f683
Show file tree
Hide file tree
Showing 2 changed files with 37 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
36 changes: 36 additions & 0 deletions scope_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,39 @@ func TestForEachScopeConcurrent(t *testing.T) {

<-done
}

func TestCachedReporterInternalMetricsAlloc(t *testing.T) {
tests := []struct {
name string
omitCardinalityMetrics bool
wantGauges int
}{
{
name: "omit metrics",
omitCardinalityMetrics: true,
wantGauges: 1,
},
{
name: "include metrics",
omitCardinalityMetrics: false,
wantGauges: 1 + numInternalMetrics,
},
}

for _, tt := range tests {
r := newTestStatsReporter()
root, closer := NewRootScope(ScopeOptions{CachedReporter: r, OmitCardinalityMetrics: tt.omitCardinalityMetrics}, 0)
s := root.(*scope)

r.gg.Add(tt.wantGauges)
s.Gauge("gauge-foo").Update(3)

closer.Close()
r.WaitAll()

assert.Equal(
t, tt.wantGauges, len(r.gauges), "%n: expected %d gauges, got %d gauges", tt.name, tt.wantGauges,
len(r.gauges),
)
}
}

0 comments on commit e98f683

Please sign in to comment.