Class CaffeineStatsCounter

java.lang.Object
io.micrometer.core.instrument.binder.cache.CaffeineStatsCounter
All Implemented Interfaces:
com.github.benmanes.caffeine.cache.stats.StatsCounter

@NonNullApi @NonNullFields public final class CaffeineStatsCounter extends Object implements com.github.benmanes.caffeine.cache.stats.StatsCounter
A StatsCounter instrumented with Micrometer. This will provide more detailed metrics than using CaffeineCacheMetrics.

Note that this doesn't instrument the cache's size by default. Use registerSizeMetric(Cache) to do so after the cache has been built.

Use Caffeine.recordStats() to supply this class to the cache builder:


 MeterRegistry registry = ...;
 Cache<Key, Graph> graphs = Caffeine.newBuilder()
     .maximumSize(10_000)
     .recordStats(() -> new CaffeineStatsCounter(registry, "graphs"))
     .build();
 
Since:
1.7.0
See Also:
  • Constructor Details

    • CaffeineStatsCounter

      public CaffeineStatsCounter(MeterRegistry registry, String cacheName)
      Constructs an instance for use by a single cache.
      Parameters:
      registry - the registry of metric instances
      cacheName - will be used to tag metrics with "cache".
    • CaffeineStatsCounter

      public CaffeineStatsCounter(MeterRegistry registry, String cacheName, Iterable<Tag> extraTags)
      Constructs an instance for use by a single cache.
      Parameters:
      registry - the registry of metric instances
      cacheName - will be used to tag metrics with "cache".
      extraTags - tags to apply to all recorded metrics.
  • Method Details

    • registerSizeMetric

      public void registerSizeMetric(com.github.benmanes.caffeine.cache.Cache<?,?> cache)
      Register a gauge for the size of the given cache.
      Parameters:
      cache - cache to register a gauge for its size
    • recordHits

      public void recordHits(int count)
      Specified by:
      recordHits in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • recordMisses

      public void recordMisses(int count)
      Specified by:
      recordMisses in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • recordLoadSuccess

      public void recordLoadSuccess(long loadTime)
      Specified by:
      recordLoadSuccess in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • recordLoadFailure

      public void recordLoadFailure(long loadTime)
      Specified by:
      recordLoadFailure in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • recordEviction

      public void recordEviction()
      Specified by:
      recordEviction in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • recordEviction

      public void recordEviction(int weight, com.github.benmanes.caffeine.cache.RemovalCause cause)
      Specified by:
      recordEviction in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • snapshot

      public com.github.benmanes.caffeine.cache.stats.CacheStats snapshot()
      Specified by:
      snapshot in interface com.github.benmanes.caffeine.cache.stats.StatsCounter
    • toString

      public String toString()
      Overrides:
      toString in class Object