java Spring 启动指标 + 数据狗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34398692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot metrics + datadog
提问by jfcorugedo
Does anyone know how to integrate Spring boot metrics with datadog?
有谁知道如何将 Spring 启动指标与 datadog 集成?
Datadogis a cloud-scale monitoring service for IT.
Datadog是一种面向 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 dropwizardmetrics to populate a lot of information about all methods I annotated with @Timed
.
我有一个 Spring Boot 应用程序,它使用dropwizard指标来填充有关我用@Timed
.
On the other hand I'm deploying my application in heroku so I can't install a Datadog agent.
另一方面,我在 heroku 中部署我的应用程序,所以我无法安装 Datadog 代理。
I want to know if there is a way to automatically integrate spring boot metric system reporting with datadog.
我想知道是否有一种方法可以将spring boot metric system报告与datadog自动集成。
回答by jfcorugedo
I've finally found a dropwizzard module that integrates this library with datadog: metrics-datadog
我终于找到了一个将这个库与 datadog 集成的 dropwizzard 模块:metrics-datadog
I've created a Spring configuration class that creates and initializes this Reporter using properties of my YAML.
我创建了一个 Spring 配置类,它使用我的 YAML 的属性创建和初始化这个 Reporter。
Just insert this dependency in your pom:
只需在你的 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:
将此配置添加到您的 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;
}
}
回答by kuceram
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
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
For Spring Boot 1.x, you can use back-ported package:
对于 Spring Boot 1.x,您可以使用向后移植的包:
compile 'io.micrometer:micrometer-spring-legacy:latest.release'
compile 'io.micrometer:micrometer-spring-legacy:latest.release'
回答by Guy Bouallet
If JMX is an option for you, you may use the JMX dropwizrd reportercombined with java datalog integration
如果 JMX 是您的选择,您可以使用JMX dropwizrd 报告器与Java 数据记录集成相结合