Skip to content

Commit e98f683

Browse files
authored
do not allocate gauge if cardinality metrics are turned off (#246)
* do not allocate gauge if cardinality metrics are turned off * add test cases
1 parent 43e1579 commit e98f683

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

scope_registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func newScopeRegistryWithShardCount(
103103
}
104104
r.subscopes[i].s[scopeRegistryKey(root.prefix, root.tags)] = root
105105
}
106-
if r.root.cachedReporter != nil {
106+
if r.root.cachedReporter != nil && !omitCardinalityMetrics {
107107
r.cachedCounterCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedCounterCardinalityName, r.cardinalityMetricsTags)
108108
r.cachedGaugeCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedGaugeCardinalityName, r.cardinalityMetricsTags)
109109
r.cachedHistogramCardinalityGauge = r.root.cachedReporter.AllocateGauge(r.sanitizedHistogramCardinalityName, r.cardinalityMetricsTags)

scope_registry_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,39 @@ func TestForEachScopeConcurrent(t *testing.T) {
221221

222222
<-done
223223
}
224+
225+
func TestCachedReporterInternalMetricsAlloc(t *testing.T) {
226+
tests := []struct {
227+
name string
228+
omitCardinalityMetrics bool
229+
wantGauges int
230+
}{
231+
{
232+
name: "omit metrics",
233+
omitCardinalityMetrics: true,
234+
wantGauges: 1,
235+
},
236+
{
237+
name: "include metrics",
238+
omitCardinalityMetrics: false,
239+
wantGauges: 1 + numInternalMetrics,
240+
},
241+
}
242+
243+
for _, tt := range tests {
244+
r := newTestStatsReporter()
245+
root, closer := NewRootScope(ScopeOptions{CachedReporter: r, OmitCardinalityMetrics: tt.omitCardinalityMetrics}, 0)
246+
s := root.(*scope)
247+
248+
r.gg.Add(tt.wantGauges)
249+
s.Gauge("gauge-foo").Update(3)
250+
251+
closer.Close()
252+
r.WaitAll()
253+
254+
assert.Equal(
255+
t, tt.wantGauges, len(r.gauges), "%n: expected %d gauges, got %d gauges", tt.name, tt.wantGauges,
256+
len(r.gauges),
257+
)
258+
}
259+
}

0 commit comments

Comments
 (0)