spring - 有没有办法将 Micrometer @Timed 注释捕获到 Prometheus 指标存储/注册表中?

标签 spring prometheus metrics micrometer spring-micrometer

我想将 Micrometer 的 @Timed 注释中的数据捕获到 Prometheus 指标存储/注册表中。我在网上找不到任何关于如何执行此操作的说明。

这些是我正在使用的版本:

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'

我正在尝试计算存储库调用的时间:

interface HouseRepository { 

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

}

我该怎么做?我尝试向@Timer注释添加一些不同的配置,例如:

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

但我在 Prometheus (/prometheus) 中看不到我的输出。

这当然可以做到吗?

最佳答案

根据micrometer docs :

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

要使 @Timed 注释按您希望的方式工作,您可能需要配置一个 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.

关于spring - 有没有办法将 Micrometer @Timed 注释捕获到 Prometheus 指标存储/注册表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56533986/

相关文章:

prometheus - Grafana 标签按指标结果过滤

methodology - 测量代码可重用性

java - 两次调用 Spring AOP 建议

java - Spring MVC : the difference between DeferredResult and ListenableFuture?

java - 记录 Spring bean 创建/依赖注入(inject)

spring-boot - http_server_requests_seconds_max 在 Micrometer 中的确切含义

kubernetes - 裸机(Bare Metal)上的Thanos

azure - 无法为指标创建 azure 警报

c++ - 使用 Doxygen 生成项目指标?

java - @target 和 @within 之间的区别(Spring AOP)