Spring 启动指标 + datadog

Spring boot metrics + datadog

有人知道如何将 Spring 引导指标与 datadog 集成吗?

Datadog 是一项针对 IT 的云级监控服务。

它允许用户使用大量图表和图形轻松查看他们的数据。

我有一个 spring 启动应用程序,它使用 dropwizard 指标来填充有关我用 @Timed.

注释的所有方法的大量信息

另一方面,我正在 heroku 中部署我的应用程序,所以我无法安装 Datadog 代理。

我想知道是否有办法自动集成 spring 引导指标系统报告与 datadog。

我终于找到了一个将这个库与 datadog 集成的 dropwizzard 模块:metrics-datadog

我已经创建了一个 Spring 配置 class,它使用我的 YAML 的属性创建并初始化了这个 Reporter。

只需在您的 pom 中插入此依赖项:

    <!-- Send metrics to Datadog -->
    <dependency>
        <groupId>org.coursera</groupId>
        <artifactId>dropwizard-metrics-datadog</artifactId>
        <version>1.1.3</version>
    </dependency>

将此配置添加到您的 YAML:

yourapp:
  metrics:
    apiKey: <your API key>
    host: <your host>
    period: 10
    enabled: true

并将此配置 class 添加到您的项目中:

/**
 * 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;
  }
}

如果 JMX 适合您,您可以使用 JMX dropwizrd reporter combined with java datalog integration

Spring Boot 2.x 似乎在其指标中添加了几个监控系统。 DataDog 是 micrometer.io. See reference documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-metrics-export-newrelic

支持的其中之一

对于Spring启动1.x,你可以使用back-ported包:

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