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 java.lang.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:
CaffeineCacheMetrics
  • Constructor Summary

    Constructors
    Constructor Description
    CaffeineStatsCounter​(MeterRegistry registry, java.lang.String cacheName)
    Constructs an instance for use by a single cache.
    CaffeineStatsCounter​(MeterRegistry registry, java.lang.String cacheName, java.lang.Iterable<Tag> extraTags)
    Constructs an instance for use by a single cache.
  • Method Summary

    Modifier and Type Method Description
    void recordEviction()  
    void recordEviction​(int weight, com.github.benmanes.caffeine.cache.RemovalCause cause)  
    void recordHits​(int count)  
    void recordLoadFailure​(long loadTime)  
    void recordLoadSuccess​(long loadTime)  
    void recordMisses​(int count)  
    void registerSizeMetric​(com.github.benmanes.caffeine.cache.Cache<?,​?> cache)
    Register a gauge for the size of the given cache.
    com.github.benmanes.caffeine.cache.stats.CacheStats snapshot()  
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.github.benmanes.caffeine.cache.stats.StatsCounter

    recordEviction
  • Constructor Details

    • CaffeineStatsCounter

      public CaffeineStatsCounter​(MeterRegistry registry, java.lang.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, java.lang.String cacheName, java.lang.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 java.lang.String toString()
      Overrides:
      toString in class java.lang.Object