From 7f6c0a0deaf9f43ed9cb56948caf3744853a5634 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Mon, 27 Jan 2025 15:49:54 +0800 Subject: [PATCH 01/10] Add withParent option to annotation `@WithSpan` Signed-off-by: xiepuhuan --- ...ntelemetry-instrumentation-annotations.txt | 4 ++- .../instrumentation/annotations/WithSpan.java | 3 +++ .../AnnotationSingletons.java | 19 ++++++++++++++ .../test/annotation/TracedWithSpan.java | 10 +++++++ .../WithSpanInstrumentationTest.java | 25 ++++++++++++++++++ .../annotations/JoinPointRequest.java | 12 +++++++-- .../annotations/WithSpanAspect.java | 10 +++++++ .../InstrumentationWithSpanAspectTest.java | 26 +++++++++++++++++++ 8 files changed, 106 insertions(+), 3 deletions(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt index 8e89ef653743..1830f9bf610c 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt @@ -1,2 +1,4 @@ Comparing source compatibility of opentelemetry-instrumentation-annotations-2.13.0-SNAPSHOT.jar against opentelemetry-instrumentation-annotations-2.12.0.jar -No changes. \ No newline at end of file +**** MODIFIED ANNOTATION: PUBLIC ABSTRACT io.opentelemetry.instrumentation.annotations.WithSpan (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++* NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean withParent() diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index 6b4cf4fefb75..c3075aaeca95 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -34,4 +34,7 @@ /** Specify the {@link SpanKind} of span to be created. Defaults to {@link SpanKind#INTERNAL}. */ SpanKind kind() default SpanKind.INTERNAL; + + /** Specify whether to create a new span with parent. Defaults to {@code true}. */ + boolean withParent() default true; } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java index 4e268ebf496b..843b6191fb34 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java @@ -9,7 +9,9 @@ import application.io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.annotation.support.MethodSpanAttributesExtractor; import io.opentelemetry.instrumentation.api.annotation.support.SpanAttributesExtractor; import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; @@ -47,6 +49,7 @@ private static Instrumenter createInstrumenter() { INSTRUMENTATION_NAME, AnnotationSingletons::spanNameFromMethod) .addAttributesExtractor(CodeAttributesExtractor.create(MethodCodeAttributesGetter.INSTANCE)) + .addContextCustomizer(AnnotationSingletons::parentContextFromMethod) .buildInstrumenter(AnnotationSingletons::spanKindFromMethod); } @@ -62,6 +65,7 @@ private static Instrumenter createInstrumenterWithAttribu MethodRequest::method, WithSpanParameterAttributeNamesExtractor.INSTANCE, MethodRequest::args)) + .addContextCustomizer(AnnotationSingletons::parentContextFromMethodRequest) .buildInstrumenter(AnnotationSingletons::spanKindFromMethodRequest); } @@ -104,5 +108,20 @@ private static String spanNameFromMethod(Method method) { return spanName; } + private static Context parentContextFromMethodRequest( + Context context, MethodRequest request, Attributes attributes) { + return parentContextFromMethod(context, request.method(), attributes); + } + + private static Context parentContextFromMethod( + Context context, Method method, Attributes attributes) { + WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); + if (annotation.withParent()) { + return context; + } + + return Context.root(); + } + private AnnotationSingletons() {} } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java index 3fab82c2bcea..6b449df43870 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java @@ -57,4 +57,14 @@ public CompletionStage completionStage(CompletableFuture future) public CompletableFuture completableFuture(CompletableFuture future) { return future; } + + @WithSpan(withParent = false) + public String withoutParent() { + return "hello!"; + } + + @WithSpan(kind = SpanKind.CONSUMER) + public String consumer() { + return withoutParent(); + } } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java index 93fe05a74076..4f79585cc90c 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java @@ -107,6 +107,31 @@ void multipleSpans() { equalTo(CODE_FUNCTION, "otel")))); } + @Test + void multipleSpansWithoutParent() { + new TracedWithSpan().consumer(); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.consumer") + .hasKind(SpanKind.CONSUMER) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "consumer"))), + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("TracedWithSpan.withoutParent") + .hasKind(SpanKind.INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, TracedWithSpan.class.getName()), + equalTo(CODE_FUNCTION, "withoutParent")))); + } + @Test void excludedMethod() throws Exception { new TracedWithSpan().ignored(); diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java index 4189c6cb3060..ce230fccb966 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java @@ -18,8 +18,10 @@ final class JoinPointRequest { private final Method method; private final String spanName; private final SpanKind spanKind; + private final boolean withParent; - private JoinPointRequest(JoinPoint joinPoint, Method method, String spanName, SpanKind spanKind) { + private JoinPointRequest( + JoinPoint joinPoint, Method method, String spanName, SpanKind spanKind, boolean withParent) { if (spanName.isEmpty()) { spanName = SpanNames.fromMethod(method); } @@ -28,6 +30,7 @@ private JoinPointRequest(JoinPoint joinPoint, Method method, String spanName, Sp this.method = method; this.spanName = spanName; this.spanKind = spanKind; + this.withParent = withParent; } String spanName() { @@ -46,6 +49,10 @@ Object[] args() { return joinPoint.getArgs(); } + boolean withParent() { + return withParent; + } + interface Factory { JoinPointRequest create(JoinPoint joinPoint); @@ -65,8 +72,9 @@ public JoinPointRequest create(JoinPoint joinPoint) { WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); String spanName = annotation != null ? annotation.value() : ""; SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL; + boolean withParent = annotation == null || annotation.withParent(); - return new JoinPointRequest(joinPoint, method, spanName, spanKind); + return new JoinPointRequest(joinPoint, method, spanName, spanKind, withParent); } } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java index f25506a0b780..618a70358376 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.annotations; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -53,10 +54,19 @@ abstract class WithSpanAspect { JoinPointRequest::method, parameterAttributeNamesExtractor, JoinPointRequest::args)) + .addContextCustomizer(WithSpanAspect::parentContext) .buildInstrumenter(JoinPointRequest::spanKind); this.requestFactory = requestFactory; } + static Context parentContext(Context parentContext, JoinPointRequest request, Attributes unused) { + if (request.withParent()) { + return parentContext; + } + + return Context.root(); + } + public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable { JoinPointRequest request = requestFactory.create(pjp); Context parentContext = Context.current(); diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java index fbacd93f47a4..af6082a5ebce 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java @@ -180,6 +180,27 @@ void withSpanAttributes() { equalTo(stringKey("explicitName"), "baz")))); } + @Test + @DisplayName( + "when method is annotated with @WithSpan(withParent=false) should build span without parent") + void withSpanWithoutParent() { + // when + testing.runWithSpan("parent", withSpanTester::testWithoutParentSpan); + + // then + testing.waitAndAssertTraces( + trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL)), + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testWithoutParentSpan") + .hasKind(INTERNAL) + .hasNoParent() + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testWithoutParentSpan")))); + } + static class InstrumentationWithSpanTester { @WithSpan public String testWithSpan() { @@ -221,6 +242,11 @@ public String withSpanAttributes( return "hello!"; } + + @WithSpan(withParent = false) + public String testWithoutParentSpan() { + return "Span without parent span was created"; + } } @Nested From 054ddb4c171d68ac98cbc779ed4ccce328c85965 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Tue, 28 Jan 2025 21:35:51 +0800 Subject: [PATCH 02/10] Backward compatibility with older versions @WithSpan --- .../AnnotationSingletons.java | 32 +++++++++++++++-- .../annotations/JoinPointRequest.java | 35 +++++++++++++++++-- .../annotations/WithSpanAspect.java | 9 ++--- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java index 843b6191fb34..0fcbd2e42cba 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java @@ -17,6 +17,9 @@ import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.semconv.util.SpanNames; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.reflect.Method; import java.util.logging.Logger; @@ -31,6 +34,21 @@ public final class AnnotationSingletons { createInstrumenterWithAttributes(); private static final SpanAttributesExtractor ATTRIBUTES = createAttributesExtractor(); + // The reason for using reflection here is that it needs to be compatible with the old version of + // @WithSpan annotation that does not include the withParent option to avoid failing the muzzle + // check. + private static MethodHandle withParentMethodHandle = null; + + static { + try { + withParentMethodHandle = + MethodHandles.publicLookup() + .findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class)); + } catch (NoSuchMethodException | IllegalAccessException ignore) { + // ignore + } + } + public static Instrumenter instrumenter() { return INSTRUMENTER; } @@ -115,12 +133,20 @@ private static Context parentContextFromMethodRequest( private static Context parentContextFromMethod( Context context, Method method, Attributes attributes) { - WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); - if (annotation.withParent()) { + if (withParentMethodHandle == null) { return context; } - return Context.root(); + WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); + + boolean withParent = true; + try { + withParent = (boolean) withParentMethodHandle.invoke(annotation); + } catch (Throwable ignore) { + // ignore + } + + return withParent ? context : Context.root(); } private AnnotationSingletons() {} diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java index ce230fccb966..8663f805b8f0 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java @@ -8,7 +8,11 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.instrumentation.api.semconv.util.SpanNames; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.lang.reflect.Method; +import javax.annotation.Nullable; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; @@ -60,6 +64,21 @@ interface Factory { static final class InstrumentationAnnotationFactory implements Factory { + // The reason for using reflection here is that it needs to be compatible with the old version + // of @WithSpan annotation that does not include the withParent option to avoid failing the + // muzzle check. + private static MethodHandle withParentMethodHandle = null; + + static { + try { + withParentMethodHandle = + MethodHandles.publicLookup() + .findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class)); + } catch (NoSuchMethodException | IllegalAccessException ignore) { + // ignore + } + } + @Override public JoinPointRequest create(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); @@ -72,9 +91,21 @@ public JoinPointRequest create(JoinPoint joinPoint) { WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); String spanName = annotation != null ? annotation.value() : ""; SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL; - boolean withParent = annotation == null || annotation.withParent(); - return new JoinPointRequest(joinPoint, method, spanName, spanKind, withParent); + return new JoinPointRequest( + joinPoint, method, spanName, spanKind, withParentValueFrom(annotation)); + } + + private static boolean withParentValueFrom(@Nullable WithSpan annotation) { + if (annotation == null || withParentMethodHandle == null) { + return true; + } + + try { + return (boolean) withParentMethodHandle.invoke(annotation); + } catch (Throwable ignore) { + return true; + } } } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java index 618a70358376..cb22e12eca13 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java @@ -59,12 +59,9 @@ abstract class WithSpanAspect { this.requestFactory = requestFactory; } - static Context parentContext(Context parentContext, JoinPointRequest request, Attributes unused) { - if (request.withParent()) { - return parentContext; - } - - return Context.root(); + private static Context parentContext( + Context parentContext, JoinPointRequest request, Attributes unused) { + return request.withParent() ? parentContext : Context.root(); } public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable { From 37284903e5272b50af2c7bdc056471f73f5a10e2 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 29 Jan 2025 15:50:57 +0800 Subject: [PATCH 03/10] Optimization withParent option description --- .../instrumentation/annotations/WithSpan.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index c3075aaeca95..23e4d50c736d 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -6,6 +6,7 @@ package io.opentelemetry.instrumentation.annotations; import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.context.Context; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -35,6 +36,14 @@ /** Specify the {@link SpanKind} of span to be created. Defaults to {@link SpanKind#INTERNAL}. */ SpanKind kind() default SpanKind.INTERNAL; - /** Specify whether to create a new span with parent. Defaults to {@code true}. */ + /** + * Specifies whether to use the current context as the parent when creating a Span. + * + *

If set to {@code true} (default), the created span will inherit the existing parent context, + * forming part of the same trace. + * + *

If set to {@code false}, the created span will use {@link Context#root()} and have no + * parent, starting a new trace independently. + */ boolean withParent() default true; } From 3d6c140719f0c6fcd23fd5af1a819542b614ec22 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 29 Jan 2025 15:56:33 +0800 Subject: [PATCH 04/10] Optimization withParent option description --- .../opentelemetry/instrumentation/annotations/WithSpan.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index 23e4d50c736d..f4be94c6fe17 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -39,10 +39,10 @@ /** * Specifies whether to use the current context as the parent when creating a Span. * - *

If set to {@code true} (default), the created span will inherit the existing parent context, - * forming part of the same trace. + *

If set to {@code true} (default), the newly created span will use the current context as its + * parent, forming part of the same trace. * - *

If set to {@code false}, the created span will use {@link Context#root()} and have no + *

If set to {@code false}, the newly created span will use {@link Context#root()} as its * parent, starting a new trace independently. */ boolean withParent() default true; From d3e79fd680faa0680cadd17629a2f85e7316a891 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 29 Jan 2025 16:08:21 +0800 Subject: [PATCH 05/10] Optimization withParent option description --- .../instrumentation/annotations/WithSpan.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index f4be94c6fe17..fed46094e26f 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -39,11 +39,11 @@ /** * Specifies whether to use the current context as the parent when creating a Span. * - *

If set to {@code true} (default), the newly created span will use the current context as its - * parent, forming part of the same trace. + *

If set to {@code true} (default), the created span will use the current context as its + * parent, remaining within the same trace. * - *

If set to {@code false}, the newly created span will use {@link Context#root()} as its - * parent, starting a new trace independently. + *

If set to {@code false}, the created span will use {@link Context#root()} as its parent, + * starting a new, independent trace. */ boolean withParent() default true; } From bd3f68c6424e1755d5cd1a0fe095d3f2ed2d0db0 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Wed, 12 Feb 2025 01:19:06 +0800 Subject: [PATCH 06/10] Modify the withParent option name of WithSpan annotation to inheritContext. --- ...ntelemetry-instrumentation-annotations.txt | 2 +- .../instrumentation/annotations/WithSpan.java | 4 +-- .../AnnotationSingletons.java | 14 ++++----- .../test/annotation/TracedWithSpan.java | 2 +- .../annotations/JoinPointRequest.java | 31 +++++++++++-------- .../annotations/WithSpanAspect.java | 2 +- .../InstrumentationWithSpanAspectTest.java | 4 +-- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt index 1830f9bf610c..6fb2f8cde727 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt @@ -1,4 +1,4 @@ Comparing source compatibility of opentelemetry-instrumentation-annotations-2.13.0-SNAPSHOT.jar against opentelemetry-instrumentation-annotations-2.12.0.jar **** MODIFIED ANNOTATION: PUBLIC ABSTRACT io.opentelemetry.instrumentation.annotations.WithSpan (not serializable) === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 - +++* NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean withParent() + +++* NEW METHOD: PUBLIC(+) ABSTRACT(+) boolean inheritContext() diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index fed46094e26f..047a9f7a4dbe 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -37,7 +37,7 @@ SpanKind kind() default SpanKind.INTERNAL; /** - * Specifies whether to use the current context as the parent when creating a Span. + * Specifies whether to inherit the current context when creating a span. * *

If set to {@code true} (default), the created span will use the current context as its * parent, remaining within the same trace. @@ -45,5 +45,5 @@ *

If set to {@code false}, the created span will use {@link Context#root()} as its parent, * starting a new, independent trace. */ - boolean withParent() default true; + boolean inheritContext() default true; } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java index 0fcbd2e42cba..a7cd92bbfbcf 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java @@ -35,15 +35,15 @@ public final class AnnotationSingletons { private static final SpanAttributesExtractor ATTRIBUTES = createAttributesExtractor(); // The reason for using reflection here is that it needs to be compatible with the old version of - // @WithSpan annotation that does not include the withParent option to avoid failing the muzzle - // check. - private static MethodHandle withParentMethodHandle = null; + // @WithSpan annotation that does not include the inheritContext option to avoid failing the + // muzzle check. + private static MethodHandle inheritContextMethodHandle = null; static { try { - withParentMethodHandle = + inheritContextMethodHandle = MethodHandles.publicLookup() - .findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class)); + .findVirtual(WithSpan.class, "inheritContext", MethodType.methodType(boolean.class)); } catch (NoSuchMethodException | IllegalAccessException ignore) { // ignore } @@ -133,7 +133,7 @@ private static Context parentContextFromMethodRequest( private static Context parentContextFromMethod( Context context, Method method, Attributes attributes) { - if (withParentMethodHandle == null) { + if (inheritContextMethodHandle == null) { return context; } @@ -141,7 +141,7 @@ private static Context parentContextFromMethod( boolean withParent = true; try { - withParent = (boolean) withParentMethodHandle.invoke(annotation); + withParent = (boolean) inheritContextMethodHandle.invoke(annotation); } catch (Throwable ignore) { // ignore } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java index 6b449df43870..29e58a4212cc 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java @@ -58,7 +58,7 @@ public CompletableFuture completableFuture(CompletableFuture fut return future; } - @WithSpan(withParent = false) + @WithSpan(inheritContext = false) public String withoutParent() { return "hello!"; } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java index 8663f805b8f0..b9e703f04132 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java @@ -22,10 +22,14 @@ final class JoinPointRequest { private final Method method; private final String spanName; private final SpanKind spanKind; - private final boolean withParent; + private final boolean inheritContext; private JoinPointRequest( - JoinPoint joinPoint, Method method, String spanName, SpanKind spanKind, boolean withParent) { + JoinPoint joinPoint, + Method method, + String spanName, + SpanKind spanKind, + boolean inheritContext) { if (spanName.isEmpty()) { spanName = SpanNames.fromMethod(method); } @@ -34,7 +38,7 @@ private JoinPointRequest( this.method = method; this.spanName = spanName; this.spanKind = spanKind; - this.withParent = withParent; + this.inheritContext = inheritContext; } String spanName() { @@ -53,8 +57,8 @@ Object[] args() { return joinPoint.getArgs(); } - boolean withParent() { - return withParent; + boolean inheritContext() { + return inheritContext; } interface Factory { @@ -65,15 +69,16 @@ interface Factory { static final class InstrumentationAnnotationFactory implements Factory { // The reason for using reflection here is that it needs to be compatible with the old version - // of @WithSpan annotation that does not include the withParent option to avoid failing the + // of @WithSpan annotation that does not include the inheritContext option to avoid failing the // muzzle check. - private static MethodHandle withParentMethodHandle = null; + private static MethodHandle inheritContextMethodHandle = null; static { try { - withParentMethodHandle = + inheritContextMethodHandle = MethodHandles.publicLookup() - .findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class)); + .findVirtual( + WithSpan.class, "inheritContext", MethodType.methodType(boolean.class)); } catch (NoSuchMethodException | IllegalAccessException ignore) { // ignore } @@ -93,16 +98,16 @@ public JoinPointRequest create(JoinPoint joinPoint) { SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL; return new JoinPointRequest( - joinPoint, method, spanName, spanKind, withParentValueFrom(annotation)); + joinPoint, method, spanName, spanKind, inheritContextValueFrom(annotation)); } - private static boolean withParentValueFrom(@Nullable WithSpan annotation) { - if (annotation == null || withParentMethodHandle == null) { + private static boolean inheritContextValueFrom(@Nullable WithSpan annotation) { + if (annotation == null || inheritContextMethodHandle == null) { return true; } try { - return (boolean) withParentMethodHandle.invoke(annotation); + return (boolean) inheritContextMethodHandle.invoke(annotation); } catch (Throwable ignore) { return true; } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java index cb22e12eca13..89ff79dd6ab9 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java @@ -61,7 +61,7 @@ abstract class WithSpanAspect { private static Context parentContext( Context parentContext, JoinPointRequest request, Attributes unused) { - return request.withParent() ? parentContext : Context.root(); + return request.inheritContext() ? parentContext : Context.root(); } public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java index 434065dd4228..443bfdc5beb9 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java @@ -183,7 +183,7 @@ void withSpanAttributes() { @Test @DisplayName( - "when method is annotated with @WithSpan(withParent=false) should build span without parent") + "when method is annotated with @WithSpan(inheritContext=false) should build span without parent") void withSpanWithoutParent() { // when testing.runWithSpan("parent", withSpanTester::testWithoutParentSpan); @@ -244,7 +244,7 @@ public String withSpanAttributes( return "hello!"; } - @WithSpan(withParent = false) + @WithSpan(inheritContext = false) public String testWithoutParentSpan() { return "Span without parent span was created"; } From 6e6292620327da9cbbbc00130c87b29757d1c18e Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Thu, 13 Feb 2025 11:37:47 +0800 Subject: [PATCH 07/10] Modify Name --- .../AnnotationSingletons.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java index a7cd92bbfbcf..7dae901c8841 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java @@ -67,7 +67,7 @@ private static Instrumenter createInstrumenter() { INSTRUMENTATION_NAME, AnnotationSingletons::spanNameFromMethod) .addAttributesExtractor(CodeAttributesExtractor.create(MethodCodeAttributesGetter.INSTANCE)) - .addContextCustomizer(AnnotationSingletons::parentContextFromMethod) + .addContextCustomizer(AnnotationSingletons::inheritContextFromMethod) .buildInstrumenter(AnnotationSingletons::spanKindFromMethod); } @@ -83,7 +83,7 @@ private static Instrumenter createInstrumenterWithAttribu MethodRequest::method, WithSpanParameterAttributeNamesExtractor.INSTANCE, MethodRequest::args)) - .addContextCustomizer(AnnotationSingletons::parentContextFromMethodRequest) + .addContextCustomizer(AnnotationSingletons::inheritContextFromMethodRequest) .buildInstrumenter(AnnotationSingletons::spanKindFromMethodRequest); } @@ -126,12 +126,12 @@ private static String spanNameFromMethod(Method method) { return spanName; } - private static Context parentContextFromMethodRequest( + private static Context inheritContextFromMethodRequest( Context context, MethodRequest request, Attributes attributes) { - return parentContextFromMethod(context, request.method(), attributes); + return inheritContextFromMethod(context, request.method(), attributes); } - private static Context parentContextFromMethod( + private static Context inheritContextFromMethod( Context context, Method method, Attributes attributes) { if (inheritContextMethodHandle == null) { return context; @@ -139,14 +139,14 @@ private static Context parentContextFromMethod( WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); - boolean withParent = true; + boolean inheritContext = true; try { - withParent = (boolean) inheritContextMethodHandle.invoke(annotation); + inheritContext = (boolean) inheritContextMethodHandle.invoke(annotation); } catch (Throwable ignore) { // ignore } - return withParent ? context : Context.root(); + return inheritContext ? context : Context.root(); } private AnnotationSingletons() {} From c8d681a3e9f249512da630cf0799ce13ab181528 Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Fri, 14 Feb 2025 15:40:43 +0800 Subject: [PATCH 08/10] Modify Comment --- .../instrumentation/annotations/JoinPointRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java index b9e703f04132..c1cbe02c5b46 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java @@ -69,8 +69,8 @@ interface Factory { static final class InstrumentationAnnotationFactory implements Factory { // The reason for using reflection here is that it needs to be compatible with the old version - // of @WithSpan annotation that does not include the inheritContext option to avoid failing the - // muzzle check. + // of @WithSpan annotation that does not include the inheritContext option to avoid + // NoSuchMethodError private static MethodHandle inheritContextMethodHandle = null; static { From 02cbe0cf006149b36e15977670e30133f2de6b0c Mon Sep 17 00:00:00 2001 From: xiepuhuan Date: Fri, 14 Feb 2025 17:09:37 +0800 Subject: [PATCH 09/10] Use API to replace reflection to obtain inheritContext --- .../annotations/JoinPointRequest.java | 36 ++----------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java index c1cbe02c5b46..6dbfa686e4d3 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java @@ -8,11 +8,7 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.instrumentation.api.semconv.util.SpanNames; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; import java.lang.reflect.Method; -import javax.annotation.Nullable; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; @@ -68,22 +64,6 @@ interface Factory { static final class InstrumentationAnnotationFactory implements Factory { - // The reason for using reflection here is that it needs to be compatible with the old version - // of @WithSpan annotation that does not include the inheritContext option to avoid - // NoSuchMethodError - private static MethodHandle inheritContextMethodHandle = null; - - static { - try { - inheritContextMethodHandle = - MethodHandles.publicLookup() - .findVirtual( - WithSpan.class, "inheritContext", MethodType.methodType(boolean.class)); - } catch (NoSuchMethodException | IllegalAccessException ignore) { - // ignore - } - } - @Override public JoinPointRequest create(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); @@ -96,21 +76,9 @@ public JoinPointRequest create(JoinPoint joinPoint) { WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); String spanName = annotation != null ? annotation.value() : ""; SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL; + boolean inheritContext = annotation == null || annotation.inheritContext(); - return new JoinPointRequest( - joinPoint, method, spanName, spanKind, inheritContextValueFrom(annotation)); - } - - private static boolean inheritContextValueFrom(@Nullable WithSpan annotation) { - if (annotation == null || inheritContextMethodHandle == null) { - return true; - } - - try { - return (boolean) inheritContextMethodHandle.invoke(annotation); - } catch (Throwable ignore) { - return true; - } + return new JoinPointRequest(joinPoint, method, spanName, spanKind, inheritContext); } } } From 70531f5fcca8f2ac0e68e5024fa1a677952ff4f3 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 21 Feb 2025 21:41:30 +0200 Subject: [PATCH 10/10] don't use context customizer --- .../AnnotationSingletons.java | 19 ++++++------------- .../WithSpanInstrumentation.java | 5 ++--- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java index 7dae901c8841..840ccf8bd6ab 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java @@ -9,7 +9,6 @@ import application.io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.annotation.support.MethodSpanAttributesExtractor; @@ -67,7 +66,6 @@ private static Instrumenter createInstrumenter() { INSTRUMENTATION_NAME, AnnotationSingletons::spanNameFromMethod) .addAttributesExtractor(CodeAttributesExtractor.create(MethodCodeAttributesGetter.INSTANCE)) - .addContextCustomizer(AnnotationSingletons::inheritContextFromMethod) .buildInstrumenter(AnnotationSingletons::spanKindFromMethod); } @@ -83,7 +81,6 @@ private static Instrumenter createInstrumenterWithAttribu MethodRequest::method, WithSpanParameterAttributeNamesExtractor.INSTANCE, MethodRequest::args)) - .addContextCustomizer(AnnotationSingletons::inheritContextFromMethodRequest) .buildInstrumenter(AnnotationSingletons::spanKindFromMethodRequest); } @@ -126,27 +123,23 @@ private static String spanNameFromMethod(Method method) { return spanName; } - private static Context inheritContextFromMethodRequest( - Context context, MethodRequest request, Attributes attributes) { - return inheritContextFromMethod(context, request.method(), attributes); + public static Context getContextForMethod(Method method) { + return inheritContextFromMethod(method) ? Context.current() : Context.root(); } - private static Context inheritContextFromMethod( - Context context, Method method, Attributes attributes) { + private static boolean inheritContextFromMethod(Method method) { if (inheritContextMethodHandle == null) { - return context; + return true; } WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class); - - boolean inheritContext = true; try { - inheritContext = (boolean) inheritContextMethodHandle.invoke(annotation); + return (boolean) inheritContextMethodHandle.invoke(annotation); } catch (Throwable ignore) { // ignore } - return inheritContext ? context : Context.root(); + return true; } private AnnotationSingletons() {} diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java index 9cfcc18666aa..74d9d84c00d3 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java @@ -19,7 +19,6 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndSupport; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import java.lang.reflect.Method; @@ -91,7 +90,7 @@ public static void onEnter( method = originMethod; Instrumenter instrumenter = instrumenter(); - Context current = Java8BytecodeBridge.currentContext(); + Context current = AnnotationSingletons.getContextForMethod(method); if (instrumenter.shouldStart(current, method)) { context = instrumenter.start(current, method); @@ -134,7 +133,7 @@ public static void onEnter( method = originMethod; Instrumenter instrumenter = instrumenterWithAttributes(); - Context current = Java8BytecodeBridge.currentContext(); + Context current = AnnotationSingletons.getContextForMethod(method); request = new MethodRequest(method, args); if (instrumenter.shouldStart(current, request)) {