-
Notifications
You must be signed in to change notification settings - Fork 807
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
Custom labels for caffeine cache #602
Closed
aaabramov
wants to merge
1
commit into
prometheus:master
from
aaabramov:feature/caffeine-extra-label
Closed
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import io.prometheus.client.SummaryMetricFamily; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
@@ -51,7 +51,25 @@ | |
*/ | ||
public class CacheMetricsCollector extends Collector { | ||
protected final ConcurrentMap<String, Cache> children = new ConcurrentHashMap<String, Cache>(); | ||
|
||
|
||
private final Map<String, String> customLabels; | ||
|
||
/** | ||
* Initialize collector without custom labels. | ||
*/ | ||
public CacheMetricsCollector() { | ||
this(Collections.<String, String>emptyMap()); | ||
} | ||
|
||
/** | ||
* Initialize collector with custom labels. | ||
* | ||
* @param customLabels labels to add to each metric being recorded. | ||
*/ | ||
public CacheMetricsCollector(Map<String, String> customLabels) { | ||
this.customLabels = Collections.unmodifiableMap(customLabels); | ||
} | ||
|
||
/** | ||
* Add or replace the cache with the given name. | ||
* <p> | ||
|
@@ -99,7 +117,12 @@ public void clear(){ | |
@Override | ||
public List<MetricFamilySamples> collect() { | ||
List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>(); | ||
List<String> labelNames = Arrays.asList("cache"); | ||
List<String> labelNames = new ArrayList<String>(customLabels.size() + 1) {{ | ||
add("cache"); | ||
for (Map.Entry<String, String> entry : customLabels.entrySet()) { | ||
add(entry.getKey()); | ||
} | ||
}}; | ||
|
||
CounterMetricFamily cacheHitTotal = new CounterMetricFamily("caffeine_cache_hit_total", | ||
"Cache hit totals", labelNames); | ||
|
@@ -137,27 +160,34 @@ public List<MetricFamilySamples> collect() { | |
"Cache load duration: both success and failures", labelNames); | ||
mfs.add(cacheLoadSummary); | ||
|
||
for(Map.Entry<String, Cache> c: children.entrySet()) { | ||
List<String> cacheName = Arrays.asList(c.getKey()); | ||
for(final Map.Entry<String, Cache> c: children.entrySet()) { | ||
|
||
List<String> labelValues = new ArrayList<String>(customLabels.size() + 1) {{ | ||
add(c.getKey()); | ||
for (Map.Entry<String, String> entry : customLabels.entrySet()) { | ||
add(entry.getValue()); | ||
} | ||
}}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be a good idea to store label inside |
||
|
||
CacheStats stats = c.getValue().stats(); | ||
|
||
try{ | ||
cacheEvictionWeight.addMetric(cacheName, stats.evictionWeight()); | ||
cacheEvictionWeight.addMetric(labelValues, stats.evictionWeight()); | ||
} catch (Exception e) { | ||
// EvictionWeight metric is unavailable, newer version of Caffeine is needed. | ||
} | ||
|
||
cacheHitTotal.addMetric(cacheName, stats.hitCount()); | ||
cacheMissTotal.addMetric(cacheName, stats.missCount()); | ||
cacheRequestsTotal.addMetric(cacheName, stats.requestCount()); | ||
cacheEvictionTotal.addMetric(cacheName, stats.evictionCount()); | ||
cacheSize.addMetric(cacheName, c.getValue().estimatedSize()); | ||
cacheHitTotal.addMetric(labelValues, stats.hitCount()); | ||
cacheMissTotal.addMetric(labelValues, stats.missCount()); | ||
cacheRequestsTotal.addMetric(labelValues, stats.requestCount()); | ||
cacheEvictionTotal.addMetric(labelValues, stats.evictionCount()); | ||
cacheSize.addMetric(labelValues, c.getValue().estimatedSize()); | ||
|
||
if(c.getValue() instanceof LoadingCache) { | ||
cacheLoadFailure.addMetric(cacheName, stats.loadFailureCount()); | ||
cacheLoadTotal.addMetric(cacheName, stats.loadCount()); | ||
cacheLoadFailure.addMetric(labelValues, stats.loadFailureCount()); | ||
cacheLoadTotal.addMetric(labelValues, stats.loadCount()); | ||
|
||
cacheLoadSummary.addMetric(cacheName, stats.loadCount(), stats.totalLoadTime() / Collector.NANOSECONDS_PER_SECOND); | ||
cacheLoadSummary.addMetric(labelValues, stats.loadCount(), stats.totalLoadTime() / Collector.NANOSECONDS_PER_SECOND); | ||
} | ||
} | ||
return mfs; | ||
|
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.
Should we initialize this this List each time? Maybe simply initialize it once as instance field?