Skip to content

Commit 54b01fd

Browse files
authored
make jsp indy-ready (#14975)
1 parent b77dee3 commit 54b01fd

File tree

3 files changed

+71
-39
lines changed

3 files changed

+71
-39
lines changed

instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/HttpJspPageInstrumentation.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import io.opentelemetry.context.Context;
1616
import io.opentelemetry.context.Scope;
17-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1817
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1918
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
19+
import javax.annotation.Nullable;
2020
import javax.servlet.http.HttpServletRequest;
2121
import net.bytebuddy.asm.Advice;
2222
import net.bytebuddy.description.type.TypeDescription;
@@ -47,32 +47,44 @@ public void transform(TypeTransformer transformer) {
4747
@SuppressWarnings("unused")
4848
public static class HttpJspPageAdvice {
4949

50-
@Advice.OnMethodEnter(suppress = Throwable.class)
51-
public static void onEnter(
52-
@Advice.Argument(0) HttpServletRequest req,
53-
@Advice.Local("otelContext") Context context,
54-
@Advice.Local("otelScope") Scope scope) {
55-
Context parentContext = Java8BytecodeBridge.currentContext();
56-
if (!instrumenter().shouldStart(parentContext, req)) {
57-
return;
50+
public static class AdviceScope {
51+
private final Context context;
52+
private final Scope scope;
53+
54+
private AdviceScope(Context context, Scope scope) {
55+
this.context = context;
56+
this.scope = scope;
57+
}
58+
59+
@Nullable
60+
public static AdviceScope start(HttpServletRequest req) {
61+
Context parentContext = Context.current();
62+
if (!instrumenter().shouldStart(parentContext, req)) {
63+
return null;
64+
}
65+
Context context = instrumenter().start(parentContext, req);
66+
return new AdviceScope(context, context.makeCurrent());
5867
}
5968

60-
context = instrumenter().start(parentContext, req);
61-
scope = context.makeCurrent();
69+
public void end(HttpServletRequest req, Throwable throwable) {
70+
scope.close();
71+
instrumenter().end(context, req, null, throwable);
72+
}
73+
}
74+
75+
@Advice.OnMethodEnter(suppress = Throwable.class)
76+
public static AdviceScope onEnter(@Advice.Argument(0) HttpServletRequest req) {
77+
return AdviceScope.start(req);
6278
}
6379

6480
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6581
public static void stopSpan(
6682
@Advice.Argument(0) HttpServletRequest req,
67-
@Advice.Thrown Throwable throwable,
68-
@Advice.Local("otelContext") Context context,
69-
@Advice.Local("otelScope") Scope scope) {
70-
if (scope == null) {
71-
return;
83+
@Advice.Thrown @Nullable Throwable throwable,
84+
@Advice.Enter @Nullable AdviceScope adviceScope) {
85+
if (adviceScope != null) {
86+
adviceScope.end(req, throwable);
7287
}
73-
74-
scope.close();
75-
instrumenter().end(context, req, null, throwable);
7688
}
7789
}
7890
}

instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspCompilationContextInstrumentation.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
import io.opentelemetry.context.Context;
1414
import io.opentelemetry.context.Scope;
15-
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1615
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1716
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
17+
import javax.annotation.Nullable;
1818
import net.bytebuddy.asm.Advice;
1919
import net.bytebuddy.description.type.TypeDescription;
2020
import net.bytebuddy.matcher.ElementMatcher;
@@ -37,32 +37,45 @@ public void transform(TypeTransformer transformer) {
3737
@SuppressWarnings("unused")
3838
public static class CompileAdvice {
3939

40-
@Advice.OnMethodEnter(suppress = Throwable.class)
41-
public static void onEnter(
42-
@Advice.This JspCompilationContext jspCompilationContext,
43-
@Advice.Local("otelContext") Context context,
44-
@Advice.Local("otelScope") Scope scope) {
45-
Context parentContext = Java8BytecodeBridge.currentContext();
46-
if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) {
47-
return;
40+
public static class AdviceScope {
41+
private final Context context;
42+
private final Scope scope;
43+
44+
private AdviceScope(Context context, Scope scope) {
45+
this.context = context;
46+
this.scope = scope;
47+
}
48+
49+
@Nullable
50+
public static AdviceScope start(JspCompilationContext jspCompilationContext) {
51+
Context parentContext = Context.current();
52+
if (!instrumenter().shouldStart(parentContext, jspCompilationContext)) {
53+
return null;
54+
}
55+
Context context = instrumenter().start(parentContext, jspCompilationContext);
56+
return new AdviceScope(context, context.makeCurrent());
4857
}
4958

50-
context = instrumenter().start(parentContext, jspCompilationContext);
51-
scope = context.makeCurrent();
59+
public void end(@Nullable Throwable throwable, JspCompilationContext jspCompilationContext) {
60+
scope.close();
61+
instrumenter().end(context, jspCompilationContext, null, throwable);
62+
}
63+
}
64+
65+
@Nullable
66+
@Advice.OnMethodEnter(suppress = Throwable.class)
67+
public static AdviceScope onEnter(@Advice.This JspCompilationContext jspCompilationContext) {
68+
return AdviceScope.start(jspCompilationContext);
5269
}
5370

5471
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
5572
public static void stopSpan(
5673
@Advice.This JspCompilationContext jspCompilationContext,
57-
@Advice.Thrown Throwable throwable,
58-
@Advice.Local("otelContext") Context context,
59-
@Advice.Local("otelScope") Scope scope) {
60-
if (scope == null) {
61-
return;
74+
@Advice.Thrown @Nullable Throwable throwable,
75+
@Advice.Enter @Nullable AdviceScope adviceScope) {
76+
if (adviceScope != null) {
77+
adviceScope.end(throwable, jspCompilationContext);
6278
}
63-
64-
scope.close();
65-
instrumenter().end(context, jspCompilationContext, null, throwable);
6679
}
6780
}
6881
}

instrumentation/jsp-2.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jsp/JspInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class JspInstrumentationModule extends InstrumentationModule {
17+
public class JspInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719
public JspInstrumentationModule() {
1820
super("jsp", "jsp-2.3");
1921
}
@@ -22,4 +24,9 @@ public JspInstrumentationModule() {
2224
public List<TypeInstrumentation> typeInstrumentations() {
2325
return asList(new HttpJspPageInstrumentation(), new JspCompilationContextInstrumentation());
2426
}
27+
28+
@Override
29+
public boolean isIndyReady() {
30+
return true;
31+
}
2532
}

0 commit comments

Comments
 (0)