CustomMetricWriter extends StepMeterRegistry { @Override protected void publish() { try { List<Metric> metrics = getMeters().stream().flatMap(meter -> { return MetricsConverterFactory.getMetrics(meter); }).filter(Objects::nonNull).collect(Collectors.toList()); List<String> whitelistedMetricNames = config.getMetrics(); if (whitelistedMetricNames != null && !whitelistedMetricNames.isEmpty()) { LOG.debug("Applying filter for whitelisted metrics {}", whitelistedMetricNames); sendMetrics(getWhiteListedMetrics(metrics, whitelistedMetricNames)); return; } sendMetrics(metrics); } catch (Exception e) { LOG.error("Unable to send metrics to the Monitoring Service", e); } } CustomMetricWriter(CustomMetricsConfiguration config, Clock clock, MonitoringClient client);   }
@Test public void testPublish_successfullyWithoutWhitelistedMetrics() { CustomMetricWriter writer = createWriter(); writer.timer("timer"); writer.publish(); checkMetricsAreSent(); }
@Test public void testPublish_successfullyWithWhitelistedMetrics() throws Exception { EnvUtils.setEnvs(new String[][] { getCustomMetricsEnv() }); CustomMetricWriter writer = createWriter(); writer.timer("timer"); writer.timer("secondTimer"); writer.publish(); checkMetricsAreSent(); }
@Test public void testPublish_withException() throws Exception { doThrow(MonitoringClientException.class).when(client).send(anyListOf(Metric.class)); CustomMetricWriter writer = createWriter(); writer.timer("timer"); writer.publish(); verify(client, times(2)).send(anyListOf(Metric.class)); }