Skip to content

Commit

Permalink
Adding TestScope interface and factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
ravirajj committed May 4, 2020
1 parent 66fdfe0 commit 7df37e8
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
3 changes: 2 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 @@ -32,7 +32,7 @@
/**
* Default {@link Scope} implementation.
*/
class ScopeImpl implements Scope {
class ScopeImpl implements Scope, TestScope {
private StatsReporter reporter;
private String prefix;
private String separator;
Expand Down Expand Up @@ -244,6 +244,7 @@ String fullyQualifiedName(String name) {
* Returns a {@link Snapshot} of this {@link Scope}.
* @return a {@link Snapshot} of this {@link Scope}
*/
@Override
public Snapshot snapshot() {
Snapshot snap = new SnapshotImpl();

Expand Down
60 changes: 60 additions & 0 deletions core/src/main/java/com/uber/m3/tally/TestScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.m3.tally;

import com.uber.m3.util.Duration;

import java.util.Map;

/**
* TestScope is a metrics collector that has no reporting, ensuring that
* all emitted values have a given prefix or set of tags.
*/
interface TestScope extends Scope {

/**
* Creates a new TestScope that adds the ability to take snapshots of
* metrics emitted to it.
*/
static TestScope create() {
return (TestScope) new RootScopeBuilder()
.reporter(new NullStatsReporter())
.reportEvery(Duration.ZERO);
}

/**
* Creates a new TestScope with given prefix/tags that adds the ability to
* take snapshots of metrics emitted to it.
*/
static TestScope create(String prefix, Map<String, String> tags) {
return (TestScope) new RootScopeBuilder()
.prefix(prefix)
.tags(tags)
.reporter(new NullStatsReporter())
.reportEvery(Duration.ZERO);
}

/**
* Snapshot returns a copy of all values since the last report execution
* This is an expensive operation and should only be used for testing purposes.
*/
Snapshot snapshot();
}
2 changes: 1 addition & 1 deletion core/src/test/java/com/uber/m3/tally/ScopeImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void snapshot() {
System.err.println("Interrupted while sleeping! Let's continue anyway...");
}

Snapshot snapshot = ((ScopeImpl) rootScope).snapshot();
Snapshot snapshot = ((TestScope) rootScope).snapshot();

Map<String, CounterSnapshot> counters = snapshot.counters();
assertEquals(1, counters.size());
Expand Down
65 changes: 65 additions & 0 deletions core/src/test/java/com/uber/m3/tally/TestScopeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.m3.tally;

import com.uber.m3.util.ImmutableMap;
import org.junit.Test;

import java.util.Map;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class TestScopeTest {

@Test
public void getInstance() {
TestScope testScope = TestScope.create();
assertNotNull(testScope);
assertThat(testScope, instanceOf(Scope.class));
assertThat(testScope, instanceOf(ScopeImpl.class));

assertNotNull(testScope.capabilities());
assertFalse(testScope.capabilities().reporting());
assertFalse(testScope.capabilities().tagging());
}

@Test
public void prefixTags() {
Map<String, String> tags = ImmutableMap.of("key", "value");
TestScope testScope = TestScope.create("prefix", tags);
testScope.counter("counter").inc(1);

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

Map<String, CounterSnapshot> counters = snapshot.counters();
assertNotNull(counters);
assertEquals(1, counters.size());
assertNotNull(counters.get("prefix.counter+key=value"));
assertEquals("prefix.counter", counters.get("prefix.counter+key=value").name());
assertEquals(tags, counters.get("prefix.counter+key=value").tags());
assertEquals(1, counters.get("prefix.counter+key=value").value());
}
}

0 comments on commit 7df37e8

Please sign in to comment.