如何修复在应用程序启动时未加载的 micronaut micrometer bean

How to fix a micronaut micrometer bean that is not loaded at application startup

A 创建了一个公开千分尺指标的简单 micronaut 应用程序。我想添加自定义标签,但执行此操作的 bean 未在启动时加载。

我错过了什么? 备注:使用intellj启动时在本地pc上不是这样。但是部署在k8s上确实有效。

豆子:

@Factory
open class MeterFilterFactory {

    @Bean
    @Singleton
    fun addCommonTags(): MeterFilter {
        return MeterFilter.commonTags(
            Arrays.asList(
                Tag.of("service", "my-super-service"),
                Tag.of("special", "tag comes here")
            )
        )
    }
}

应用配置

micronaut:
  metrics:
    enabled: true
    sensitive: true
    export:
      prometheus:
        enabled: true
        step: PT1M
        descriptions: true
  distribution:
    percentiles-histogram:
      http.server.requests: true
    sla:
      http.server.requests: 1ms,5ms
  binders:
    logback:
      enabled: false
    processor:
      enabled: false
    uptime:
      enabled: false

建设gradle

    compile "io.micronaut.configuration:micronaut-micrometer-registry-statsd"
    compile "io.micronaut:micronaut-management"
    compile "io.micronaut.configuration:micronaut-micrometer-registry-prometheus"

目前我回来了

{"name":"system.cpu.usage","measurements":[{"statistic":"VALUE","value":0.07751937984496124}]}

但我错过了我尝试添加的标签。

如果用 @Context 标记 bean,则在初始化上下文时将初始化 bean。参见 https://docs.micronaut.io/1.0.5/api/io/micronaut/context/annotation/Context.html

希望对您有所帮助。

IntelliJ 缓存清除+重启解决了问题。