-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit plucks select changes from #69 and #43 to bring uber-java/tally on par with uber-go/tally. It adds a `TestScope` class which allows users of the library to get a `Snapshot` of all the metrics emitted by the scope. Users can then assert if certain metrics were created.
- Loading branch information
Showing
8 changed files
with
297 additions
and
9 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
core/src/main/java/com/uber/m3/tally/NullStatsReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2023 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; | ||
|
||
/** | ||
* NullStatsReporter is a noop implementation of StatsReporter. | ||
*/ | ||
public class NullStatsReporter implements StatsReporter { | ||
@Override | ||
public Capabilities capabilities() { | ||
return CapableOf.NONE; | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
|
||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
@Override | ||
public void reportCounter(String name, Map<String, String> tags, long value) { | ||
|
||
} | ||
|
||
@Override | ||
public void reportGauge(String name, Map<String, String> tags, double value) { | ||
|
||
} | ||
|
||
@Override | ||
public void reportTimer(String name, Map<String, String> tags, Duration interval) { | ||
|
||
} | ||
|
||
@Override | ||
public void reportHistogramValueSamples(String name, Map<String, String> tags, Buckets buckets, double bucketLowerBound, double bucketUpperBound, long samples) { | ||
|
||
} | ||
|
||
@Override | ||
public void reportHistogramDurationSamples(String name, Map<String, String> tags, Buckets buckets, Duration bucketLowerBound, Duration bucketUpperBound, long samples) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2023 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 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. | ||
*/ | ||
public interface TestScope extends Scope { | ||
|
||
/** | ||
* Creates a new TestScope that adds the ability to take snapshots of | ||
* metrics emitted to it. | ||
*/ | ||
static TestScope create() { | ||
return new RootScopeBuilder() | ||
.reporter(new NullStatsReporter()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* 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 new RootScopeBuilder() | ||
.prefix(prefix) | ||
.tags(tags) | ||
.reporter(new NullStatsReporter()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
core/src/test/java/com/uber/m3/tally/NullStatsReporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2023 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 org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class NullStatsReporterTest { | ||
|
||
@Test | ||
public void capabilities() { | ||
NullStatsReporter reporter = new NullStatsReporter(); | ||
assertNotNull(reporter.capabilities()); | ||
assertFalse(reporter.capabilities().reporting()); | ||
assertFalse(reporter.capabilities().tagging()); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
core/src/test/java/com/uber/m3/tally/TestScopeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) 2023 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 testCreate() { | ||
TestScope testScope = TestScope.create(); | ||
assertNotNull(testScope); | ||
assertThat(testScope, instanceOf(Scope.class)); | ||
|
||
assertNotNull(testScope.capabilities()); | ||
assertFalse(testScope.capabilities().reporting()); | ||
assertFalse(testScope.capabilities().tagging()); | ||
|
||
ImmutableMap<String, String> tags = ImmutableMap.of("key", "value"); | ||
|
||
testScope.tagged(tags).counter("counter").inc(1); | ||
|
||
Snapshot snapshot = testScope.snapshot(); | ||
assertNotNull(snapshot); | ||
|
||
Map<ScopeKey, CounterSnapshot> counters = snapshot.counters(); | ||
assertNotNull(counters); | ||
assertEquals(1, counters.size()); | ||
|
||
CounterSnapshot counterSnapshot = counters.get(new ScopeKey("counter", tags)); | ||
assertNotNull(counterSnapshot); | ||
|
||
assertEquals("counter", counterSnapshot.name()); | ||
assertEquals(tags, counterSnapshot.tags()); | ||
assertEquals(1, counterSnapshot.value()); | ||
} | ||
|
||
@Test | ||
public void createWithPrefixAndTags() { | ||
Map<String, String> tags = ImmutableMap.of("key", "value"); | ||
TestScope testScope = TestScope.create("prefix", tags); | ||
testScope.tagged(ImmutableMap.of("other_key", "other_value")).counter("counter").inc(1); | ||
|
||
Snapshot snapshot = testScope.snapshot(); | ||
assertNotNull(snapshot); | ||
|
||
Map<ScopeKey, CounterSnapshot> counters = snapshot.counters(); | ||
assertNotNull(counters); | ||
assertEquals(1, counters.size()); | ||
|
||
ImmutableMap<String, String> totalTags = ImmutableMap.of("key", "value", "other_key", "other_value"); | ||
CounterSnapshot counterSnapshot = counters.get(new ScopeKey("prefix.counter", totalTags)); | ||
|
||
assertNotNull(counterSnapshot); | ||
assertEquals("prefix.counter", counterSnapshot.name()); | ||
assertEquals(totalTags, counterSnapshot.tags()); | ||
assertEquals(1, counterSnapshot.value()); | ||
} | ||
|
||
@Test | ||
public void testCreateWithTagsAndSubscope() { | ||
ImmutableMap<String, String> tags = ImmutableMap.of("key", "value"); | ||
TestScope testScope = TestScope.create("", tags); | ||
|
||
ImmutableMap<String, String> subScopeTags = ImmutableMap.of("key", "other_value"); | ||
testScope.tagged(subScopeTags).subScope("subscope").counter("counter").inc(1); | ||
|
||
Snapshot snapshot = testScope.snapshot(); | ||
assertNotNull(snapshot); | ||
|
||
Map<ScopeKey, CounterSnapshot> counters = snapshot.counters(); | ||
assertNotNull(counters); | ||
assertEquals(1, counters.size()); | ||
|
||
CounterSnapshot counterSnapshot = counters.get(new ScopeKey("subscope.counter", subScopeTags)); | ||
assertNotNull(counterSnapshot); | ||
|
||
assertEquals("subscope.counter", counterSnapshot.name()); | ||
assertEquals(subScopeTags, counterSnapshot.tags()); | ||
assertEquals(1, counterSnapshot.value()); | ||
} | ||
} | ||
|