Exporting Spring Boot Actuator Metrics (& Dropwizard Metrics) to Statsd

前端 未结 4 452
慢半拍i
慢半拍i 2020-12-09 20:40

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

I\'ve got the following config

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 21:30

    To register JVM metrics you can use the JVM related MetricSets supplied by codehale.metrics.jvm library. You can just add the whole set without supplying whether they are gauges or counters.

    Here is my example code where I am registering jvm related metrics:

    @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()
        );
    }
    

    }

    Note: I am using spring boot version 1.3.0.M4

提交回复
热议问题