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

Add span names cache #4004

Merged
merged 2 commits into from
Aug 30, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> {
Expand All @@ -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}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, String>> spanNameCaches =
new ClassValue<Map<String, String>>() {
@Override
protected Map<String, String> 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.
Expand All @@ -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<String, String> 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() {}
Expand Down