Ton*_*ita 9 spring-boot spring-boot-actuator
我正在尝试将端点上可见的所有指标导出/metrics到StatsdMetricWriter.
到目前为止,我有以下配置类:
package com.tonyghita.metricsdriven.service.config;
import com.codahale.metrics.MetricRegistry;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.ExportMetricReader;
import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.actuate.metrics.reader.MetricRegistryMetricReader;
import org.springframework.boot.actuate.metrics.statsd.StatsdMetricWriter;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableMetrics(proxyTargetClass = true)
public class MetricsConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(MetricsConfig.class);
@Value("${statsd.host:localhost}")
private String host = "localhost";
@Value("${statsd.port:8125}")
private int port;
@Autowired
private MetricRegistry metricRegistry;
@Bean
@ExportMetricReader
public MetricReader metricReader() {
return new MetricRegistryMetricReader(metricRegistry);
}
@Bean
@ExportMetricWriter
public MetricWriter metricWriter() {
LOGGER.info("Configuring StatsdMetricWriter to export to {}:{}", host, port);
return new StatsdMetricWriter(host, port);
}
}
Run Code Online (Sandbox Code Playgroud)
其中列出了我已添加到Statsd的所有指标,但我还要发送在/metrics端点上可见的系统/ JVM指标.
我错过了什么?
我遇到了同样的问题,并在此处找到了解决方案:https://github.com/tzolov/export-metrics-example
只需MetricsEndpointMetricReader在您的配置中添加一个,并且/ e metrics端点上的所有可用内容都将发布到StatsdMetricWriter.
以下是spring boot 1.3.x和dropwizard metrics-jvm 3.1.x的完整示例配置:
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.MetricsEndpointMetricReader;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.statsd.StatsdMetricWriter;
import org.springframework.boot.actuate.metrics.writer.Delta;
import org.springframework.boot.actuate.metrics.writer.MetricWriter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MetricsConfiguration {
@Bean
public MetricRegistry metricRegistry() {
final MetricRegistry metricRegistry = new MetricRegistry();
metricRegistry.register("jvm.memory",new MemoryUsageGaugeSet());
metricRegistry.register("jvm.thread-states",new ThreadStatesGaugeSet());
metricRegistry.register("jvm.garbage-collector",new GarbageCollectorMetricSet());
return metricRegistry;
}
/*
* Reading all metrics that appear on the /metrics endpoint to expose them to metrics writer beans.
*/
@Bean
public MetricsEndpointMetricReader metricsEndpointMetricReader(final MetricsEndpoint metricsEndpoint) {
return new MetricsEndpointMetricReader(metricsEndpoint);
}
@Bean
@ConditionalOnProperty(prefix = "statsd", name = {"prefix", "host", "port"})
@ExportMetricWriter
public MetricWriter statsdMetricWriter(@Value("${statsd.prefix}") String statsdPrefix,
@Value("${statsd.host}") String statsdHost,
@Value("${statsd.port}") int statsdPort) {
return new StatsdMetricWriter(statsdPrefix, statsdHost, statsdPort);
}
}
Run Code Online (Sandbox Code Playgroud)
从我在spring-boot代码中看到的,只有调用CounterService和GaugeService实现被转发到dropwizard MetricRegistry.
因此,你已经观察到的,只有counter.*和gauge.*从指标/metrics端点将在结束了Statsd.
系统和JVM指标通过自定义SystemPublicMetrics类公开,该类不使用计数器或计量服务.
我不确定是否有一个更简单的解决方案(可能是Spring团队的某个人会评论),但是一种方法(不是特定于spring-boot)将使用定期将系统统计信息写入的计划任务MetricRegistry.
要注册JVM指标,您可以使用codehale.metrics.jvm库提供的与JVM相关的MetricSet.您可以添加整套,而无需提供它们是仪表还是计数器.
这是我的示例代码,我正在注册jvm相关指标:
@Configuration
@EnableMetrics(proxyTargetClass = true)
public class MetricsConfig {
@Autowired
private StatsdProperties statsdProperties;
@Autowired
private MetricsEndpoint metricsEndpoint;
@Autowired
private DataSourcePublicMetrics dataSourcePublicMetrics;
@Bean
@ExportMetricReader
public MetricReader metricReader() {
return new MetricRegistryMetricReader(metricRegistry());
}
public MetricRegistry metricRegistry() {
final MetricRegistry metricRegistry = new MetricRegistry();
//jvm metrics
metricRegistry.register("jvm.gc",new GarbageCollectorMetricSet());
metricRegistry.register("jvm.mem",new MemoryUsageGaugeSet());
metricRegistry.register("jvm.thread-states",new ThreadStatesGaugeSet());
return metricRegistry;
}
@Bean
@ConditionalOnProperty(prefix = "metrics.writer.statsd", name = {"host", "port"})
@ExportMetricWriter
public MetricWriter statsdMetricWriter() {
return new StatsdMetricWriter(
statsdProperties.getPrefix(),
statsdProperties.getHost(),
statsdProperties.getPort()
);
}
Run Code Online (Sandbox Code Playgroud)
}
注意:我使用的是spring boot 1.3.0.M4版
| 归档时间: |
|
| 查看次数: |
15262 次 |
| 最近记录: |