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

Allow renaming of metric fields. #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
157 changes: 110 additions & 47 deletions src/main/java/no/finn/metrics/GuavaCacheMetrics.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package no.finn.metrics;

import static com.codahale.metrics.MetricRegistry.name;

import java.util.HashMap;
import java.util.Map;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import com.google.common.cache.Cache;

import java.util.HashMap;
import java.util.Map;

import static com.codahale.metrics.MetricRegistry.name;

/**
* Created by Henning Spjelkavik on 23.10.2014.
*
Expand All @@ -20,6 +20,110 @@

public class GuavaCacheMetrics extends HashMap< String, Metric> implements MetricSet {

public class Builder {
private String hitRateMetric = "hitRate";
private String hitCountMetric = "hitCount";
private String missCountMetric = "missCount";
private String loadExceptionCountMetric = "loadExceptionCount";
private String evictionCountMetric = "evictionCount";
private Class clzz = GuavaCacheMetrics.class;
private String cacheName = "cache";
final Cache cache;

Builder(final Cache cache) {
this.cache = cache;
}

public Builder withHitRateName(String name) {
hitRateMetric = name;
return this;
}

public Builder withHitCountName(String name) {
hitCountMetric = name;
return this;
}

public Builder withMissCountName(String name) {
missCountMetric = name;
return this;
}

public Builder withLoadExceptionCountName(String name) {
loadExceptionCountMetric = name;
return this;
}

public Builder withEvictionCountName(String name) {
evictionCountMetric = name;
return this;
}

public Builder forClass(Class clzz) {
this.clzz = clzz;
return this;
}

public Builder withCacheName(String cacheName) {
this.cacheName = cacheName;
return this;
}

public GuavaCacheMetrics build() {
GuavaCacheMetrics metrics = GuavaCacheMetrics.this;

metrics.put( name( clzz, cacheName, hitRateMetric), new Gauge< Double >() {
@Override
public Double getValue() {
return cache.stats().hitRate();
}
} );

metrics.put( name( clzz, cacheName, hitCountMetric), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().hitCount();
}
} );

metrics.put( name( clzz, cacheName, missCountMetric), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().missCount();
}
} );

metrics.put( name( clzz, cacheName, loadExceptionCountMetric), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().loadExceptionCount();
}
} );

metrics.put( name( clzz, cacheName, evictionCountMetric ), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().evictionCount();
}
} );

return metrics;
}
}

/**
* Rename metrics. Has global effect.
*/
private GuavaCacheMetrics() {}

/**
* Create new Builder.
*/
public static Builder newBuilder(final Cache cache) {
GuavaCacheMetrics metrics = new GuavaCacheMetrics();
return metrics.new Builder(cache);
}

/**
* Wraps the provided Guava cache's statistics into Gauges suitable for reporting via Codahale Metrics
* The returned MetricSet is suitable for registration with a MetricRegistry like so:
Expand All @@ -31,48 +135,7 @@ public class GuavaCacheMetrics extends HashMap< String, Metric> implements Metri
* @return MetricSet suitable for registration with a MetricRegistry
*/
public static MetricSet metricsFor( Class clzz, String cacheName, final Cache cache ) {

GuavaCacheMetrics metrics = new GuavaCacheMetrics();

metrics.put( name( clzz, cacheName, "hitRate" ), new Gauge< Double >() {
@Override
public Double getValue() {
return cache.stats().hitRate();
}
} );

metrics.put( name( clzz, cacheName, "hitCount" ), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().hitCount();
}
} );

metrics.put( name( clzz, cacheName, "missCount" ), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().missCount();
}
} );

metrics.put( name( clzz, cacheName, "loadExceptionCount" ), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().loadExceptionCount();
}
} );

metrics.put( name( clzz, cacheName, "evictionCount" ), new Gauge< Long >() {
@Override
public Long getValue() {
return cache.stats().evictionCount();
}
} );

return metrics;
}

private GuavaCacheMetrics() {
return newBuilder(cache).withCacheName(cacheName).forClass(clzz).build();
}

@Override
Expand Down
21 changes: 15 additions & 6 deletions src/test/groovy/no/finn/metrics/GuavaCacheMetricsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ class GuavaCacheMetricsTest extends Specification {

def cache = CacheBuilder.newBuilder().recordStats().build()
def registry = new MetricRegistry()
registry.registerAll(GuavaCacheMetrics.metricsFor(GuavaCacheMetricsTest.class, "MyCache", cache))
def metrics = GuavaCacheMetrics.newBuilder(cache)
.withHitCountName("hits")
.withMissCountName("misses")
.withHitRateName("efficiency")
.withEvictionCountName("evictions")
.withLoadExceptionCountName("loadExceptions")
.withCacheName("MyCache")
.forClass(GuavaCacheMetricsTest.class)
.build()
registry.registerAll(metrics)

when: "various read/write operations are performed on the cache"

Expand All @@ -37,10 +46,10 @@ class GuavaCacheMetricsTest extends Specification {

def gauges = registry.gauges

2 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.hitCount"].value
3 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.missCount"].value
0.4 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.hitRate"].value
1 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.loadExceptionCount"].value
0 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.evictionCount"].value
2 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.hits"].value
3 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.misses"].value
0.4 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.efficiency"].value
1 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.loadExceptions"].value
0 == gauges["no.finn.metrics.GuavaCacheMetricsTest.MyCache.evictions"].value
}
}