From 313d3a96bf69b905a58ab91b7ea8bd6b0e4860fe Mon Sep 17 00:00:00 2001 From: Lauren Muhlhauser Date: Thu, 11 Feb 2021 11:21:05 -0500 Subject: [PATCH] Use original key value to report counter within reportMetered, add test (#102) --- .../metrics/ffwd/FastForwardReporter.java | 3 +- .../metrics/ffwd/FastForwardReporterTest.java | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ffwd-reporter/src/main/java/com/spotify/metrics/ffwd/FastForwardReporter.java b/ffwd-reporter/src/main/java/com/spotify/metrics/ffwd/FastForwardReporter.java index fb40e4e..060582d 100644 --- a/ffwd-reporter/src/main/java/com/spotify/metrics/ffwd/FastForwardReporter.java +++ b/ffwd-reporter/src/main/java/com/spotify/metrics/ffwd/FastForwardReporter.java @@ -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 @@ -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) { diff --git a/ffwd-reporter/src/test/java/com/spotify/metrics/ffwd/FastForwardReporterTest.java b/ffwd-reporter/src/test/java/com/spotify/metrics/ffwd/FastForwardReporterTest.java index 683d7e8..9bae0cb 100644 --- a/ffwd-reporter/src/test/java/com/spotify/metrics/ffwd/FastForwardReporterTest.java +++ b/ffwd-reporter/src/test/java/com/spotify/metrics/ffwd/FastForwardReporterTest.java @@ -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; @@ -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) @@ -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); @@ -214,4 +220,29 @@ public void shouldFinalReportAfterStopWithFlush() throws Exception { assert(counterBeforeStop < counterAfterStop); } + + @Test + public void testKeyValuePrefixAddedOnce() throws Exception { + ArgumentCaptor 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 expectedKeys = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "test.hi", "test.thename", "test.thename", "test.thename", "test.hi", "test.thename", + "test.thename", "test.thename"))); + + Set actualKeys = argumentCaptor.getAllValues().stream().map(Metric::getKey) + .collect(Collectors.toSet()); + + assertEquals(expectedKeys, actualKeys); + } }