Skip to content

Commit e014cbf

Browse files
committed
add javadoc,unit test
1 parent 94738e3 commit e014cbf

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Comparing source compatibility of opentelemetry-instrumentation-api-2.11.0-SNAPSHOT.jar against opentelemetry-instrumentation-api-2.10.0.jar
2-
No changes.
2+
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.instrumentation.api.semconv.util.SpanNames (not serializable)
3+
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
4+
+++ NEW SUPERCLASS: java.lang.Object
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String fromMethod(java.lang.reflect.Method)
6+
+++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String fromMethod(java.lang.Class<?>, java.lang.String)

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/util/SpanNames.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
package io.opentelemetry.instrumentation.api.incubator.semconv.util;
77

8-
import io.opentelemetry.instrumentation.api.internal.ClassNames;
9-
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
108
import java.lang.reflect.Method;
11-
import java.util.Map;
12-
import java.util.concurrent.ConcurrentHashMap;
139

1410
/**
1511
* This class has been stabilized and moved to {@link
@@ -18,33 +14,21 @@
1814
@Deprecated
1915
public final class SpanNames {
2016

21-
private static final Cache<Class<?>, Map<String, String>> spanNameCaches = Cache.weak();
22-
2317
/**
2418
* This method is used to generate a span name based on a method. Anonymous classes are named
2519
* based on their parent.
2620
*/
2721
public static String fromMethod(Method method) {
28-
return fromMethod(method.getDeclaringClass(), method.getName());
22+
return io.opentelemetry.instrumentation.api.semconv.util.SpanNames.fromMethod(method);
2923
}
3024

3125
/**
3226
* This method is used to generate a span name based on a method. Anonymous classes are named
3327
* based on their parent.
3428
*/
3529
public static String fromMethod(Class<?> clazz, String methodName) {
36-
// the cache (ConcurrentHashMap) is naturally bounded by the number of methods in a class
37-
Map<String, String> spanNameCache =
38-
spanNameCaches.computeIfAbsent(clazz, c -> new ConcurrentHashMap<>());
39-
40-
// not using computeIfAbsent, because it would require a capturing (allocating) lambda
41-
String spanName = spanNameCache.get(methodName);
42-
if (spanName != null) {
43-
return spanName;
44-
}
45-
spanName = ClassNames.simpleName(clazz) + "." + methodName;
46-
spanNameCache.put(methodName, spanName);
47-
return spanName;
30+
return io.opentelemetry.instrumentation.api.semconv.util.SpanNames.fromMethod(
31+
clazz, methodName);
4832
}
4933

5034
private SpanNames() {}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/util/SpanNames.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Map;
1212
import java.util.concurrent.ConcurrentHashMap;
1313

14+
/** A utility class used to generate span names. */
1415
public final class SpanNames {
1516

1617
private static final Cache<Class<?>, Map<String, String>> spanNameCaches = Cache.weak();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.api.semconv.util;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import java.lang.reflect.Method;
11+
import org.junit.jupiter.api.Test;
12+
13+
class SpanNamesTest {
14+
@Test
15+
void testFromMethod() throws NoSuchMethodException {
16+
Method method = TestClass.class.getMethod("test");
17+
assertThat(SpanNames.fromMethod(method)).isEqualTo("TestClass.test");
18+
}
19+
20+
static class TestClass {
21+
private TestClass() {}
22+
23+
public void test() {}
24+
}
25+
}

0 commit comments

Comments
 (0)