Skip to content

Commit

Permalink
Replace expiringMap with caffeine (#862)
Browse files Browse the repository at this point in the history
* Replace expiringMap with caffeine
* Use interface instead of implementation
  • Loading branch information
SimunKaracic authored Sep 29, 2020
1 parent 5fcc68e commit 14bef34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,11 @@ lazy val `kamon-annotation` = (project in file("instrumentation/kamon-annotation
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("javax.el.**" -> "kamon.lib.@0").inAll,
ShadeRule.rename("com.sun.el.**" -> "kamon.lib.@0").inAll,
ShadeRule.rename("com.github.ben-manes.**" -> "kamon.lib.@0").inAll,
),
libraryDependencies ++= Seq(
kanelaAgent % "provided",
"com.github.ben-manes.caffeine" % "caffeine" % "2.8.5" % "provided,shaded", // provided? no?
"org.glassfish" % "javax.el" % "3.0.1-b11" % "provided,shaded",
scalatest % "test",
logbackClassic % "test",
Expand Down Expand Up @@ -637,4 +639,4 @@ val `kamon-bundle` = (project in file("bundle/kamon-bundle"))
`kamon-akka-http` % "shaded",
`kamon-play` % "shaded",
`kamon-okhttp` % "shaded",
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package kamon.annotation.instrumentation.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import kamon.Kamon;
import kamon.annotation.api.Time;
import kamon.annotation.api.TrackConcurrency;
Expand All @@ -24,29 +26,26 @@
import kamon.metric.*;
import kamon.tag.TagSet;
import kamon.trace.SpanBuilder;
import kanela.agent.libs.net.jodah.expiringmap.ExpirationListener;
import kanela.agent.libs.net.jodah.expiringmap.ExpirationPolicy;
import kanela.agent.libs.net.jodah.expiringmap.ExpiringMap;
import kanela.agent.util.log.Logger;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

public final class AnnotationCache {

private static Map<MetricKey, Object> metrics = buildCache();

private static Map<MetricKey, Object> buildCache() {
return ExpiringMap
.builder()
.expiration(1, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.ACCESSED)
.asyncExpirationListener(ExpirationListener())
.build();
return Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.removalListener(LogExpirationListener())
.build()
.asMap();
}

public static Gauge getGauge(Method method, Object obj, Class<?> clazz, String className, String methodName) {
Expand Down Expand Up @@ -193,8 +192,8 @@ private static String getOperationName(String name, Object obj, Class<?> clazz,
return (evaluatedString.isEmpty() || evaluatedString.equals("unknown")) ? className + "." + methodName: evaluatedString;
}

private static ExpirationListener<MetricKey, Object> ExpirationListener() {
return (key, value) -> {
private static RemovalListener<MetricKey, Object> LogExpirationListener() {
return (key, value, cause) -> {
if(value instanceof Instrument) ((Instrument) value).remove();
Logger.debug(() -> "Expiring key: " + key + "with value" + value);
};
Expand Down

0 comments on commit 14bef34

Please sign in to comment.