diff --git a/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/ActionInvocationInstrumentation.java b/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/ActionInvocationInstrumentation.java index 8c831dfd2394..1d6c8dda88ec 100644 --- a/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/ActionInvocationInstrumentation.java +++ b/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/ActionInvocationInstrumentation.java @@ -17,9 +17,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import javax.annotation.Nullable; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class InvokeActionOnlyAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.This ActionInvocation actionInvocation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = Java8BytecodeBridge.currentContext(); + public static class AdviceScope { + private final Context context; + private final Scope scope; - HttpServerRoute.update( - parentContext, - CONTROLLER, - StrutsServerSpanNaming.SERVER_SPAN_NAME, - actionInvocation.getProxy()); + public AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; + } - if (!instrumenter().shouldStart(parentContext, actionInvocation)) { - return; + @Nullable + public static AdviceScope start(ActionInvocation actionInvocation) { + Context parentContext = Context.current(); + HttpServerRoute.update( + parentContext, + CONTROLLER, + StrutsServerSpanNaming.SERVER_SPAN_NAME, + actionInvocation.getProxy()); + + if (!instrumenter().shouldStart(parentContext, actionInvocation)) { + return null; + } + Context context = instrumenter().start(parentContext, actionInvocation); + return new AdviceScope(context, context.makeCurrent()); } - context = instrumenter().start(parentContext, actionInvocation); - scope = context.makeCurrent(); + public void end(ActionInvocation actionInvocation, @Nullable Throwable throwable) { + scope.close(); + instrumenter().end(context, actionInvocation, null, throwable); + } + } + + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope onEnter(@Advice.This ActionInvocation actionInvocation) { + return AdviceScope.start(actionInvocation); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( - @Advice.Thrown Throwable throwable, + @Advice.Thrown @Nullable Throwable throwable, @Advice.This ActionInvocation actionInvocation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; + @Advice.Enter @Nullable AdviceScope adviceScope) { + if (adviceScope != null) { + adviceScope.end(actionInvocation, throwable); } - scope.close(); - - instrumenter().end(context, actionInvocation, null, throwable); } } } diff --git a/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/Struts2InstrumentationModule.java b/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/Struts2InstrumentationModule.java index d44b85d81229..b07b4b9d7198 100644 --- a/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/Struts2InstrumentationModule.java +++ b/instrumentation/struts/struts-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v2_3/Struts2InstrumentationModule.java @@ -10,10 +10,12 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; @AutoService(InstrumentationModule.class) -public class Struts2InstrumentationModule extends InstrumentationModule { +public class Struts2InstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public Struts2InstrumentationModule() { super("struts", "struts-2.3"); @@ -23,4 +25,9 @@ public Struts2InstrumentationModule() { public List typeInstrumentations() { return singletonList(new ActionInvocationInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } } diff --git a/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.java b/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.java index 6e751570538e..c2150b7c7879 100644 --- a/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.java +++ b/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/ActionInvocationInstrumentation.java @@ -16,9 +16,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import javax.annotation.Nullable; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class InvokeActionOnlyAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.This ActionInvocation actionInvocation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = Java8BytecodeBridge.currentContext(); + public static class AdviceScope { + private final Context context; + private final Scope scope; - HttpServerRoute.update( - parentContext, - CONTROLLER, - StrutsServerSpanNaming.SERVER_SPAN_NAME, - actionInvocation.getProxy()); + public AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; + } - if (!instrumenter().shouldStart(parentContext, actionInvocation)) { - return; + @Nullable + public static AdviceScope start(ActionInvocation actionInvocation) { + Context parentContext = Context.current(); + HttpServerRoute.update( + parentContext, + CONTROLLER, + StrutsServerSpanNaming.SERVER_SPAN_NAME, + actionInvocation.getProxy()); + + if (!instrumenter().shouldStart(parentContext, actionInvocation)) { + return null; + } + Context context = instrumenter().start(parentContext, actionInvocation); + return new AdviceScope(context, context.makeCurrent()); } - context = instrumenter().start(parentContext, actionInvocation); - scope = context.makeCurrent(); + public void end(ActionInvocation actionInvocation, @Nullable Throwable throwable) { + scope.close(); + instrumenter().end(context, actionInvocation, null, throwable); + } + } + + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope onEnter(@Advice.This ActionInvocation actionInvocation) { + return AdviceScope.start(actionInvocation); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( - @Advice.Thrown Throwable throwable, + @Advice.Thrown @Nullable Throwable throwable, @Advice.This ActionInvocation actionInvocation, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; + @Advice.Enter @Nullable AdviceScope adviceScope) { + if (adviceScope != null) { + adviceScope.end(actionInvocation, throwable); } - scope.close(); - - instrumenter().end(context, actionInvocation, null, throwable); } } } diff --git a/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.java b/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.java index 901f9656c862..a838b4f53ce3 100644 --- a/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.java +++ b/instrumentation/struts/struts-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/struts/v7_0/Struts2InstrumentationModule.java @@ -11,11 +11,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) -public class Struts2InstrumentationModule extends InstrumentationModule { +public class Struts2InstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public Struts2InstrumentationModule() { super("struts", "struts-7.0"); @@ -31,4 +33,9 @@ public ElementMatcher.Junction classLoaderMatcher() { public List typeInstrumentations() { return singletonList(new ActionInvocationInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } }