Display rich metrics in Spring Boot with Actuator

1.4k views Asked by At

I develop a Spring Boot app with version 1.2.4.RELEASE and want to measure some metrics data using Actuator. I'm getting some data at http://localhost:8080/metrics but I want rich data to be exposed there, like avg gauge times for my requests.

I found that a RichGauge class is contained in the Actuator plugin which does what I need but it doesn't seem to be used. I also couldn't find any information on google about how to achieve that.

Here is a snippet of my pom.xml with the spring dependencies:

<properties>
    <spring.boot.version>1.2.4.RELEASE</spring.boot.version>
</properties>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${spring.boot.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>${spring.boot.version}</version>
</dependency>

How can I measure rich metrics data with the actuator plugin? Is there some configuration for that or do I need some code to achieve that?

1

There are 1 answers

2
Lukas Hinsch On BEST ANSWER

I managed to get it running with the following code

@SpringBootApplication
public class SpringBootRichMetricsTestApplication {

    private final InMemoryMetricRepository counterMetricRepository = new InMemoryMetricRepository();

    @Bean
    @Primary
    public InMemoryRichGaugeRepository inMemoryRichGaugeRepository() {
        return new InMemoryRichGaugeRepository();
    }

    @Bean
    public CounterService counterService() {
        return new DefaultCounterService(counterMetricRepository);
    }

    // bean must not be named metricReaderPublicMetrics, one with that name already exists and the other one silently wins
    @Bean
    public MetricReaderPublicMetrics counterMetricReaderPublicMetrics() {
        return new MetricReaderPublicMetrics(counterMetricRepository);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRichMetricsTestApplication.class, args);
    }
}

Technically, the InMemoryRichGaugeRepository bean declaration alone should do the trick, but that implementation effectively disables the CounterService (replacing it with counts on anything reported to GaugeService) where only CounterService is used, so I found this workaround to get the old CounterService behaviour back.