Skip to content

Commit

Permalink
Fix Snapshots (#122)
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
zmanji authored Jan 4, 2024
1 parent 42a8b81 commit 79ef154
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion core/src/main/java/com/uber/m3/tally/ScopeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -172,7 +173,11 @@ String fullyQualifiedName(String name) {
public Snapshot snapshot() {
Snapshot snap = new SnapshotImpl();

for (ScopeImpl subscope : registry.subscopes.values()) {
ArrayList<ScopeImpl> scopes = new ArrayList<>();
scopes.add(this);
scopes.addAll(registry.subscopes.values());

for (ScopeImpl subscope : scopes) {
ImmutableMap<String, String> tags = new ImmutableMap.Builder<String, String>()
.putAll(this.tags)
.putAll(subscope.tags)
Expand Down
10 changes: 9 additions & 1 deletion core/src/test/java/com/uber/m3/tally/TestScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,27 @@ public void testCreate() {

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

testScope.counter("untagged_counter").inc(1);

Snapshot snapshot = testScope.snapshot();
assertNotNull(snapshot);

Map<ScopeKey, CounterSnapshot> counters = snapshot.counters();
assertNotNull(counters);
assertEquals(1, counters.size());
assertEquals(2, counters.size());

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

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
Expand Down

0 comments on commit 79ef154

Please sign in to comment.