本文介绍了将 Spring Boot Actuator Metrics (& Dropwizard Metrics) 导出到 Statsd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将端点 /metrics 处可见的所有指标导出到 StatsdMetricWriter.

I'm trying to export all of the metrics which are visible at the endpoint /metrics to a StatsdMetricWriter.

到目前为止,我有以下配置类:

I've got the following configuration class so far:

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 指标.

Which writes all of the metrics which I've added to Statsd, but I'd like to also send the system/JVM metrics that are visible on the /metrics endpoint.

我错过了什么?

推荐答案

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

I had the same problem and found a solution here: https://github.com/tzolov/export-metrics-example

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

Just add a MetricsEndpointMetricReader to your config and everything available at th e/metrics endpoint will be published to the StatsdMetricWriter.

这是 spring boot 1.3.x 和 dropwizard metrics-jvm 3.1.x 的完整示例配置:

Here is a complete example config for spring boot 1.3.x and 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);
  }

}

这篇关于将 Spring Boot Actuator Metrics (& Dropwizard Metrics) 导出到 Statsd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:46