-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Caffeine CacheStats recorder #1897
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
66a4888
Initial Caffeine CacheStats recorder
schlosna 0af16d2
readme
schlosna 683c166
self supply
schlosna 61e72e5
Remove gauges & simplify
schlosna 7c83c7a
javadoc
schlosna 0f65783
enum map
schlosna 9f1a698
Add generated changelog entries
svc-changelog 7ad96c1
private metric schema
schlosna ff78d58
safe
schlosna 031b770
deprecate older cache registration mechanisms
schlosna c89fdc2
trigger cache cleanup & evictions in tests
schlosna cd7e68b
update readme
schlosna 4530a28
meters
schlosna af50aaf
Cleanup metric names
schlosna 1fd78b9
relax deprecation for now
schlosna 1bcf527
load timer
schlosna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
type: improvement | ||
improvement: | ||
description: | | ||
Initial Caffeine CacheStats recorder | ||
|
||
Example usage: | ||
``` | ||
TaggedMetricRegistry taggedMetricRegistry = ... | ||
Cache<Integer, String> cache = Caffeine.newBuilder() | ||
.recordStats(CacheStats.of(taggedMetricRegistry, "unique-cache-name")) | ||
.build(); | ||
|
||
LoadingCache<String, Integer> loadingCache = Caffeine.newBuilder() | ||
.recordStats(CacheStats.of(taggedMetricRegistry, "unique-loading-cache-name")) | ||
.build(key::length); | ||
``` | ||
links: | ||
- https://github.com/palantir/tritium/pull/1897 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tritium-caffeine/src/main/java/com/palantir/tritium/metrics/caffeine/CacheStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 com.palantir.tritium.metrics.caffeine; | ||
|
||
import com.codahale.metrics.Counting; | ||
import com.codahale.metrics.Meter; | ||
import com.codahale.metrics.Timer; | ||
import com.github.benmanes.caffeine.cache.RemovalCause; | ||
import com.github.benmanes.caffeine.cache.stats.StatsCounter; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Maps; | ||
import com.palantir.logsafe.Safe; | ||
import com.palantir.tritium.metrics.caffeine.CacheMetrics.Load_Result; | ||
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry; | ||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.LongAdder; | ||
import java.util.function.Supplier; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
|
||
public final class CacheStats implements StatsCounter, Supplier<StatsCounter> { | ||
private final String name; | ||
private final Meter hitMeter; | ||
private final Meter missMeter; | ||
private final Timer loadSuccessTimer; | ||
private final Timer loadFailureTimer; | ||
private final Meter evictionsTotalMeter; | ||
private final ImmutableMap<RemovalCause, Meter> evictionMeters; | ||
private final LongAdder totalLoadNanos = new LongAdder(); | ||
|
||
/** | ||
* Creates a {@link CacheStats} instance that registers metrics for Caffeine cache statistics. | ||
* <p> | ||
* Example usage for a {@link com.github.benmanes.caffeine.cache.Cache} or | ||
* {@link com.github.benmanes.caffeine.cache.LoadingCache}: | ||
* <pre> | ||
* LoadingCache<Integer, String> cache = Caffeine.newBuilder() | ||
* .recordStats(CacheStats.of(taggedMetricRegistry, "your-cache-name")) | ||
* .build(key -> computeSomethingExpensive(key)); | ||
* </pre> | ||
* @param taggedMetricRegistry tagged metric registry to add cache metrics | ||
* @param name cache name | ||
* @return Caffeine stats instance to register via | ||
* {@link com.github.benmanes.caffeine.cache.Caffeine#recordStats(Supplier)}. | ||
*/ | ||
public static CacheStats of(TaggedMetricRegistry taggedMetricRegistry, @Safe String name) { | ||
return new CacheStats(CacheMetrics.of(taggedMetricRegistry), name); | ||
} | ||
|
||
private CacheStats(CacheMetrics metrics, @Safe String name) { | ||
this.name = name; | ||
this.hitMeter = metrics.hit(name); | ||
this.missMeter = metrics.miss(name); | ||
this.loadSuccessTimer = | ||
metrics.load().cache(name).result(Load_Result.SUCCESS).build(); | ||
this.loadFailureTimer = | ||
metrics.load().cache(name).result(Load_Result.FAILURE).build(); | ||
this.evictionsTotalMeter = metrics.eviction(name); | ||
this.evictionMeters = Arrays.stream(RemovalCause.values()) | ||
.collect(Maps.toImmutableEnumMap(cause -> cause, cause -> metrics.evictions() | ||
.cache(name) | ||
.cause(cause.toString()) | ||
.build())); | ||
} | ||
|
||
@Override | ||
public StatsCounter get() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public void recordHits(@NonNegative int count) { | ||
hitMeter.mark(count); | ||
} | ||
|
||
@Override | ||
public void recordMisses(@NonNegative int count) { | ||
missMeter.mark(count); | ||
} | ||
|
||
@Override | ||
public void recordLoadSuccess(@NonNegative long loadTime) { | ||
loadSuccessTimer.update(loadTime, TimeUnit.NANOSECONDS); | ||
totalLoadNanos.add(loadTime); | ||
} | ||
|
||
@Override | ||
public void recordLoadFailure(@NonNegative long loadTime) { | ||
loadFailureTimer.update(loadTime, TimeUnit.NANOSECONDS); | ||
totalLoadNanos.add(loadTime); | ||
} | ||
|
||
@Override | ||
public void recordEviction(@NonNegative int weight, RemovalCause cause) { | ||
Meter counter = evictionMeters.get(cause); | ||
if (counter != null) { | ||
counter.mark(weight); | ||
} | ||
evictionsTotalMeter.mark(weight); | ||
} | ||
|
||
@Override | ||
public com.github.benmanes.caffeine.cache.stats.CacheStats snapshot() { | ||
return com.github.benmanes.caffeine.cache.stats.CacheStats.of( | ||
hitMeter.getCount(), | ||
missMeter.getCount(), | ||
loadSuccessTimer.getCount(), | ||
loadFailureTimer.getCount(), | ||
totalLoadNanos.sum(), | ||
evictionsTotalMeter.getCount(), | ||
evictionMeters.values().stream().mapToLong(Counting::getCount).sum()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + ": " + snapshot(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
options: | ||
javaPackage: com.palantir.tritium.metrics.caffeine | ||
javaVisibility: packagePrivate | ||
namespaces: | ||
cache: | ||
docs: Cache statistic metrics | ||
metrics: | ||
hit: | ||
type: meter | ||
tags: [cache] | ||
docs: Count of cache hits | ||
miss: | ||
type: meter | ||
tags: [cache] | ||
docs: Count of cache misses | ||
load: | ||
type: timer | ||
tags: | ||
- name: cache | ||
- name: result | ||
values: [success, failure] | ||
docs: Count of successful cache loads | ||
evictions: | ||
type: meter | ||
tags: [cache, cause] | ||
docs: Count of evicted entries by cause | ||
eviction: | ||
type: meter | ||
tags: [cache] | ||
docs: Total count of evicted entries | ||
stats.disabled: | ||
type: meter | ||
tags: [cache] | ||
docs: | | ||
Registered cache does not have stats recording enabled, stats will always be zero. | ||
To enable cache metrics, stats recording must be enabled when constructing the cache: | ||
Caffeine.newBuilder().recordStats() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we could make the meter into a timer, which also provides a “count” value in addition to percentiles, more timeseries though