Search code examples
springprometheusmetricsmicrometerspring-micrometer

Is there a way to capture the Micrometer @Timed annotation into the Prometheus metrics store/registry?


I want to capture the data from Micrometer's @Timed annotation into the Prometheus metrics store/registry. I can't find any explanations on how to do this online.

These are the versions I am using:

compile 'org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE' // This already includes micrometer I believe
compile "io.micrometer:micrometer-registry-prometheus:1.1.4'

I'm trying to time a repository call:

interface HouseRepository { 

@Timed
@Query(some long complicated query)
House deleteByAddressAndLastModifiedBefore(
    Address address,
    Instant instant
)

}

How do I do this? I tried adding some different configs to the @Timer annotation, such as:

  @Timed(description = 'this.is.my.metric', value = 'my.metric', extraTags = ['my.metric.name', 'test'])

But I don't see my output in Prometheus (/prometheus).

Surely, this is possible to do?


Solution

  • According to the micrometer docs:

    Micrometer’s Spring Boot configuration does not recognize @Timed on arbitrary methods.

    To enable the @Timed annotation to work as you would like it to, you probably need to configure a TimedAspect bean.

    @Configuration
    public class TimedConfiguration {
       @Bean
       public TimedAspect timedAspect(MeterRegistry registry) {
          return new TimedAspect(registry);
       }
    }
    

    Applying TimedAspect makes @Timed usable on any arbitrary method.