Skip to content

Commit 869b8b7

Browse files
dougqhclaude
andcommitted
Drain StatsMetrics singleton deltas after each test
StatsMetrics is a process-wide singleton; these tests left per-reason deltas behind, leaking stats.collapsed_spans metrics into CoreMetricCollectorTest when they shared a Gradle worker and making its exact span-metric count assertion order-dependent. Reset every counter in @AfterEach. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c0ce934 commit 869b8b7

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map;
88
import java.util.function.Function;
99
import java.util.stream.Collectors;
10+
import org.junit.jupiter.api.AfterEach;
1011
import org.junit.jupiter.api.Test;
1112

1213
/**
@@ -20,6 +21,16 @@ private static Map<String, StatsMetrics.TaggedCounter> countersByTag() {
2021
.collect(Collectors.toMap(StatsMetrics.TaggedCounter::getTag, Function.identity()));
2122
}
2223

24+
@AfterEach
25+
void drainDeltas() {
26+
// StatsMetrics is a process-wide singleton; these tests leave per-reason deltas behind. Drain
27+
// every counter so they do not leak into another collector's expectations when tests share a
28+
// Gradle worker (e.g. CoreMetricCollectorTest asserting an exact span-metric count).
29+
for (StatsMetrics.TaggedCounter counter : StatsMetrics.getInstance().getTaggedCounters()) {
30+
counter.getValueAndReset();
31+
}
32+
}
33+
2334
@Test
2435
void accumulatesPerReasonAndEmitsResetDeltas() {
2536
StatsMetrics metrics = StatsMetrics.getInstance();

0 commit comments

Comments
 (0)