Use ClassValue instead of WeakMap to cache span name
MODIFY
instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsTracer.java
instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsTracer.java
package io.opentelemetry.javaagent.instrumentation.jaxrs.v1_0;
-import static io.opentelemetry.javaagent.instrumentation.api.WeakMap.Provider.newWeakMap;
-
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
-import io.opentelemetry.javaagent.instrumentation.api.WeakMap;
import io.opentelemetry.javaagent.tooling.ClassHierarchyIterable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -30,7 +27,13 @@ public class JaxRsAnnotationsTracer extends BaseTracer {
return TRACER;
}
- private final WeakMap<Class<?>, Map<Method, String>> spanNames = newWeakMap();
+ private final ClassValue<Map<Method, String>> spanNames =
+ new ClassValue<Map<Method, String>>() {
+ @Override
+ protected Map<Method, String> computeValue(Class<?> type) {
+ return new ConcurrentHashMap<>();
+ }
+ };
public Context startSpan(Class<?> target, Method method) {
String pathBasedSpanName = getPathSpanName(target, method);
@@ -63,14 +66,6 @@ public class JaxRsAnnotationsTracer extends BaseTracer {
*/
private String getPathSpanName(Class<?> target, Method method) {
Map<Method, String> classMap = spanNames.get(target);
-
- if (classMap == null) {
- spanNames.putIfAbsent(target, new ConcurrentHashMap<>());
- classMap = spanNames.get(target);
- // classMap should not be null at this point because we have a
- // strong reference to target and don't manually clear the map.
- }
-
String spanName = classMap.get(method);
if (spanName == null) {
String httpMethod = null;

MODIFY
instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsTracer.java
instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsTracer.java
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
-import static io.opentelemetry.javaagent.instrumentation.api.WeakMap.Provider.newWeakMap;
-
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
-import io.opentelemetry.javaagent.instrumentation.api.WeakMap;
import io.opentelemetry.javaagent.tooling.ClassHierarchyIterable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -33,7 +30,13 @@ public class JaxRsAnnotationsTracer extends BaseTracer {
return TRACER;
}
- private final WeakMap<Class<?>, Map<Method, String>> spanNames = newWeakMap();
+ private final ClassValue<Map<Method, String>> spanNames =
+ new ClassValue<Map<Method, String>>() {
+ @Override
+ protected Map<Method, String> computeValue(Class<?> type) {
+ return new ConcurrentHashMap<>();
+ }
+ };
public Context startSpan(Class<?> target, Method method) {
return startSpan(Context.current(), target, method);
@@ -74,14 +77,6 @@ public class JaxRsAnnotationsTracer extends BaseTracer {
*/
private String getPathSpanName(Class<?> target, Method method) {
Map<Method, String> classMap = spanNames.get(target);
-
- if (classMap == null) {
- spanNames.putIfAbsent(target, new ConcurrentHashMap<>());
- classMap = spanNames.get(target);
- // classMap should not be null at this point because we have a
- // strong reference to target and don't manually clear the map.
- }
-
String spanName = classMap.get(method);
if (spanName == null) {
String httpMethod = null;

Use ClassValue instead of WeakMap to cache span name (#2491)