From ae57960734ef9e1cf61cb227d5577ef689c8c17d Mon Sep 17 00:00:00 2001 From: Zameer Manji Date: Wed, 3 Jan 2024 16:58:02 -0500 Subject: [PATCH] Fix Snapshots https://github.com/uber-java/tally/pull/120 has a bug where the `Snapshot` only includes the subscopes and not the metrics on the current scope. This fixes the `snapshot` method to include metrics on the current scope as well. --- core/src/main/java/com/uber/m3/tally/ScopeImpl.java | 7 ++++++- .../src/test/java/com/uber/m3/tally/TestScopeTest.java | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java index 09e8377..6133957 100644 --- a/core/src/main/java/com/uber/m3/tally/ScopeImpl.java +++ b/core/src/main/java/com/uber/m3/tally/ScopeImpl.java @@ -23,6 +23,7 @@ import com.uber.m3.util.ImmutableMap; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Collection; import java.util.Map; import java.util.Optional; @@ -172,7 +173,11 @@ String fullyQualifiedName(String name) { public Snapshot snapshot() { Snapshot snap = new SnapshotImpl(); - for (ScopeImpl subscope : registry.subscopes.values()) { + ArrayList scopes = new ArrayList<>(); + scopes.add(this); + scopes.addAll(registry.subscopes.values()); + + for (ScopeImpl subscope : scopes) { ImmutableMap tags = new ImmutableMap.Builder() .putAll(this.tags) .putAll(subscope.tags) diff --git a/core/src/test/java/com/uber/m3/tally/TestScopeTest.java b/core/src/test/java/com/uber/m3/tally/TestScopeTest.java index ccfc3aa..5e84dab 100644 --- a/core/src/test/java/com/uber/m3/tally/TestScopeTest.java +++ b/core/src/test/java/com/uber/m3/tally/TestScopeTest.java @@ -47,12 +47,14 @@ public void testCreate() { testScope.tagged(tags).counter("counter").inc(1); + testScope.counter("untagged_counter").inc(1); + Snapshot snapshot = testScope.snapshot(); assertNotNull(snapshot); Map counters = snapshot.counters(); assertNotNull(counters); - assertEquals(1, counters.size()); + assertEquals(2, counters.size()); CounterSnapshot counterSnapshot = counters.get(new ScopeKey("counter", tags)); assertNotNull(counterSnapshot); @@ -60,6 +62,12 @@ public void testCreate() { assertEquals("counter", counterSnapshot.name()); assertEquals(tags, counterSnapshot.tags()); assertEquals(1, counterSnapshot.value()); + + counterSnapshot = counters.get(new ScopeKey("untagged_counter", ImmutableMap.EMPTY)); + assertNotNull(counterSnapshot); + assertEquals("untagged_counter", counterSnapshot.name()); + assertEquals(ImmutableMap.EMPTY, counterSnapshot.tags()); + assertEquals(1, counterSnapshot.value()); } @Test