Created
September 22, 2016 18:48
-
-
Save johnrengelman/3b01b2d782ee4b9fdd0acda89f28637e to your computer and use it in GitHub Desktop.
InfluxDB Reporter module for Ratpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.peoplenet.common.ratpack.metrics | |
import com.codahale.metrics.MetricRegistry | |
import com.google.inject.Inject | |
import com.google.inject.Provides | |
import com.izettle.metrics.influxdb.InfluxDbHttpSender | |
import com.izettle.metrics.influxdb.InfluxDbSender | |
import com.izettle.metrics.influxdb.InfluxDbTcpSender | |
import com.izettle.metrics.influxdb.InfluxDbUdpSender | |
import com.peoplenet.common.ratpack.config.Application | |
import com.peoplenet.metrics.InfluxDbReporter | |
import com.peoplenet.metrics.Metrics | |
import ratpack.guice.ConfigurableModule | |
import ratpack.service.Service | |
import ratpack.service.StartEvent | |
import javax.inject.Provider | |
import java.util.concurrent.TimeUnit | |
class InfluxDbMetricsModule extends ConfigurableModule<Metrics> { | |
@Override | |
protected void configure() { | |
bind(InfluxDbReporter).toProvider(InfluxDbReporterProvider) | |
bind(Startup) | |
} | |
@Provides | |
InfluxDbSender influxDbSender(Metrics conf) { | |
switch(conf.protocol.toLowerCase()) { | |
case "http": | |
return new InfluxDbHttpSender("http", conf.hostname, conf.port, conf.database, null, conf.timePrecision, conf.socketTimeout, conf.socketTimeout) | |
break | |
case "tcp": | |
return new InfluxDbTcpSender(conf.hostname, conf.port, conf.socketTimeout, conf.database, conf.timePrecision) | |
break | |
default: | |
return new InfluxDbUdpSender(conf.hostname, conf.port, conf.socketTimeout, conf.database, conf.timePrecision) | |
break | |
} | |
} | |
static class InfluxDbReporterProvider implements Provider<InfluxDbReporter> { | |
private final InfluxDbSender sender | |
private final Metrics conf | |
private final MetricRegistry metricRegistry | |
private final Application application | |
@Inject | |
InfluxDbReporterProvider(InfluxDbSender sender, Metrics conf, MetricRegistry metricRegistry, Application application) { | |
this.sender = sender | |
this.conf = conf | |
this.metricRegistry = metricRegistry | |
this.application = application | |
} | |
@Override | |
InfluxDbReporter get() { | |
return InfluxDbReporter.forRegistry(metricRegistry).withTags([ | |
"environment": application.environment, | |
"app": application.name, | |
"hostname": InetAddress.getLocalHost().getHostName(), | |
]) | |
.groupCounters(false) | |
.groupGauges(false) | |
.measurementMappings(conf.mappings) | |
.tagMappings(conf.tags) | |
.build(sender) | |
} | |
} | |
static class Startup implements Service { | |
private final InfluxDbReporter reporter | |
@Inject | |
Startup(InfluxDbReporter reporter) { | |
this.reporter = reporter | |
} | |
@Override | |
void onStart(StartEvent event) throws Exception { | |
reporter.start(1000, TimeUnit.MILLISECONDS) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment