Skip to content

Commit d5193a2

Browse files
dougqhclaude
andcommitted
Test whole_key collapse increments the pre-created StatsMetrics counter
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 533dc1f commit d5193a2

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

internal-api/src/test/java/datadog/trace/api/metrics/StatsMetricsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ void nonPositiveCountsAreIgnored() {
7373
assertEquals(4, countersByTag().get(reason).getValue());
7474
}
7575

76+
@Test
77+
void wholeKeyCollapseIncrementsPreCreatedCounter() {
78+
StatsMetrics metrics = StatsMetrics.getInstance();
79+
80+
// The whole_key counter is pre-created at construction, so it is always present in the drain.
81+
StatsMetrics.TaggedCounter counter = countersByTag().get(StatsMetrics.COLLAPSED_WHOLE_KEY);
82+
assertEquals(StatsMetrics.COLLAPSED_SPANS, counter.getName());
83+
assertEquals(StatsMetrics.COLLAPSED_WHOLE_KEY, counter.getTag());
84+
85+
long before = counter.getValue();
86+
metrics.onWholeKeyCollapse();
87+
metrics.onWholeKeyCollapse();
88+
assertEquals(before + 2, counter.getValue(), "each call increments the counter by one");
89+
90+
// Routing the same tag through onCollapsedSpans hits the same pre-created counter instance.
91+
assertTrue(
92+
countersByTag().get(StatsMetrics.COLLAPSED_WHOLE_KEY) == counter,
93+
"whole_key resolves to a single stable counter");
94+
metrics.onCollapsedSpans(StatsMetrics.COLLAPSED_WHOLE_KEY, 3);
95+
assertEquals(before + 5, counter.getValue());
96+
}
97+
7698
@Test
7799
void reasonCounterIsStableAcrossLookups() {
78100
StatsMetrics metrics = StatsMetrics.getInstance();

0 commit comments

Comments
 (0)