2
votes

I am trying to use prometheus to monitor some cache metrics from spring-boot. The cache is created with @Cacheable and my configuration is as follows:

management.endpoints:
  web.exposure.include: "*"
  metrics.enabled: true
  prometheus.enabled: true
management.metrics:
  export.prometheus.enabled: true
  cache.instrument: true

My cache is created with a simple @Cacheable('mycache') - I have no other cache code or setup. I'm also NOT using any specific cache provided just the built in one.

I do see my cache in the /actuator/caches/ list, but no detailed in either the /metrics or /prometheus endpoints.

My expectation was that some cache metrics would be published to both the /actuator/metrics and /actuator/prometheus endpoints.

I saw some notes about manually needing to register the cache, but I couldn't get that working either (nor am I sure it really pertains). When attempting to do this the issue is that I cannot autowire in the CacheMetricsRegistrar bean. It's not found.

3

3 Answers

2
votes

There isn't a Micrometer binder for the built in hashmap based cache. See https://github.com/micrometer-metrics/micrometer/tree/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache for the implementation created out of the box.

Those implementation keep track of their hit/miss counts themselves. Since nothing is tracking the hashmap isn't tracking metrics, there are none available to surface.

1
votes

It seems you got your property wrong, it should be endpoint you have and extra s

your property should be management.endpoint.prometheus.enabled

you can refer all actuators properties here

https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#actuator-properties

Edit: Endpoint property mentioned is as per Spring boot 2.2

Edit 2

While reading more about actuator cache, on this URL on #6.3.5. Cache Metrics, I found

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.

I further explored CacheMetricsRegistrar and at this URL, I found an example, which I implemented in my actuator sample project.

now I can see books cache details in Prometheus URL of application something like below

# TYPE cache_gets_total counter
cache_gets_total{cache="books",cacheManager="cacheManager",name="books",result="hit",} 4.0
cache_gets_total{cache="books",cacheManager="cacheManager",name="books",result="miss",} 2.0
0
votes

As checketts said, there are no metrics registered by micrometer for the simple implementation backed by a ConcurrentHashMap.

The cache libraries supporting metrics in Spring Boot are also listed there : https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-cache.

It also says

Only caches that are configured on startup are bound to the registry.

It means that you must declare the caches you want to monitor at startup, like this :

spring.cache.cache-names=books

You'll find this property in the application.properties, see : https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#common-application-properties-cache.

If you want to test, add for example Caffeine :

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

Then if the prometheus endpoint is activated, you'll see this for example :

# TYPE cache_size gauge
cache_size{cache="books",cacheManager="cacheManager",name="books",} 2.0

Code tested with Spring Boot 2.4