From dc4e239722e51e90b13ac2c14d59c845246df658 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Mon, 30 Aug 2021 09:02:27 -0700 Subject: [PATCH] Add span names cache (#4004) * Add span names cache * Simplify --- .../instrumentation/api/caching/Cache.java | 2 ++ .../instrumentation/api/tracer/SpanNames.java | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/instrumentation-api-caching/src/main/java/io/opentelemetry/instrumentation/api/caching/Cache.java b/instrumentation-api-caching/src/main/java/io/opentelemetry/instrumentation/api/caching/Cache.java index 0a12f43862eb..2eaf273a39fa 100644 --- a/instrumentation-api-caching/src/main/java/io/opentelemetry/instrumentation/api/caching/Cache.java +++ b/instrumentation-api-caching/src/main/java/io/opentelemetry/instrumentation/api/caching/Cache.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.api.caching; import java.util.function.Function; +import org.checkerframework.checker.nullness.qual.Nullable; /** A cache from keys to values. */ public interface Cache { @@ -25,6 +26,7 @@ static CacheBuilder newBuilder() { * Returns the cached value associated with the provided {@code key} if present, or {@code null} * otherwise. */ + @Nullable V get(K key); /** Puts the {@code value} into the cache for the {@code key}. */ diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/SpanNames.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/SpanNames.java index dad2a2bd3cf1..7c17e99674d8 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/SpanNames.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/SpanNames.java @@ -6,9 +6,21 @@ package io.opentelemetry.instrumentation.api.tracer; import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.checkerframework.checker.nullness.qual.Nullable; public final class SpanNames { + + private static final ClassValue> spanNameCaches = + new ClassValue>() { + @Override + protected Map computeValue(Class clazz) { + // the cache is naturally bounded by the number of methods in a class + return new ConcurrentHashMap<>(); + } + }; + /** * This method is used to generate a span name based on a method. Anonymous classes are named * based on their parent. @@ -29,8 +41,16 @@ public static String fromMethod(Class clazz, @Nullable Method method) { * This method is used to generate a span name based on a method. Anonymous classes are named * based on their parent. */ - public static String fromMethod(Class cl, String methodName) { - return ClassNames.simpleName(cl) + "." + methodName; + public static String fromMethod(Class clazz, String methodName) { + Map spanNameCache = spanNameCaches.get(clazz); + // not using computeIfAbsent, because it would require a capturing (allocating) lambda + String spanName = spanNameCache.get(methodName); + if (spanName != null) { + return spanName; + } + spanName = ClassNames.simpleName(clazz) + "." + methodName; + spanNameCache.put(methodName, spanName); + return spanName; } private SpanNames() {}