after upgrade to Spring Boot 2, how to expose cache metrics to prometheus?

前端 未结 1 731
刺人心
刺人心 2021-01-12 12:54

I recently upgraded a spring boot application from 1.5 to 2.0.1. I also migrated the prometheus integration to the new actuator approach using micrometer. Most things work n

1条回答
  •  不思量自难忘°
    2021-01-12 13:23

    As you've answered my question, I can provide an answer for this.

    my caches get created through scheduled tasks later on

    Then this section of the doc applies to you:

    Only caches that are available on startup are bound to the registry. For caches created on-the-fly or programmatically after the startup phase, an explicit registration is required. A CacheMetricsRegistrar bean is made available to make that process easier.

    So you have to register such caches yourself, hopefully it is pretty easy, something like:

    public class MyComponent {
    
        private final CacheMetricsRegistrar cacheMetricsRegistrar;
        private final CacheManager cacheManager
    
        public MyComponent(CacheMetricsRegistrar cacheMetricsRegistrar,
                    CacheManager cacheManager) { ... }
    
        public void register() {
             // you have just registered cache "xyz"
             Cache xyz = this.cacheManager.getCache("xyz");
             this.cacheMetricsRegistrar.bindCacheToRegistry(xyz);
        }
    
    }
    

    you can include this code in your existing code. If you don't want to do that, then you need something else that runs after your existing code to register those caches to the registry.

    0 讨论(0)
提交回复
热议问题