Configuring Spring Boot Actuator and ConcurrentMapCache
Hi there. This is the second article in a series of articles about Spring Boot Actuator.
In our project we have a need to cache some unalterable data, like a list of countries and so on.
For this purpose was selected simple ConcurrentMapCache which start by default if you use @EnableCaching and @Cacheable annotations.
If you just start your application and check metrics which provided by actuator you would’t find any metrics related to cache.
If we will look at the official documentation we will see that:
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.
ConcurrentMapCache
by itself doesn’t contains any metrics information. Therefore we have to implement our own version of this type of cache. For this we have to extends
ConcurrentMapCache and add counters to main cache methods. We will use AtomicLong types.
To bind our metrics to actuator we will extends
CacheMeterBinder
class.
A common base class for cache metrics that ensures that all caches are instrumented with the same basic set of metrics while allowing for additional detail that is specific to an individual implementation.
CacheMetricsRegistrar
by default requires two parameters:
MeterRegistry
- Collection of
CacheMeterBinderProvider
By default Spring Actuator contains only four types of providers — Caffeine, EhCache2, Hazelcast, JCache.
Let’s create our ConcurrentMapCacheBinderProvider
. We have to implement CacheMeterBinderProvider
interface and generify it by ConcurrentMapCacheMetricsWrapper
class.
Now, we have to override standard Spring Boot behaviour and create CacheManager
bean manually.
In this configuration we will get spring cache properties by ConfigurationProperties mechanism with “spring.cache” prefix to retrieve all cache names that we need to create.
Thank you for reading. It was a second article from series of articles about Spring Boot Actuator.
Full example with Spring Boot Web and Spring Boot Actuator integration you can find in my Github repo.