Skip to content

Commit f88aa36

Browse files
committed
make jsp indy-ready
1 parent fb50649 commit f88aa36

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1818
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
20+
import javax.annotation.Nullable;
2021
import javax.servlet.http.HttpServletRequest;
2122
import net.bytebuddy.asm.Advice;
2223
import net.bytebuddy.description.type.TypeDescription;
@@ -47,32 +48,44 @@ public void transform(TypeTransformer transformer) {
4748
@SuppressWarnings("unused")
4849
public static class HttpJspPageAdvice {
4950

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;
51+
public static class AdviceScope {
52+
private final Context context;
53+
private final Scope scope;
54+
55+
private AdviceScope(Context context, Scope scope) {
56+
this.context = context;
57+
this.scope = scope;
58+
}
59+
60+
@Nullable
61+
public static AdviceScope start(HttpServletRequest req) {
62+
Context parentContext = Java8BytecodeBridge.currentContext();
63+
if (!instrumenter().shouldStart(parentContext, req)) {
64+
return null;
65+
}
66+
Context context = instrumenter().start(parentContext, req);
67+
return new AdviceScope(context, context.makeCurrent());
5868
}
5969

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

6481
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6582
public static void stopSpan(
6683
@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;
84+
@Advice.Thrown @Nullable Throwable throwable,
85+
@Advice.Enter @Nullable AdviceScope adviceScope) {
86+
if (adviceScope != null) {
87+
adviceScope.end(req, throwable);
7288
}
73-
74-
scope.close();
75-
instrumenter().end(context, req, null, throwable);
7689
}
7790
}
7891
}

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)