-
Couldn't load subscription status.
- Fork 1k
stabilise spanNames #12487
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
stabilise spanNames #12487
Changes from 8 commits
1fb9db5
5faaf8c
1adb303
8fd0e55
26cfbf0
f7c177a
94738e3
e014cbf
649d6ce
d3356bd
899b573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| Comparing source compatibility of opentelemetry-instrumentation-api-2.11.0-SNAPSHOT.jar against opentelemetry-instrumentation-api-2.10.0.jar | ||
| No changes. | ||
| +++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.instrumentation.api.semconv.util.SpanNames (not serializable) | ||
| +++ CLASS FILE FORMAT VERSION: 52.0 <- n.a. | ||
| +++ NEW SUPERCLASS: java.lang.Object | ||
| +++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String fromMethod(java.lang.reflect.Method) | ||
| +++ NEW METHOD: PUBLIC(+) STATIC(+) java.lang.String fromMethod(java.lang.Class<?>, java.lang.String) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.semconv.util; | ||
|
|
||
| import io.opentelemetry.instrumentation.api.internal.ClassNames; | ||
| import io.opentelemetry.instrumentation.api.internal.cache.Cache; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** A utility class used to generate span names. */ | ||
| public final class SpanNames { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static final Cache<Class<?>, Map<String, String>> spanNameCaches = Cache.weak(); | ||
|
|
||
| /** | ||
| * 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(Method method) { | ||
| return fromMethod(method.getDeclaringClass(), method.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * 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<?> clazz, String methodName) { | ||
| // the cache (ConcurrentHashMap) is naturally bounded by the number of methods in a class | ||
| Map<String, String> spanNameCache = | ||
| spanNameCaches.computeIfAbsent(clazz, c -> new ConcurrentHashMap<>()); | ||
|
|
||
| // not using computeIfAbsent, because it would require a capturing (allocating) lambda | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That shouldn't be the case if you use a method reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how you would use a method reference here? |
||
| String spanName = spanNameCache.get(methodName); | ||
| if (spanName != null) { | ||
| return spanName; | ||
| } | ||
| spanName = ClassNames.simpleName(clazz) + "." + methodName; | ||
| spanNameCache.put(methodName, spanName); | ||
| return spanName; | ||
| } | ||
|
|
||
| private SpanNames() {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.semconv.util; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SpanNamesTest { | ||
| @Test | ||
| void testFromMethod() throws NoSuchMethodException { | ||
| Method method = TestClass.class.getMethod("test"); | ||
| assertThat(SpanNames.fromMethod(method)).isEqualTo("TestClass.test"); | ||
| } | ||
|
|
||
| static class TestClass { | ||
| private TestClass() {} | ||
|
|
||
| public void test() {} | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.