All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics Maven / Gradle / Ivy

There is a newer version: 1.13.2
Show newest version
/**
 * Copyright 2017 Pivotal Software, Inc.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.micrometer.core.instrument.binder.cache; import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import com.github.benmanes.caffeine.cache.stats.CacheStats; import io.micrometer.core.instrument.*; import io.micrometer.core.lang.NonNullApi; import io.micrometer.core.lang.NonNullFields; import java.util.concurrent.TimeUnit; /** * Collect metrics from Caffeine's com.github.benmanes.caffeine.cache.Cache. *

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

{@code
 * Cache cache = Caffeine.newBuilder().recordStats().build();
 * CaffeineCacheMetrics.monitor(registry, cache, "mycache", "region", "test");
 * }
*

* * @author Clint Checketts */ @NonNullApi @NonNullFields public class CaffeineCacheMetrics extends CacheMeterBinder { private final Cache cache; /** * Creates a new {@link CaffeineCacheMetrics} instance. * * @param cache The cache to be instrumented. You must call {@link Caffeine#recordStats()} prior to building the cache * for metrics to be recorded. * @param cacheName Will be used to tag metrics with "cache". * @param tags tags to apply to all recorded metrics. */ public CaffeineCacheMetrics(Cache cache, String cacheName, Iterable tags) { super(cache, cacheName, tags); this.cache = cache; } /** * Record metrics on a Caffeine cache. You must call {@link Caffeine#recordStats()} prior to building the cache * for metrics to be recorded. * * @param registry The registry to bind metrics to. * @param cache The cache to instrument. * @param cacheName Will be used to tag metrics with "cache". * @param tags Tags to apply to all recorded metrics. Must be an even number of arguments representing key/value pairs of tags. * @param The cache type. * @return The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way. */ public static C monitor(MeterRegistry registry, C cache, String cacheName, String... tags) { return monitor(registry, cache, cacheName, Tags.of(tags)); } /** * Record metrics on a Caffeine cache. You must call {@link Caffeine#recordStats()} prior to building the cache * for metrics to be recorded. * * @param registry The registry to bind metrics to. * @param cache The cache to instrument. * @param cacheName Will be used to tag metrics with "cache". * @param tags Tags to apply to all recorded metrics. * @param The cache type. * @return The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way. * @see CacheStats */ public static C monitor(MeterRegistry registry, C cache, String cacheName, Iterable tags) { new CaffeineCacheMetrics(cache, cacheName, tags).bindTo(registry); return cache; } /** * Record metrics on a Caffeine cache. You must call {@link Caffeine#recordStats()} prior to building the cache * for metrics to be recorded. * * @param registry The registry to bind metrics to. * @param cache The cache to instrument. * @param cacheName Will be used to tag metrics with "cache". * @param tags Tags to apply to all recorded metrics. Must be an even number of arguments representing key/value pairs of tags. * @param The cache type. * @return The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way. */ public static C monitor(MeterRegistry registry, C cache, String cacheName, String... tags) { return monitor(registry, cache, cacheName, Tags.of(tags)); } /** * Record metrics on a Caffeine cache. You must call {@link Caffeine#recordStats()} prior to building the cache * for metrics to be recorded. * * @param registry The registry to bind metrics to. * @param cache The cache to instrument. * @param cacheName Will be used to tag metrics with "cache". * @param tags Tags to apply to all recorded metrics. * @param The cache type. * @return The instrumented cache, unchanged. The original cache is not wrapped or proxied in any way. * @see CacheStats */ public static C monitor(MeterRegistry registry, C cache, String cacheName, Iterable tags) { monitor(registry, cache.synchronous(), cacheName, tags); return cache; } @Override protected Long size() { return cache.estimatedSize(); } @Override protected long hitCount() { return cache.stats().hitCount(); } @Override protected long missCount() { return cache.stats().missCount(); } @Override protected Long evictionCount() { return cache.stats().evictionCount(); } @Override protected long putCount() { cache.stats().requestCount(); return cache.stats().loadCount(); } @Override protected void bindImplementationSpecificMetrics(MeterRegistry registry) { Gauge.builder("cache.eviction.weight", cache, c -> c.stats().evictionWeight()) .tags(getTagsWithCacheName()) .description("The sum of weights of evicted entries. This total does not include manual invalidations.") .register(registry); if (cache instanceof LoadingCache) { // dividing these gives you a measure of load latency TimeGauge.builder("cache.load.duration", cache, TimeUnit.NANOSECONDS, c -> c.stats().totalLoadTime()) .tags(getTagsWithCacheName()) .description("The time the cache has spent loading new values") .register(registry); FunctionCounter.builder("cache.load", cache, c -> c.stats().loadSuccessCount()) .tags(getTagsWithCacheName()) .tags("result", "success") .description("The number of times cache lookup methods have successfully loaded a new value") .register(registry); FunctionCounter.builder("cache.load", cache, c -> c.stats().loadFailureCount()) .tags(getTagsWithCacheName()) .tags("result", "failure") .description("The number of times {@link Cache} lookup methods failed to load a new value, either " + "because no value was found or an exception was thrown while loading") .register(registry); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy