Skip to content
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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion simpleclient_caffeine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
<version>2.8.6</version>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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());
}
}};
Copy link
Author

@aaabramov aaabramov Nov 16, 2020

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?


CounterMetricFamily cacheHitTotal = new CounterMetricFamily("caffeine_cache_hit_total",
"Cache hit totals", labelNames);
Expand Down Expand Up @@ -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());
}
}};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a good idea to store label inside children value. This would require adding custom class as a value but since it is internal we can implement this without breaking changes.


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;
Expand Down