Skip to content

Commit 79ef154

Browse files
authored
Fix Snapshots (#122)
#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.
1 parent 42a8b81 commit 79ef154

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

core/src/main/java/com/uber/m3/tally/ScopeImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.uber.m3.util.ImmutableMap;
2424

2525
import javax.annotation.Nullable;
26+
import java.util.ArrayList;
2627
import java.util.Collection;
2728
import java.util.Map;
2829
import java.util.Optional;
@@ -172,7 +173,11 @@ String fullyQualifiedName(String name) {
172173
public Snapshot snapshot() {
173174
Snapshot snap = new SnapshotImpl();
174175

175-
for (ScopeImpl subscope : registry.subscopes.values()) {
176+
ArrayList<ScopeImpl> scopes = new ArrayList<>();
177+
scopes.add(this);
178+
scopes.addAll(registry.subscopes.values());
179+
180+
for (ScopeImpl subscope : scopes) {
176181
ImmutableMap<String, String> tags = new ImmutableMap.Builder<String, String>()
177182
.putAll(this.tags)
178183
.putAll(subscope.tags)

core/src/test/java/com/uber/m3/tally/TestScopeTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,27 @@ public void testCreate() {
4747

4848
testScope.tagged(tags).counter("counter").inc(1);
4949

50+
testScope.counter("untagged_counter").inc(1);
51+
5052
Snapshot snapshot = testScope.snapshot();
5153
assertNotNull(snapshot);
5254

5355
Map<ScopeKey, CounterSnapshot> counters = snapshot.counters();
5456
assertNotNull(counters);
55-
assertEquals(1, counters.size());
57+
assertEquals(2, counters.size());
5658

5759
CounterSnapshot counterSnapshot = counters.get(new ScopeKey("counter", tags));
5860
assertNotNull(counterSnapshot);
5961

6062
assertEquals("counter", counterSnapshot.name());
6163
assertEquals(tags, counterSnapshot.tags());
6264
assertEquals(1, counterSnapshot.value());
65+
66+
counterSnapshot = counters.get(new ScopeKey("untagged_counter", ImmutableMap.EMPTY));
67+
assertNotNull(counterSnapshot);
68+
assertEquals("untagged_counter", counterSnapshot.name());
69+
assertEquals(ImmutableMap.EMPTY, counterSnapshot.tags());
70+
assertEquals(1, counterSnapshot.value());
6371
}
6472

6573
@Test

0 commit comments

Comments
 (0)