7

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?

G Lor
  • 205
  • 2
  • 3
  • 10
  • 3
    Does this answer your question? [@Timed annotation in spring metrics](https://stackoverflow.com/questions/51749391/timed-annotation-in-spring-metrics) – Martin Schröder Feb 10 '21 at 10:39

1 Answers1

19

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.

jolo
  • 751
  • 4
  • 6
  • You also need spring-aspects dependency: https://mvnrepository.com/artifact/org.springframework/spring-aspects/. Also see https://stackoverflow.com/a/63233672/432903 – prayagupa Nov 04 '22 at 18:35