From 45210bd5f8b1a446e5a8a0b224986af684aeb85e Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Sat, 20 Oct 2018 16:54:49 +0200 Subject: [PATCH] Add support for skipping idle counters This is the same issue as reported in #78. For some reason, idle counters still get reported. It would be nice if we could not send anything from the client during its idle period. --- .../metrics/influxdb/InfluxDbReporter.java | 3 +++ .../influxdb/InfluxDbReporterTest.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/metrics-influxdb/src/main/java/com/izettle/metrics/influxdb/InfluxDbReporter.java b/metrics-influxdb/src/main/java/com/izettle/metrics/influxdb/InfluxDbReporter.java index 01a4340..12a0dee 100644 --- a/metrics-influxdb/src/main/java/com/izettle/metrics/influxdb/InfluxDbReporter.java +++ b/metrics-influxdb/src/main/java/com/izettle/metrics/influxdb/InfluxDbReporter.java @@ -395,6 +395,9 @@ private void reportHistogram(String name, Histogram histogram, long now) { } private void reportCounter(String name, Counter counter, long now) { + if (canSkipMetric(name, counter)) { + return; + } Map fields = new HashMap(); fields.put("count", counter.getCount()); diff --git a/metrics-influxdb/src/test/java/com/izettle/metrics/influxdb/InfluxDbReporterTest.java b/metrics-influxdb/src/test/java/com/izettle/metrics/influxdb/InfluxDbReporterTest.java index 1d68937..0b4ccca 100644 --- a/metrics-influxdb/src/test/java/com/izettle/metrics/influxdb/InfluxDbReporterTest.java +++ b/metrics-influxdb/src/test/java/com/izettle/metrics/influxdb/InfluxDbReporterTest.java @@ -26,9 +26,11 @@ import java.util.SortedMap; import java.util.TreeMap; import java.util.concurrent.TimeUnit; + import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -517,6 +519,28 @@ public void shouldSkipIdleMetrics() throws Exception { verify(influxDb, times(1)).appendPoints(Mockito.any(InfluxDbPoint.class)); } + @Test + public void shouldSkipIdleCounters() { + when(influxDb.hasSeriesData()).thenReturn(true); + + final Counter counter = mock(Counter.class); + when(counter.getCount()).thenReturn(42L); + + InfluxDbReporter skippingReporter = InfluxDbReporter + .forRegistry(registry) + .convertRatesTo(TimeUnit.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS) + .filter(MetricFilter.ALL) + .withTags(globalTags) + .skipIdleMetrics(true) + .build(influxDb); + + skippingReporter.report(map(), map("question-of-life", counter), map(), map(), map()); + skippingReporter.report(map(), map("question-of-life", counter), map(), map(), map()); + + verify(influxDb, times(1)).appendPoints(ArgumentMatchers.any(InfluxDbPoint.class)); + } + @Test public void shouldCatchExceptions() throws Exception { doThrow(ConnectException.class).when(influxDb).writeData();