All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.izettle.metrics.dw.InfluxDbReporterFactory Maven / Gradle / Ivy
package com.izettle.metrics.dw;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.izettle.metrics.influxdb.InfluxDbHttpSender;
import com.izettle.metrics.influxdb.InfluxDbReporter;
import com.izettle.metrics.influxdb.InfluxDbTcpSender;
import com.izettle.metrics.influxdb.InfluxDbUdpSender;
import io.dropwizard.metrics.BaseReporterFactory;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.ValidationMethod;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.activation.UnsupportedDataTypeException;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
/**
* A factory for {@link InfluxDbReporter} instances.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* protocol
* http
* The protocol (http or https) of the InfluxDb server to report to.
*
*
* host
* localhost
* The hostname of the InfluxDb server to report to.
*
*
* port
* 8086
* The port of the InfluxDb server to report to.
*
*
* prefix
* None
* The prefix for Metric key names to report to InfluxDb.
*
*
* tags
* None
* tags for all metrics reported to InfluxDb.
*
*
* fields
* timers = p50, p99, m1_rate meters = m1_rate
* fields by metric type to reported to InfluxDb.
*
*
* database
* None
* The database that metrics will be reported to InfluxDb.
*
*
* precision
* 1m
* The precision of timestamps. Does not take into account the quantity, so for example `5m` will be minute precision
*
*
* connectTimeout
* 1500
* The connect timeout in milliseconds for connecting to InfluxDb.
*
*
* readTimeout
* 1500
* The read timeout in milliseconds for reading from InfluxDb.
*
*
* auth
* None
* An auth string of format username:password to authenticate with when reporting to InfluxDb.
*
*
* groupGauges
* None
* A boolean to signal whether to group gauges when reporting to InfluxDb.
*
*
* measurementMappings
* None
* A map for measurement mappings to be added, overridden or removed from the defaultMeasurementMappings.
*
*
* defaultMeasurementMappings
*
* health = .*\.health(\..*)?
* auth = .*\.auth\..*
* dao = *\.(jdbi|dao)\..*
* resources = *\.resources?\..*
* datasources = io\.dropwizard\.db\.ManagedPooledDataSource.*
* clients = org\.apache\.http\.client\.HttpClient.*
* client_connections = org\.apache\.http\.conn\.HttpClientConnectionManager.*
* connections = org\.eclipse\.jetty\.server\.HttpConnectionFactory.*
* thread_pools = org\.eclipse\.jetty\.util\.thread\.QueuedThreadPool.*
* logs = ch\.qos\.logback\.core\.Appender.*
* http_server = io\.dropwizard\.jetty\.MutableServletContextHandler.*
* raw_sql = org\.skife\.jdbi\.v2\.DBI\.raw-sql
* jvm = ^jvm$
* jvm_attribute = jvm\.attribute.*?
* jvm_buffers = jvm\.buffers\..*
* jvm_classloader = jvm\.classloader.*
* jvm_gc = jvm\.gc\..*
* jvm_memory = jvm\.memory\..*
* jvm_threads = jvm\.threads.*
* event_handlers = .*Handler.*
*
* A map with default measurement mappings.
*
*
* excludes
* defaultExcludes
* A set of pre-calculated metrics like usage and percentage, and unchanging JVM
* metrics to exclude by default
*
*
*/
@JsonTypeName("influxdb")
public class InfluxDbReporterFactory extends BaseReporterFactory {
@NotEmpty
private String protocol = "http";
@NotEmpty
private String host = "localhost";
@Range(min = 0, max = 49151)
private int port = 8086;
@NotNull
private String prefix = "";
@NotNull
private Map tags = new HashMap<>();
@NotEmpty
private ImmutableMap> fields = ImmutableMap.of(
"timers",
ImmutableSet.of("p50", "p99", "m1_rate"),
"meters",
ImmutableSet.of("m1_rate"));
@NotNull
private String database = "";
@NotNull
private String auth = "";
@Range(min = 500, max = 30000)
private int connectTimeout = 1500;
@Range(min = 500, max = 30000)
private int readTimeout = 1500;
@NotNull
private Duration precision = Duration.minutes(1);
@NotNull
private SenderType senderType = SenderType.HTTP;
private boolean groupGauges = true;
private ImmutableMap measurementMappings = ImmutableMap.of();
private ImmutableMap defaultMeasurementMappings = ImmutableMap.builder()
.put("health", ".*\\.health(\\..*)?$")
.put("auth", ".*\\.auth\\..*")
.put("dao", ".*\\.(jdbi|dao)\\..*")
.put("resources", ".*\\.resources?\\..*")
.put("event_handlers", ".*\\.messaging\\..*")
.put("datasources", "io\\.dropwizard\\.db\\.ManagedPooledDataSource.*")
.put("clients", "org\\.apache\\.http\\.client\\.HttpClient.*")
.put("client_connections", "org\\.apache\\.http\\.conn\\.HttpClientConnectionManager.*")
.put("connections", "org\\.eclipse\\.jetty\\.server\\.HttpConnectionFactory.*")
.put("thread_pools", "org\\.eclipse\\.jetty\\.util\\.thread\\.QueuedThreadPool.*")
.put("logs", "ch\\.qos\\.logback\\.core\\.Appender.*")
.put("http_server", "io\\.dropwizard\\.jetty\\.MutableServletContextHandler.*")
.put("raw_sql", "org\\.skife\\.jdbi\\.v2\\.DBI\\.raw-sql")
.put("jvm", "^jvm$")
.put("jvm_attribute", "jvm\\.attribute.*?")
.put("jvm_buffers", "jvm\\.buffers\\..*")
.put("jvm_classloader", "jvm\\.classloader.*")
.put("jvm_gc", "jvm\\.gc\\..*")
.put("jvm_memory", "jvm\\.memory\\..*")
.put("jvm_threads", "jvm\\.threads.*")
.build();
private ImmutableSet excludes = ImmutableSet.builder()
.add("ch.qos.logback.core.Appender.debug")
.add("ch.qos.logback.core.Appender.trace")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-4xx-15m")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-4xx-1m")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-4xx-5m")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-5xx-15m")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-5xx-1m")
.add("io.dropwizard.jetty.MutableServletContextHandler.percent-5xx-5m")
.add("jvm.attribute.name")
.add("jvm.attribute.vendor")
.add("jvm.memory.heap.usage")
.add("jvm.memory.non-heap.usage")
.add("jvm.memory.pools.Code-Cache.usage")
.add("jvm.memory.pools.Compressed-Class-Space.usage")
.add("jvm.memory.pools.Metaspace.usage")
.add("jvm.memory.pools.PS-Eden-Space.usage")
.add("jvm.memory.pools.PS-Old-Gen.usage")
.add("jvm.memory.pools.PS-Survivor-Space.usage")
.build();
@JsonProperty
public String getProtocol() {
return protocol;
}
@JsonProperty
public void setProtocol(String protocol) {
this.protocol = protocol;
}
@JsonProperty
public String getHost() {
return host;
}
@JsonProperty
public void setHost(String host) {
this.host = host;
}
@JsonProperty
public int getPort() {
return port;
}
@JsonProperty
public void setPort(int port) {
this.port = port;
}
@JsonProperty
public String getPrefix() {
return prefix;
}
@JsonProperty
public void setPrefix(String prefix) {
this.prefix = prefix;
}
@JsonProperty
private Map getTags() {
return tags;
}
@JsonProperty
public void setTags(Map tags) {
this.tags = tags;
}
@JsonProperty
public ImmutableMap> getFields() {
return fields;
}
@JsonProperty
public void setFields(ImmutableMap> fields) {
this.fields = fields;
}
@JsonProperty
public String getDatabase() {
return database;
}
@JsonProperty
public void setDatabase(String database) {
this.database = database;
}
@JsonProperty
public String getAuth() {
return auth;
}
@JsonProperty
public void setAuth(String auth) {
this.auth = auth;
}
@JsonProperty
public int getConnectTimeout() {
return connectTimeout;
}
@JsonProperty
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
@JsonProperty
public int getReadTimeout() {
return readTimeout;
}
@JsonProperty
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
@JsonProperty
public boolean getGroupGauges() {
return groupGauges;
}
@JsonProperty
public void setGroupGauges(boolean groupGauges) {
this.groupGauges = groupGauges;
}
@JsonProperty
public Duration getPrecision() {
return precision;
}
@JsonProperty
public void setPrecision(Duration precision) {
this.precision = precision;
}
@JsonProperty
public Map getMeasurementMappings() {
return measurementMappings;
}
@JsonProperty
public void setMeasurementMappings(ImmutableMap measurementMappings) {
this.measurementMappings = measurementMappings;
}
@JsonProperty
public Map getDefaultMeasurementMappings() {
return defaultMeasurementMappings;
}
@JsonProperty
@Override
public ImmutableSet getExcludes() {
return this.excludes;
}
@JsonProperty
@Override
public void setExcludes(ImmutableSet excludes) {
this.excludes = excludes;
}
@JsonProperty
public void setDefaultMeasurementMappings(ImmutableMap defaultMeasurementMappings) {
this.defaultMeasurementMappings = defaultMeasurementMappings;
}
@JsonProperty
public void setSenderType(SenderType senderType) {
this.senderType = senderType;
}
@JsonProperty
public SenderType getSenderType() {
return senderType;
}
@Override
public ScheduledReporter build(MetricRegistry registry) {
try {
InfluxDbReporter.Builder builder = builder(registry);
switch (senderType) {
case HTTP:
return builder.build(
new InfluxDbHttpSender(
protocol,
host,
port,
database,
auth,
precision.getUnit(),
connectTimeout,
readTimeout
)
);
case TCP:
return builder.build(
new InfluxDbTcpSender(
host,
port,
readTimeout,
database,
precision.getUnit()
)
);
case UDP:
return builder.build(
new InfluxDbUdpSender(
host,
port,
readTimeout,
database,
precision.getUnit()
)
);
default:
throw new UnsupportedDataTypeException("The Sender Type is not supported. ");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected Map buildMeasurementMappings() {
Map mappings = new HashMap<>(defaultMeasurementMappings);
for (Map.Entry entry : measurementMappings.entrySet()) {
String mappingKey = entry.getKey();
String mappingValue = entry.getValue();
if (mappingValue.isEmpty()) {
mappings.remove(mappingKey);
continue;
}
mappings.put(mappingKey, mappingValue);
}
return mappings;
}
@ValidationMethod(message = "measurementMappings must be regular expressions")
public boolean isMeasurementMappingRegularExpressions() {
for (Map.Entry entry : buildMeasurementMappings().entrySet()) {
try {
Pattern.compile(entry.getValue());
} catch (PatternSyntaxException e) {
return false;
}
}
return true;
}
@VisibleForTesting
protected InfluxDbReporter.Builder builder(MetricRegistry registry) {
return InfluxDbReporter.forRegistry(registry)
.convertDurationsTo(getDurationUnit())
.convertRatesTo(getRateUnit())
.includeMeterFields(fields.get("meters"))
.includeTimerFields(fields.get("timers"))
.filter(getFilter())
.groupGauges(getGroupGauges())
.withTags(getTags())
.measurementMappings(buildMeasurementMappings());
}
}