Skip to main content
 首页 » 编程设计

spring-boot之将Spring Boot执行器指标(和Dropwizard指标)导出到Statsd

2025年05月04日28over140

我正在尝试将端点/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); 
    } 
} 

它将写入我已添加到Statsd的所有指标,但是我还要发送在 /metrics端点上可见的系统/JVM指标。

我想念什么?

请您参考如下方法:

我遇到了同样的问题,并在这里找到了解决方案:https://github.com/tzolov/export-metrics-example

只需将MetricsEndpointMetricReader添加到您的配置中,e/metrics端点上可用的所有内容都将发布到StatsdMetricWriter

这是Spring Boot 1.3.x和dropwizardmetrics-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); 
  } 
 
}