当前位置:  开发笔记 > 编程语言 > 正文

Spring启动指标+ datadog

如何解决《Spring启动指标+datadog》经验,为你挑选了2个好方法。

有谁知道如何将Spring启动指标与datadog集成?

Datadog是一种面向IT的云规模监控服务.

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

我有一个spring boot应用程序,它使用dropwizard指标填充有关我注释的所有方法的大量信息@Timed.

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

我想知道是否有一种方法可以自动将spring boot metric系统报告与datadog集成.



1> jfcorugedo..:

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

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

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

    
    
        org.coursera
        dropwizard-metrics-datadog
        1.1.3
    

将此配置添加到YAML:

yourapp:
  metrics:
    apiKey: 
    host: 
    period: 10
    enabled: true

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

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



2> kuceram..:

似乎Spring Boot 2.x在其指标中添加了几个监视系统。DataDog是micrometer.io支持的其中之一。请参阅参考文档:https : //docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-metrics-export-newrelic

对于Spring Boot 1.x,可以使用反向移植的软件包:

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

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有