Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 now - including some custom counters and gauges.

I noted the new prometheus endpoint /actuator/prometheus does no longer publish the spring cache metrics (size and hit ratio).

The only thing I could find was this issue and its related commit.

Still I can't get cache metrics on the prometheus export. I tried settings some properties:

management.metrics.cache.instrument-cache=true
spring.cache.cache-names=cache1Name,cache2Name...

But nothing really works. I can see the Hazelcast cache manager starting up, registering the cache manager bean and so on - but neither /metrics nor /prometheus show any statistics. The caches are populated using the @Cacheable annotation. This worked with Spring Boot 1.5 - I think via Hazelcast exposing its metrics via JMX and the prometheus exporter picking it up from there?

Not sure now how to wire this together. Any hints are welcome!

like image 696
wemu Avatar asked Apr 06 '18 16:04

wemu


1 Answers

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.

like image 131
Stephane Nicoll Avatar answered Oct 23 '22 08:10

Stephane Nicoll