Skip to content

Instantly share code, notes, and snippets.

@sherman
Created October 5, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sherman/5485144d951d38d79e7b3187119590c9 to your computer and use it in GitHub Desktop.
Save sherman/5485144d951d38d79e7b3187119590c9 to your computer and use it in GitHub Desktop.
package metrics.reporter;
import com.codahale.metrics.*;
import config.database.config.ConfigDatabaseConfiguration;
import influxdb.client.configuration.InfluxDbClientPropertiesConfiguration;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.SortedMap;
import java.util.concurrent.TimeUnit;
import static java.lang.String.valueOf;
import static org.joda.time.DateTime.now;
import static org.joda.time.DateTimeZone.UTC;
/**
* @author Denis Gabaydulin
* @since 12/08/2016
*/
public class InfluxDbReporter extends ScheduledReporter {
private static final Logger log = LoggerFactory.getLogger(InfluxDbReporter.class);
private static final String REPORTER_NAME = "influxdb_reporter";
private final InfluxDB influxDB;
private final ConfigDatabaseConfiguration configuration;
private final InfluxDbClientPropertiesConfiguration clientPropertiesConfiguration;
/**
* Creates a new {@link ScheduledReporter} instance.
*
* @param registry the {@link MetricRegistry} containing the metrics this
* reporter will report
* @param filter the filter for which metrics to report
* @param rateUnit
* @param durationUnit
*/
protected InfluxDbReporter(
InfluxDB influxDB,
InfluxDbClientPropertiesConfiguration clientPropertiesConfiguration,
ConfigDatabaseConfiguration configuration,
MetricRegistry registry,
MetricFilter filter,
TimeUnit rateUnit,
TimeUnit durationUnit
) {
super(registry, REPORTER_NAME, filter, rateUnit, durationUnit);
this.influxDB = influxDB;
this.configuration = configuration;
this.clientPropertiesConfiguration = clientPropertiesConfiguration;
}
@Override
public void report(
SortedMap<String, Gauge> gauges,
SortedMap<String, Counter> counters,
SortedMap<String, Histogram> histograms,
SortedMap<String, Meter> meters,
SortedMap<String, Timer> timers
) {
long reportTime = now(UTC).getMillis();
gauges.entrySet().forEach(entry -> logGauge(reportTime, entry.getKey(), entry.getValue()));
counters.entrySet().forEach(entry -> logCounter(reportTime, entry.getKey(), entry.getValue()));
histograms.entrySet().forEach(entry -> logHistogram(reportTime, entry.getKey(), entry.getValue()));
meters.entrySet().forEach(entry -> logMeter(reportTime, entry.getKey(), entry.getValue()));
timers.entrySet().forEach(entry -> logTimer(reportTime, entry.getKey(), entry.getValue()));
}
private void logTimer(long reportTime, String name, Timer timer) {
Snapshot snapshot = timer.getSnapshot();
Point point = Point.measurement(name.replace(".", "_"))
.time(reportTime, TimeUnit.MILLISECONDS)
.tag("host", configuration.getHost())
.tag("app", configuration.getModule())
.tag("machine", valueOf(configuration.getMachineId()))
.addField("count", timer.getCount())
.addField("min", convertDuration(snapshot.getMin()))
.addField("max", convertDuration(snapshot.getMax()))
.addField("mean", convertDuration(snapshot.getMean()))
.addField("stdDev", convertDuration(snapshot.getStdDev()))
.addField("median", convertDuration(snapshot.getMedian()))
.addField("75thPercentile", convertDuration(snapshot.get75thPercentile()))
.addField("95thPercentile", convertDuration(snapshot.get95thPercentile()))
.addField("99thPercentile", convertDuration(snapshot.get99thPercentile()))
.addField("meanRate", timer.getMeanRate())
.build();
try {
influxDB.write(clientPropertiesConfiguration.getDatabase(), "default", point);
} catch (Exception ignored) {
}
}
private void logMeter(long reportTime, String name, Meter meter) {
Point point = Point.measurement(name.replace(".", "_"))
.time(reportTime, TimeUnit.MILLISECONDS)
.tag("host", configuration.getHost())
.tag("app", configuration.getModule())
.tag("machine", valueOf(configuration.getMachineId()))
.addField("count", meter.getCount())
.addField("meanRate", meter.getMeanRate())
.build();
try {
influxDB.write(clientPropertiesConfiguration.getDatabase(), "default", point);
} catch (Exception ignored) {
}
}
private void logHistogram(long reportTime, String name, Histogram histogram) {
Snapshot snapshot = histogram.getSnapshot();
Point point = Point.measurement(name.replace(".", "_"))
.time(reportTime, TimeUnit.MILLISECONDS)
.tag("host", configuration.getHost())
.tag("app", configuration.getModule())
.tag("machine", valueOf(configuration.getMachineId()))
.addField("count", histogram.getCount())
.addField("min", (double) snapshot.getMin())
.addField("max", (double) snapshot.getMax())
.addField("mean", snapshot.getMean())
.addField("stdDev", snapshot.getStdDev())
.addField("median", snapshot.getMedian())
.addField("75thPercentile", snapshot.get75thPercentile())
.addField("95thPercentile", snapshot.get95thPercentile())
.addField("99thPercentile", snapshot.get99thPercentile())
.build();
try {
influxDB.write(clientPropertiesConfiguration.getDatabase(), "default", point);
} catch (Exception ignored) {
}
}
private void logCounter(long reportTime, String name, Counter counter) {
Point point = Point.measurement(name.replace(".", "_"))
.time(reportTime, TimeUnit.MILLISECONDS)
.tag("host", configuration.getHost())
.tag("app", configuration.getModule())
.tag("machine", valueOf(configuration.getMachineId()))
.addField("count", counter.getCount())
.build();
try {
influxDB.write(clientPropertiesConfiguration.getDatabase(), "default", point);
} catch (Exception ignored) {
}
}
private void logGauge(long reportTime, String name, Gauge gauge) {
Point point = Point.measurement(name.replace(".", "_"))
.time(reportTime, TimeUnit.MILLISECONDS)
.tag("host", configuration.getHost())
.tag("app", configuration.getModule())
.tag("machine", valueOf(configuration.getMachineId()))
.addField("value", valueOf(gauge.getValue()))
.build();
try {
influxDB.write(clientPropertiesConfiguration.getDatabase(), "default", point);
} catch (Exception ignored) {
}
}
public static class Builder {
private final MetricRegistry registry;
private TimeUnit rateUnit;
private TimeUnit durationUnit;
private MetricFilter filter;
private InfluxDB influxDB;
private InfluxDbClientPropertiesConfiguration clientPropertiesConfiguration;
private ConfigDatabaseConfiguration configuration;
public Builder(
MetricRegistry registry,
InfluxDB influxDB,
InfluxDbClientPropertiesConfiguration clientPropertiesConfiguration,
ConfigDatabaseConfiguration configuration
) {
this.registry = registry;
this.influxDB = influxDB;
this.clientPropertiesConfiguration = clientPropertiesConfiguration;
this.configuration = configuration;
this.rateUnit = TimeUnit.SECONDS;
this.durationUnit = TimeUnit.MILLISECONDS;
this.filter = MetricFilter.ALL;
}
public Builder convertRatesTo(TimeUnit rateUnit) {
this.rateUnit = rateUnit;
return this;
}
public Builder convertDurationsTo(TimeUnit durationUnit) {
this.durationUnit = durationUnit;
return this;
}
public Builder filter(MetricFilter filter) {
this.filter = filter;
return this;
}
public InfluxDbReporter build() {
return new InfluxDbReporter(
influxDB,
clientPropertiesConfiguration,
configuration,
registry,
filter,
rateUnit,
durationUnit
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment