I finally found a dropwizzard module that integrates this library with datadog: metrics-datadog
I created a Spring configuration class that creates and initializes this reporter using the properties of my YAML.
Just paste this dependency into your pom:
<dependency> <groupId>org.coursera</groupId> <artifactId>dropwizard-metrics-datadog</artifactId> <version>1.1.3</version> </dependency>
Add this configuration to your YAML:
yourapp: metrics: apiKey: <your API key> host: <your host> period: 10 enabled: true
and add this configuration class to your project:
@Configuration @ConfigurationProperties("yourapp.metrics") public class DatadogReporterConfig { private static final Logger LOGGER = LoggerFactory.getLogger(DatadogReporterConfig.class); private String apiKey; private String host; private long period; private boolean enabled = false; @Bean @Autowired public DatadogReporter datadogReporter(MetricRegistry registry) { DatadogReporter reporter = null; if(enabled) { reporter = enableDatadogMetrics(registry); } else { if(LOGGER.isWarnEnabled()) { LOGGER.info("Datadog reporter is disabled. To turn on this feature just set 'rJavaServer.metrics.enabled:true' in your config file (property or YAML)"); } } return reporter; } private DatadogReporter enableDatadogMetrics(MetricRegistry registry) { if(LOGGER.isInfoEnabled()) { LOGGER.info("Initializing Datadog reporter using [ host: {}, period(seconds):{}, api-key:{} ]", getHost(), getPeriod(), getApiKey()); } EnumSet<Expansion> expansions = DatadogReporter.Expansion.ALL; HttpTransport httpTransport = new HttpTransport .Builder() .withApiKey(getApiKey()) .build(); DatadogReporter reporter = DatadogReporter.forRegistry(registry) .withHost(getHost()) .withTransport(httpTransport) .withExpansions(expansions) .build(); reporter.start(getPeriod(), TimeUnit.SECONDS); if(LOGGER.isInfoEnabled()) { LOGGER.info("Datadog reporter successfully initialized"); } return reporter; } public String getApiKey() { return apiKey; } public void setApiKey(String apiKey) { this.apiKey = apiKey; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public long getPeriod() { return period; } public void setPeriod(long period) { this.period = period; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } }
source share