CustomMetricsReporter extends ScheduledReporter { @Override public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { try { List<Metric> convertedMetrics = convert(gauges, counters, histograms, meters, timers); if (convertedMetrics.isEmpty()) { LOGGER.debug("No metrics for sending."); return; } sendMetrics(convertedMetrics); } catch (Exception e) { LOGGER.error("Unable to send metrics.", e); } } CustomMetricsReporter(MetricRegistry registry, MonitoringClient client,
                                 CustomMetricsConfiguration customMetricsConfig); @Override void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
                       SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
                       SortedMap<String, Timer> timers);  }
@Test public void testReportCounterSuccessfully() { Counter counter = registry.counter(METRIC_NAME); counter.inc(3); reporter.report(); checkMetricsAreSent(1, "counter", METRIC_NAME); }
@Test public void testReportGaugeSuccessfully() { registry.register(METRIC_NAME, new Gauge<Integer>() { @Override public Integer getValue() { return 13; } }); reporter.report(); checkMetricsAreSent(1, "gauge", METRIC_NAME); }
@Test public void testReportHistogramWithMetricQuantilesSuccessfully() { when(customMetricsConfig.metricQuantiles()).thenReturn(true); registry.histogram(METRIC_NAME); reporter.report(); checkMetricsAreSent(11, "histogram", METRIC_NAME); }
@Test public void testReportHistogramWithoutMetricQuantilesSuccessfully() { when(customMetricsConfig.metricQuantiles()).thenReturn(false); registry.histogram(METRIC_NAME); reporter.report(); checkMetricsAreSent(6, "histogram", METRIC_NAME); }
@Test public void testReportMeterWithMetricQuantilesSuccessfully() { when(customMetricsConfig.metricQuantiles()).thenReturn(true); registry.meter(METRIC_NAME); reporter.report(); checkMetricsAreSent(5, "meter", METRIC_NAME); }
@Test public void testReportMeterWithoutMetricQuantilesSuccessfully() { when(customMetricsConfig.metricQuantiles()).thenReturn(false); registry.meter(METRIC_NAME); reporter.report(); checkMetricsAreSent(2, "meter", METRIC_NAME); }
@Test public void testReportEmptyMetrics() { reporter.report(); verify(client, never()).send(anyListOf(Metric.class)); }
@Test public void testReportMetricWithWhitelist() { when(customMetricsConfig.metricQuantiles()).thenReturn(true); when(customMetricsConfig.getMetrics()).thenReturn(Arrays.asList(METRIC_NAME)); registry.meter(METRIC_NAME); reporter = new CustomMetricsReporter(registry, client, customMetricsConfig); reporter.report(); checkMetricsAreSent(5, "meter", METRIC_NAME); }
@Test public void testReportMetricWithoutWhitelist() { when(customMetricsConfig.getMetrics()).thenReturn(Arrays.asList("whitelistMetricName")); registry.meter(METRIC_NAME); reporter = new CustomMetricsReporter(registry, client, customMetricsConfig); reporter.report(); verify(client, never()).send(anyListOf(Metric.class)); }
@Test public void testReportNonEmptyMetricsWithMonitoringClientEx() { doThrow(MonitoringClientException.class).when(client).send(anyListOf(Metric.class)); registry.counter(METRIC_NAME); reporter.report(); verify(client, times(CustomMetricsReporter.SEND_METRICS_ATTEMPTS)).send(anyListOf(Metric.class)); }
@Test public void testReportNonEmptyMetricsWithException() { doThrow(Exception.class).when(client).send(anyListOf(Metric.class)); registry.counter(METRIC_NAME); reporter.report(); verify(client, times(1)).send(anyListOf(Metric.class)); }