class ElasticsearchReporter extends ScheduledMetrics2Reporter {

	@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  ElasticsearchReporter(Builder builder);

}