diff --git a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/HttpJspPageInstrumentation.java b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/HttpJspPageInstrumentation.java index 2d9230fcb436..15e6808862e1 100644 --- a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/HttpJspPageInstrumentation.java +++ b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/HttpJspPageInstrumentation.java @@ -14,9 +14,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; -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 javax.servlet.http.HttpServletRequest; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -47,32 +47,44 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class HttpJspPageAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.Argument(0) HttpServletRequest req, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = Java8BytecodeBridge.currentContext(); - if (!instrumenter().shouldStart(parentContext, req)) { - return; + public static class AdviceScope { + private final Context context; + private final Scope scope; + + private AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; + } + + @Nullable + public static AdviceScope start(HttpServletRequest req) { + Context parentContext = Context.current(); + if (!instrumenter().shouldStart(parentContext, req)) { + return null; + } + Context context = instrumenter().start(parentContext, req); + return new AdviceScope(context, context.makeCurrent()); } - context = instrumenter().start(parentContext, req); - scope = context.makeCurrent(); + public void end(HttpServletRequest req, Throwable throwable) { + scope.close(); + instrumenter().end(context, req, null, throwable); + } + } + + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope onEnter(@Advice.Argument(0) HttpServletRequest req) { + return AdviceScope.start(req); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( @Advice.Argument(0) HttpServletRequest req, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; + @Advice.Thrown @Nullable Throwable throwable, + @Advice.Enter @Nullable AdviceScope adviceScope) { + if (adviceScope != null) { + adviceScope.end(req, throwable); } - - scope.close(); - instrumenter().end(context, req, null, throwable); } } } diff --git a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspCompilationContextInstrumentation.java b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspCompilationContextInstrumentation.java index 250e61731a73..a4e64da4a5f1 100644 --- a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspCompilationContextInstrumentation.java +++ b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspCompilationContextInstrumentation.java @@ -12,9 +12,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; -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; @@ -37,32 +37,45 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class CompileAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void onEnter( - @Advice.This JspCompilationContext jspCompilationContext, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - Context parentContext = Java8BytecodeBridge.currentContext(); - if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) { - return; + public static class AdviceScope { + private final Context context; + private final Scope scope; + + private AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; + } + + @Nullable + public static AdviceScope start(JspCompilationContext jspCompilationContext) { + Context parentContext = Context.current(); + if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) { + return null; + } + Context context = instrumenter().start(parentContext, jspCompilationContext); + return new AdviceScope(context, context.makeCurrent()); } - context = instrumenter().start(parentContext, jspCompilationContext); - scope = context.makeCurrent(); + public void end(@Nullable Throwable throwable, JspCompilationContext jspCompilationContext) { + scope.close(); + instrumenter().end(context, jspCompilationContext, null, throwable); + } + } + + @Nullable + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope onEnter(@Advice.This JspCompilationContext jspCompilationContext) { + return AdviceScope.start(jspCompilationContext); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( @Advice.This JspCompilationContext jspCompilationContext, - @Advice.Thrown Throwable throwable, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - if (scope == null) { - return; + @Advice.Thrown @Nullable Throwable throwable, + @Advice.Enter @Nullable AdviceScope adviceScope) { + if (adviceScope != null) { + adviceScope.end(throwable, jspCompilationContext); } - - scope.close(); - instrumenter().end(context, jspCompilationContext, null, throwable); } } } diff --git a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspInstrumentationModule.java b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspInstrumentationModule.java index 274e55b3d620..8328f55a6761 100644 --- a/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspInstrumentationModule.java +++ b/instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspInstrumentationModule.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 JspInstrumentationModule extends InstrumentationModule { +public class JspInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public JspInstrumentationModule() { super("jsp", "jsp-2.3"); } @@ -22,4 +24,9 @@ public JspInstrumentationModule() { public List typeInstrumentations() { return asList(new HttpJspPageInstrumentation(), new JspCompilationContextInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } }