Search code examples
spring-bootmetricscodahale-metricsspring-boot-actuator

How to have spring boot metrics export averages and absolute values to csv


I need to export counters and gauges to csv in order to process them later on. By using gradle I get the jars for codahale metrics 3.1.2:

compile('io.dropwizard.metrics:metrics-core:${metricsVersion}' )
compile('io.dropwizard.metrics:metrics-annotations:${metricsVersion}' )

For the csv export I created one reporter using these lines of code:

@Configuration
public class MetricsConfiguration {
    @Autowired
    MetricRegistry metricRegistry;

    @Bean
    public CsvReporter configureReporters() {
        CsvReporter reporter = CsvReporter.forRegistry(metricRegistry).build(new File("C:/temp/metrics"));
        reporter.start(5, TimeUnit.SECONDS);

        return reporter;
    }
}

I can see files are created and they contain a timestamp and value (in this example the values were set for a gauge):

1444137261,42.0
1444137266,42.0
1444137271,42.0
1444137276,1.0
1444137281,1.0
1444137286,1.0

The only problem with this is that the file repeats the last value set until it is overwritten by me using gaugeService.submit(). In this scenario I set a gauge value of 42.0 once, waited several minutes and then set a new value of 1.0.

This makes it difficult to parse the csv and create averages on my own or create histograms because I don't know if the 42.0 was submitted once or three times.

I had a look at these SO posts but they didn't help me solve my problem:


Solution

  • All of this was based on a misunderstanding from my side: the metrics offered by Spring Boot only allow to use a gauge or counters. For both no averages are calculated.

    In order to access what I need I have to gain access to the underlying Codahale metrics registry:

    @Autowired
    MetricRegistry metricRegistry;
    

    Then I can start and stop a timer in my code:

    Context timer = metricRegistry.timer("foo").time();
    // do whatever needs to be timed
    timer.stop();
    

    These values will be exported by the CsvReporter in a file named timer.foo.csv which contains these values:

    t,count,max,mean,min,stddev,p50,p75,p95,p98,p99,p999,mean_rate,m1_rate,m5_rate,m15_rate,rate_unit,duration_unit
    1446036738,5,18,597084,14,172184,12,832516,2,145484,13,280135,13,445525,18,597084,18,597084,18,597084,18,597084,0,215907,0,494353,0,577132,0,592274,calls/second,milliseconds
    1446036798,5,18,597084,14,172184,12,832516,2,145484,13,280135,13,445525,18,597084,18,597084,18,597084,18,597084,0,060092,0,181862,0,472516,0,554077,calls/second,milliseconds
    1446036858,5,18,597084,14,172184,12,832516,2,145484,13,280135,13,445525,18,597084,18,597084,18,597084,18,597084,0,034923,0,066903,0,386863,0,518343,calls/second,milliseconds
    1446036918,5,18,597084,14,172184,12,832516,2,145484,13,280135,13,445525,18,597084,18,597084,18,597084,18,597084,0,024578,0,024612,0,316737,0,484913,calls/second,milliseconds