将Spring Boot Actuator Metrics(& Dropwizard Metrics)导出到Statsd

时间:2015-08-25 23:27:25

标签: 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);
    }
}

其中列出了我已添加到Statsd的所有指标,但我还要发送/metrics端点上可见的系统/ JVM指标。

我错过了什么?

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,并在此处找到了解决方案: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);
  }

}

答案 1 :(得分:5)

要注册JVM指标,您可以使用codehale.metrics.jvm库提供的与JVM相关的MetricSets。你可以添加整套而不提供它们是仪表还是计数器。

以下是我在注册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()
    );
}

}

注意:我使用的是春季启动版本1.3.0.M4

答案 2 :(得分:4)

享受! (请参阅控制台中记录的公共指标作为dropwizard指标)

@Configuration
@EnableMetrics
@EnableScheduling
public class MetricsReporter extends MetricsConfigurerAdapter {

    @Autowired private SystemPublicMetrics systemPublicMetrics;
    private MetricRegistry metricRegistry;

    @Scheduled(fixedDelay = 5000)
    void exportPublicMetrics() {
        for (Metric<?> metric : systemPublicMetrics.metrics()) {
            Counter counter = metricRegistry.counter(metric.getName());
            counter.dec(counter.getCount());
            counter.inc(Double.valueOf(metric.getValue().toString()).longValue());
        }
    }

    @Override
    public void configureReporters(MetricRegistry metricRegistry) {
        this.metricRegistry = metricRegistry;
        ConsoleReporter.forRegistry(metricRegistry).build().start(10, TimeUnit.SECONDS);
    }

}