Skip to content

Commit a37b6e3

Browse files
committed
jetty-12.0
1 parent c1faa23 commit a37b6e3

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

instrumentation/jetty/jetty-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v12_0/Jetty12InstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import com.google.auto.service.AutoService;
1212
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1313
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
14+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1415
import java.util.List;
1516
import net.bytebuddy.matcher.ElementMatcher;
1617

1718
@AutoService(InstrumentationModule.class)
18-
public class Jetty12InstrumentationModule extends InstrumentationModule {
19+
public class Jetty12InstrumentationModule extends InstrumentationModule
20+
implements ExperimentalInstrumentationModule {
1921

2022
public Jetty12InstrumentationModule() {
2123
super("jetty", "jetty-12.0");
@@ -30,4 +32,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
3032
public List<TypeInstrumentation> typeInstrumentations() {
3133
return singletonList(new Jetty12ServerInstrumentation());
3234
}
35+
36+
@Override
37+
public boolean isIndyReady() {
38+
return true;
39+
}
3340
}

instrumentation/jetty/jetty-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/v12_0/Jetty12ServerInstrumentation.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
1717
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1818
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
19+
import javax.annotation.Nullable;
1920
import net.bytebuddy.asm.Advice;
2021
import net.bytebuddy.description.type.TypeDescription;
2122
import net.bytebuddy.matcher.ElementMatcher;
@@ -43,39 +44,53 @@ public void transform(TypeTransformer transformer) {
4344
@SuppressWarnings("unused")
4445
public static class HandlerAdvice {
4546

46-
@Advice.OnMethodEnter(suppress = Throwable.class)
47-
public static void onEnter(
48-
@Advice.This Object source,
49-
@Advice.Argument(0) Request request,
50-
@Advice.Argument(1) Response response,
51-
@Advice.Local("otelContext") Context context,
52-
@Advice.Local("otelScope") Scope scope) {
47+
public static class AdviceScope {
48+
private final Context context;
49+
private final Scope scope;
50+
51+
private AdviceScope(Context context, Scope scope) {
52+
this.context = context;
53+
this.scope = scope;
54+
}
5355

54-
Context parentContext = Java8BytecodeBridge.currentContext();
55-
if (!helper().shouldStart(parentContext, request)) {
56-
return;
56+
@Nullable
57+
public static AdviceScope start(Object source, Request request, Response response) {
58+
Context parentContext = Java8BytecodeBridge.currentContext();
59+
if (!helper().shouldStart(parentContext, request)) {
60+
return null;
61+
}
62+
Context context = helper().start(parentContext, request, response);
63+
Scope scope = context.makeCurrent();
64+
HttpServerResponseCustomizerHolder.getCustomizer()
65+
.customize(context, response, Jetty12ResponseMutator.INSTANCE);
66+
return new AdviceScope(context, scope);
5767
}
5868

59-
context = helper().start(parentContext, request, response);
60-
scope = context.makeCurrent();
69+
public void end(Request request, Response response, @Nullable Throwable throwable) {
70+
if (scope != null) {
71+
scope.close();
72+
helper().end(context, request, response, throwable);
73+
}
74+
}
75+
}
6176

62-
HttpServerResponseCustomizerHolder.getCustomizer()
63-
.customize(context, response, Jetty12ResponseMutator.INSTANCE);
77+
@Advice.OnMethodEnter(suppress = Throwable.class)
78+
@Nullable
79+
public static AdviceScope onEnter(
80+
@Advice.This Object source,
81+
@Advice.Argument(0) Request request,
82+
@Advice.Argument(1) Response response) {
83+
return AdviceScope.start(source, request, response);
6484
}
6585

6686
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6787
public static void stopSpan(
6888
@Advice.Argument(0) Request request,
6989
@Advice.Argument(1) Response response,
70-
@Advice.Thrown Throwable throwable,
71-
@Advice.Local("otelContext") Context context,
72-
@Advice.Local("otelScope") Scope scope) {
73-
if (scope == null) {
74-
return;
75-
}
76-
scope.close();
77-
if (throwable != null) {
78-
helper().end(context, request, response, throwable);
90+
@Advice.Thrown @Nullable Throwable throwable,
91+
@Advice.Enter @Nullable AdviceScope adviceScope) {
92+
if (adviceScope != null) {
93+
adviceScope.end(request, response, throwable);
7994
}
8095
}
8196
}

0 commit comments

Comments
 (0)