Class CaffeineCacheMetrics

  • All Implemented Interfaces:
    MeterBinder

    @NonNullApi
    @NonNullFields
    public class CaffeineCacheMetrics
    extends CacheMeterBinder
    Collect metrics from Caffeine's com.github.benmanes.caffeine.cache.Cache.

    Note that `recordStats()` is required to gather non-zero statistics:

    
     Cache<String, String> cache = Caffeine.newBuilder().recordStats().build();
     CaffeineCacheMetrics.monitor(registry, cache, "mycache", "region", "test");
     

    • Constructor Summary

      Constructors 
      Constructor Description
      CaffeineCacheMetrics​(com.github.benmanes.caffeine.cache.Cache<?,​?> cache, java.lang.String cacheName, java.lang.Iterable<Tag> tags)
      Creates a new CaffeineCacheMetrics instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void bindImplementationSpecificMetrics​(MeterRegistry registry)
      Bind detailed metrics that are particular to the cache implementation, e.g.
      protected java.lang.Long evictionCount()  
      protected long hitCount()  
      protected java.lang.Long missCount()  
      static <C extends com.github.benmanes.caffeine.cache.Cache<?,​?>>
      C
      monitor​(MeterRegistry registry, C cache, java.lang.String cacheName, java.lang.Iterable<Tag> tags)
      Record metrics on a Caffeine cache.
      static <C extends com.github.benmanes.caffeine.cache.Cache<?,​?>>
      C
      monitor​(MeterRegistry registry, C cache, java.lang.String cacheName, java.lang.String... tags)
      Record metrics on a Caffeine cache.
      protected long putCount()
      The put mechanism is unimportant - this count applies to entries added to the cache according to a pre-defined load function such as exists in Guava/Caffeine caches as well as manual puts.
      protected java.lang.Long size()
      MOST cache implementations provide a means of retrieving the number of entries.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CaffeineCacheMetrics

        public CaffeineCacheMetrics​(com.github.benmanes.caffeine.cache.Cache<?,​?> cache,
                                    java.lang.String cacheName,
                                    java.lang.Iterable<Tag> tags)
        Creates a new CaffeineCacheMetrics instance.
        Parameters:
        cache - The cache to be instrumented. You must call Caffeine.recordStats() prior to building the cache for metrics to be recorded.
        cacheName - Will be used to tag metrics with "cache".
        tags - tags to apply to all recorded metrics.
    • Method Detail

      • monitor

        public static <C extends com.github.benmanes.caffeine.cache.Cache<?,​?>> C monitor​(MeterRegistry registry,
                                                                                                C cache,
                                                                                                java.lang.String cacheName,
                                                                                                java.lang.String... tags)
        Record metrics on a Caffeine cache. You must call Caffeine.recordStats() prior to building the cache for metrics to be recorded.
        Type Parameters:
        C - The cache type.
        Parameters:
        registry - The registry to bind metrics to.
        cache - The cache to instrument.
        cacheName - Will be used to tag metrics with "cache".
        tags - Tags to apply to all recorded metrics. Must be an even number of arguments representing key/value pairs of tags.
        Returns:
        The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way.
      • monitor

        public static <C extends com.github.benmanes.caffeine.cache.Cache<?,​?>> C monitor​(MeterRegistry registry,
                                                                                                C cache,
                                                                                                java.lang.String cacheName,
                                                                                                java.lang.Iterable<Tag> tags)
        Record metrics on a Caffeine cache. You must call Caffeine.recordStats() prior to building the cache for metrics to be recorded.
        Type Parameters:
        C - The cache type.
        Parameters:
        registry - The registry to bind metrics to.
        cache - The cache to instrument.
        cacheName - Will be used to tag metrics with "cache".
        tags - Tags to apply to all recorded metrics.
        Returns:
        The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way.
        See Also:
        CacheStats
      • monitor

        public static <C extends com.github.benmanes.caffeine.cache.AsyncCache<?,​?>> C monitor​(MeterRegistry registry,
                                                                                                     C cache,
                                                                                                     java.lang.String cacheName,
                                                                                                     java.lang.String... tags)
        Record metrics on a Caffeine cache. You must call Caffeine.recordStats() prior to building the cache for metrics to be recorded.
        Type Parameters:
        C - The cache type.
        Parameters:
        registry - The registry to bind metrics to.
        cache - The cache to instrument.
        cacheName - Will be used to tag metrics with "cache".
        tags - Tags to apply to all recorded metrics. Must be an even number of arguments representing key/value pairs of tags.
        Returns:
        The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way.
      • monitor

        public static <C extends com.github.benmanes.caffeine.cache.AsyncCache<?,​?>> C monitor​(MeterRegistry registry,
                                                                                                     C cache,
                                                                                                     java.lang.String cacheName,
                                                                                                     java.lang.Iterable<Tag> tags)
        Record metrics on a Caffeine cache. You must call Caffeine.recordStats() prior to building the cache for metrics to be recorded.
        Type Parameters:
        C - The cache type.
        Parameters:
        registry - The registry to bind metrics to.
        cache - The cache to instrument.
        cacheName - Will be used to tag metrics with "cache".
        tags - Tags to apply to all recorded metrics.
        Returns:
        The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way.
        See Also:
        CacheStats
      • size

        protected java.lang.Long size()
        Description copied from class: CacheMeterBinder
        MOST cache implementations provide a means of retrieving the number of entries. Even if
        Specified by:
        size in class CacheMeterBinder
        Returns:
        Total number of cache entries. This value may go up or down with puts, removes, and evictions. Returns null if the cache implementation does not provide a way to track cache size.
      • hitCount

        protected long hitCount()
        Specified by:
        hitCount in class CacheMeterBinder
        Returns:
        Get requests that resulted in a "hit" against an existing cache entry. Monotonically increasing hit count.
      • missCount

        protected java.lang.Long missCount()
        Specified by:
        missCount in class CacheMeterBinder
        Returns:
        Get requests that resulted in a "miss", or didn't match an existing cache entry. Monotonically increasing count. Returns null if the cache implementation does not provide a way to track miss count, especially in distributed caches.
      • evictionCount

        protected java.lang.Long evictionCount()
        Specified by:
        evictionCount in class CacheMeterBinder
        Returns:
        Total number of entries that have been evicted from the cache. Monotonically increasing eviction count. Returns null if the cache implementation does not support eviction, or does not provide a way to track the eviction count.
      • putCount

        protected long putCount()
        Description copied from class: CacheMeterBinder
        The put mechanism is unimportant - this count applies to entries added to the cache according to a pre-defined load function such as exists in Guava/Caffeine caches as well as manual puts.
        Specified by:
        putCount in class CacheMeterBinder
        Returns:
        Total number of entries added to the cache. Monotonically increasing count.
      • bindImplementationSpecificMetrics

        protected void bindImplementationSpecificMetrics​(MeterRegistry registry)
        Description copied from class: CacheMeterBinder
        Bind detailed metrics that are particular to the cache implementation, e.g. load duration for Caffeine caches, heap and disk size for EhCache caches. These metrics are above and beyond the basic set of metrics that is common to all caches.
        Specified by:
        bindImplementationSpecificMetrics in class CacheMeterBinder
        Parameters:
        registry - The registry to bind metrics to.