15

Does anyone know how to integrate Spring boot metrics with datadog?

Datadog is a cloud-scale monitoring service for IT.

It allows users to easily visualice their data using a lot of charts and graphs.

I have a spring boot application that is using dropwizard metrics to populate a lot of information about all methods I annotated with @Timed.

On the other hand I'm deploying my application in heroku so I can't install a Datadog agent.

I want to know if there is a way to automatically integrate spring boot metric system reporting with datadog.

jfcorugedo
  • 9,793
  • 8
  • 39
  • 47

3 Answers3

11

I've finally found a dropwizzard module that integrates this library with datadog: metrics-datadog

I've created a Spring configuration class that creates and initializes this Reporter using properties of my YAML.

Just insert this dependency in your pom:

    <!-- Send metrics to Datadog -->
    <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:

/**
 * This bean will create and configure a DatadogReporter that will be in charge of sending
 * all the metrics collected by Spring Boot actuator system to Datadog.
 *     
 * @see https://www.datadoghq.com/
 * @author jfcorugedo
 *
 */
@Configuration
@ConfigurationProperties("yourapp.metrics")
public class DatadogReporterConfig {

  private static final Logger LOGGER = LoggerFactory.getLogger(DatadogReporterConfig.class);

  /** Datadog API key used to authenticate every request to Datadog API */
  private String apiKey;

  /** Logical name associated to all the events send by this application */
  private String host;

  /** Time, in seconds, between every call to Datadog API. The lower this value the more information will be send to Datadog */
  private long period;

  /** This flag enables or disables the datadog reporter */
  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;
  }

  /**
   * @return Datadog API key used to authenticate every request to Datadog API
   */
  public String getApiKey() {
      return apiKey;
  }

  /**
   * @param apiKey Datadog API key used to authenticate every request to Datadog API
   */
  public void setApiKey(String apiKey) {
      this.apiKey = apiKey;
  }

  /**
   * @return Logical name associated to all the events send by this application
   */
  public String getHost() {
      return host;
  }

  /**
   * @param host Logical name associated to all the events send by this application
   */
  public void setHost(String host) {
      this.host = host;
  }

  /**
   * @return Time, in seconds, between every call to Datadog API. The lower this value the more information will be send to Datadog
   */
  public long getPeriod() {
      return period;
  }

  /**
   * @param period Time, in seconds, between every call to Datadog API. The lower this value the more information will be send to Datadog
   */
  public void setPeriod(long period) {
      this.period = period;
  }

  /**
   * @return true if DatadogReporter is enabled in this application
   */
  public boolean isEnabled() {
      return enabled;
  }

  /**
   * This flag enables or disables the datadog reporter.
   * This flag is only read during initialization, subsequent changes on this value will no take effect 
   * @param enabled
   */
  public void setEnabled(boolean enabled) {
      this.enabled = enabled;
  }
}
jfcorugedo
  • 9,793
  • 8
  • 39
  • 47
  • 1
    It is also possible to use the same library for StatsD reporting, instead of HTTP, should anybody be interested in that option. – Yonatan Wilkof Sep 18 '16 at 14:59
  • Do you need to write a scheduler, because adding given resources is not working out for me. – Shek Feb 22 '18 at 23:57
7

It seems that Spring Boot 2.x added several monitoring system into its metrics. DataDog is one of them supported by micrometer.io. See reference documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-metrics-export-newrelic

For Spring Boot 1.x, you can use back-ported package:

compile 'io.micrometer:micrometer-spring-legacy:latest.release'

kuceram
  • 3,795
  • 9
  • 34
  • 54
  • Hey is spring boot 2 working with datadog agent 5? since I have upgraded my application to spring boot 2 and now I don't get any metric in my datadog! do I need to change anything? I have heard datadog doesnt support netty I have problem with APM metrics – Am1rr3zA May 11 '18 at 15:17
  • same problem here... spring boot 2 apps says it submits only jdbc and pool metrics and even those i cant see in the datadog gui. – Logemann Sep 02 '18 at 22:31
  • @Marc, do you have management.metrics.export.datadog.enabled set to true? You might need to implement a metrics configuration and a meter service – Jolley71717 Feb 12 '19 at 00:06
2

If JMX is an option for you, you may use the JMX dropwizrd reporter combined with java datalog integration

Guy Bouallet
  • 2,099
  • 11
  • 16
  • That's a good point. However I'm deploying my app in heroku, so I can't install a datadog agent in the OS – jfcorugedo Dec 21 '15 at 17:07
  • 1
    @jfcorugedo I don't think the question mentioned anything about heroku? If heroku solution is needed, you'll need to add it to the question. – eis Dec 21 '15 at 17:15
  • There appears to be a good economic argument for this way too - you get 350 metrics per instance for free. If your application pumps all it's metrics into DataDog (using `dropwizard-metrics-datadog` for instance) those metrics count toward your custom use limit - using the integration significantly lessens the impact. – jmkgreen Sep 27 '16 at 08:05