diff --git a/pom.xml b/pom.xml index d2f9e00..97717f9 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,12 @@ 1.9.0 test + + io.dropwizard + dropwizard-metrics + 0.7.0 + provided + diff --git a/src/main/java/com/bealetech/metrics/reporting/StatsdReporterFactory.java b/src/main/java/com/bealetech/metrics/reporting/StatsdReporterFactory.java new file mode 100644 index 0000000..7b8b482 --- /dev/null +++ b/src/main/java/com/bealetech/metrics/reporting/StatsdReporterFactory.java @@ -0,0 +1,97 @@ +package com.bealetech.metrics.reporting; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.ScheduledReporter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; +import io.dropwizard.metrics.BaseReporterFactory; +import org.hibernate.validator.constraints.NotEmpty; +import org.hibernate.validator.constraints.Range; + +import javax.validation.constraints.NotNull; + +/** + * A factory for {@link com.bealetech.metrics.reporting.StatsdReporter} instances. + *

+ * Configuration Parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
NameDefaultDescription
hostlocalhostThe hostname of the Statsd server to report to.
port8080The port of the Statsd server to report to.
prefixNoneThe prefix for Metric key names to report to Statsd.
+ */ +@JsonTypeName("statsd") +public class StatsdReporterFactory extends BaseReporterFactory { + @NotEmpty + private String host = "localhost"; + + @Range(min = 0, max = 49151) + private int port = 8080; + + @NotNull + private String prefix = ""; + + @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; + } + + /** + * Configures and builds a {@link com.codahale.metrics.ScheduledReporter} instance for the given registry. + * + * @param registry the metrics registry to report metrics from. + * @return a reporter configured for the given metrics registry. + */ + @Override + public ScheduledReporter build(MetricRegistry registry) { + Statsd statsd = new Statsd(host, port); + return StatsdReporter.forRegistry(registry) + .prefixedWith(prefix) + .convertDurationsTo(getDurationUnit()) + .convertRatesTo(getRateUnit()) + .filter(getFilter()) + .build(statsd); + } +} diff --git a/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory b/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory new file mode 100644 index 0000000..f310f65 --- /dev/null +++ b/src/main/resources/META-INF/services/io.dropwizard.metrics.ReporterFactory @@ -0,0 +1 @@ +com.bealetech.metrics.reporting.StatsdReporterFactory \ No newline at end of file