@Override public void reportMetrics(final Map<MetricName, Gauge> gauges, final Map<MetricName, Counter> counters, final Map<MetricName, Histogram> histograms, final Map<MetricName, Meter> meters, final Map<MetricName, Timer> timers) { if (!corePlugin.isStagemonitorActive()) { return; } long timestamp = clock.getTime(); final Timer.Context time = registry.timer(reportingTimeMetricName).time(); final MetricsOutputStreamHandler metricsOutputStreamHandler = new MetricsOutputStreamHandler(gauges, counters, histograms, meters, timers, timestamp); if (!corePlugin.isOnlyLogElasticsearchMetricReports()) { if (!elasticsearchClient.isElasticsearchAvailable()) { return; } final String url = corePlugin.getElasticsearchUrl() + "/" + getTodaysIndexName() + "/" + METRICS_TYPE + "/_bulk"; httpClient.send("POST", url, CONTENT_TYPE_JSON, metricsOutputStreamHandler, NoopResponseHandler.INSTANCE); } else { try { final ByteArrayOutputStream os = new ByteArrayOutputStream(); metricsOutputStreamHandler.withHttpURLConnection(os); elasticsearchMetricsLogger.info(os.toString("UTF-8")); } catch (IOException e) { logger.warn(e.getMessage(), e); } } time.stop(); }
private void reportToElasticsearch(Metric2Registry metricRegistry, int reportingInterval, final MeasurementSession measurementSession) { if (corePlugin.isReportToElasticsearch()) { logger.info("Sending metrics to Elasticsearch ({}) every {}s", corePlugin.getElasticsearchUrlsWithoutAuthenticationInformation(), reportingInterval); } if (corePlugin.isReportToElasticsearch() || corePlugin.isOnlyLogElasticsearchMetricReports()) { final ElasticsearchReporter reporter = ElasticsearchReporter.forRegistry(metricRegistry, corePlugin) .globalTags(measurementSession.asMap()) .build(); reporter.start(reportingInterval, TimeUnit.SECONDS); corePlugin.closeOnShutdown(reporter); } else { logger.info("Not sending metrics to Elasticsearch (url={}, interval={}s)", corePlugin.getElasticsearchUrlsWithoutAuthenticationInformation(), reportingInterval); } }
@Test public void testSchedule() throws Exception { when(corePlugin.isOnlyLogElasticsearchMetricReports()).thenReturn(true); // this clock starts at 0 and then progresses normally when(clock.getTime()).then(new Answer<Long>() { long firstTimestamp; @Override public Long answer(InvocationOnMock invocation) throws Throwable { final long time = System.currentTimeMillis(); if (firstTimestamp == 0) { firstTimestamp = time; } return (time - firstTimestamp); } }); registry.register(name("test").build(), new Gauge<Integer>() { @Override public Integer getValue() { return 1; } }); elasticsearchReporter.start(100, TimeUnit.MILLISECONDS); Thread.sleep(300); verify(metricsLogger).info(eq(String.format("{\"index\":{\"_index\":\"stagemonitor-metrics-%s\",\"_type\":\"metrics\"}}\n" + "{\"@timestamp\":100,\"name\":\"test\",\"app\":\"test\",\"value\":1.0}\n", StringUtils.getLogstashStyleDate()))); verify(metricsLogger).info(eq(String.format("{\"index\":{\"_index\":\"stagemonitor-metrics-%s\",\"_type\":\"metrics\"}}\n" + "{\"@timestamp\":200,\"name\":\"test\",\"app\":\"test\",\"value\":1.0}\n", StringUtils.getLogstashStyleDate()))); }
@Test public void testReportToLog() throws Exception { when(corePlugin.isOnlyLogElasticsearchMetricReports()).thenReturn(true); elasticsearchReporter.reportMetrics( metricNameMap(name("gauge").build(), gauge(1)), metricNameMap(Counter.class), metricNameMap(Histogram.class), metricNameMap(Meter.class), metricNameMap(Timer.class)); verify(metricsLogger).info(eq(String.format("{\"index\":{\"_index\":\"stagemonitor-metrics-%s\",\"_type\":\"metrics\"}}\n" + "{\"@timestamp\":%d,\"name\":\"gauge\",\"app\":\"test\",\"value\":1.0}\n", StringUtils.getLogstashStyleDate(), timestamp))); }
doReturn(true).when(elasticsearchClient).isElasticsearchAvailable(); doReturn(elasticsearchClient).when(corePlugin).getElasticsearchClient(); doReturn(false).when(corePlugin).isOnlyLogElasticsearchMetricReports();