Skip to content

Commit

Permalink
Use original key value to report counter within reportMetered, add te…
Browse files Browse the repository at this point in the history
…st (#102)
  • Loading branch information
lmuhlha authored Feb 11, 2021
1 parent 2bee9e7 commit 313d3a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ private void reportHistogram(MetricId key, Histogram value) {
}

private void reportMetered(MetricId key, Meter value) {
MetricId originalKey = key;
key = MetricId.join(prefix, key);

final Metric m = FastForward
Expand All @@ -305,7 +306,7 @@ private void reportMetered(MetricId key, Meter value) {
.attribute(METRIC_TYPE, "meter");

reportMetered(m, value);
reportCounter(key, value);
reportCounter(originalKey, value);
}

private void reportTimer(MetricId key, Timer value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
import com.spotify.metrics.core.MetricId;
import com.spotify.metrics.core.SemanticMetricRegistry;
import com.spotify.metrics.tags.EnvironmentTagExtractor;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.jmock.lib.concurrent.DeterministicScheduler;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -46,6 +51,7 @@ public void setUp() throws Exception {
executorService = new DeterministicScheduler();
reporter = FastForwardReporter
.forRegistry(registry)
.prefix("test")
.schedule(TimeUnit.MILLISECONDS, REPORTING_PERIOD)
.fastForward(fastForward)
.executorService(executorService)
Expand Down Expand Up @@ -111,7 +117,7 @@ public void shouldIncludeDerivingMetersInReport() throws Exception {
new HashMap<>(Maps.asMap(ImmutableSet.of("1m", "5m"), input -> false));

for (Metric metric : metrics) {
if (metric.getKey().equals("thename")) {
if (metric.getKey().equals("test.thename")) {
String stat = metric.getAttributes().get("stat");

foundStats.put(stat, true);
Expand Down Expand Up @@ -214,4 +220,29 @@ public void shouldFinalReportAfterStopWithFlush() throws Exception {

assert(counterBeforeStop < counterAfterStop);
}

@Test
public void testKeyValuePrefixAddedOnce() throws Exception {
ArgumentCaptor<Metric> argumentCaptor = ArgumentCaptor.forClass(Metric.class);

doNothing().when(fastForward).send(argumentCaptor.capture());

MetricId name = MetricId.build("thename");

registry.meter(name);

reporter.start();

executorService.tick(REPORTING_PERIOD + REPORTING_PERIOD / 3, TimeUnit.MILLISECONDS);
verify(fastForward, atLeastOnce()).send(any(Metric.class));

Set<String> expectedKeys = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
"test.hi", "test.thename", "test.thename", "test.thename", "test.hi", "test.thename",
"test.thename", "test.thename")));

Set<String> actualKeys = argumentCaptor.getAllValues().stream().map(Metric::getKey)
.collect(Collectors.toSet());

assertEquals(expectedKeys, actualKeys);
}
}

0 comments on commit 313d3a9

Please sign in to comment.