Skip to content

Commit 331fdca

Browse files
authored
make jodd-http indy-ready (#15124)
1 parent 55d6ae4 commit 331fdca

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

instrumentation/jodd-http-4.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/joddhttp/v4_2/JoddHttpInstrumentation.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.javaagent.instrumentation.joddhttp.v4_2;
77

8-
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
98
import static io.opentelemetry.javaagent.instrumentation.joddhttp.v4_2.JoddHttpSingletons.instrumenter;
109
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1110
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -15,6 +14,7 @@
1514
import io.opentelemetry.context.Scope;
1615
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1716
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
17+
import javax.annotation.Nullable;
1818
import jodd.http.HttpRequest;
1919
import jodd.http.HttpResponse;
2020
import net.bytebuddy.asm.Advice;
@@ -38,31 +38,46 @@ public void transform(TypeTransformer transformer) {
3838
@SuppressWarnings("unused")
3939
public static class RequestAdvice {
4040

41-
@Advice.OnMethodEnter(suppress = Throwable.class)
42-
public static void methodEnter(
43-
@Advice.This HttpRequest request,
44-
@Advice.Local("otelContext") Context context,
45-
@Advice.Local("otelScope") Scope scope) {
46-
Context parentContext = currentContext();
47-
if (!instrumenter().shouldStart(parentContext, request)) {
48-
return;
41+
public static class AdviceScope {
42+
private final Context context;
43+
private final Scope scope;
44+
45+
private AdviceScope(Context context, Scope scope) {
46+
this.context = context;
47+
this.scope = scope;
48+
}
49+
50+
@Nullable
51+
public static AdviceScope start(HttpRequest request) {
52+
Context parentContext = Context.current();
53+
if (!instrumenter().shouldStart(parentContext, request)) {
54+
return null;
55+
}
56+
Context context = instrumenter().start(parentContext, request);
57+
return new AdviceScope(context, context.makeCurrent());
4958
}
50-
context = instrumenter().start(parentContext, request);
51-
scope = context.makeCurrent();
59+
60+
public void end(HttpRequest request, HttpResponse response, Throwable throwable) {
61+
scope.close();
62+
instrumenter().end(context, request, response, throwable);
63+
}
64+
}
65+
66+
@Nullable
67+
@Advice.OnMethodEnter(suppress = Throwable.class)
68+
public static AdviceScope methodEnter(@Advice.This HttpRequest request) {
69+
return AdviceScope.start(request);
5270
}
5371

5472
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
5573
public static void methodExit(
5674
@Advice.This HttpRequest request,
57-
@Advice.Return HttpResponse response,
58-
@Advice.Thrown Throwable throwable,
59-
@Advice.Local("otelContext") Context context,
60-
@Advice.Local("otelScope") Scope scope) {
61-
if (scope == null) {
62-
return;
75+
@Advice.Return @Nullable HttpResponse response,
76+
@Advice.Thrown @Nullable Throwable throwable,
77+
@Advice.Enter @Nullable AdviceScope adviceScope) {
78+
if (adviceScope != null) {
79+
adviceScope.end(request, response, throwable);
6380
}
64-
scope.close();
65-
instrumenter().end(context, request, response, throwable);
6681
}
6782
}
6883
}

instrumentation/jodd-http-4.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/joddhttp/v4_2/JoddHttpInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import com.google.auto.service.AutoService;
99
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1010
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
11+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1112
import java.util.Collections;
1213
import java.util.List;
1314

1415
@AutoService(InstrumentationModule.class)
15-
public class JoddHttpInstrumentationModule extends InstrumentationModule {
16+
public class JoddHttpInstrumentationModule extends InstrumentationModule
17+
implements ExperimentalInstrumentationModule {
1618

1719
public JoddHttpInstrumentationModule() {
1820
super("jodd-http", "jodd-http-4.2");
@@ -22,4 +24,9 @@ public JoddHttpInstrumentationModule() {
2224
public List<TypeInstrumentation> typeInstrumentations() {
2325
return Collections.singletonList(new JoddHttpInstrumentation());
2426
}
27+
28+
@Override
29+
public boolean isIndyReady() {
30+
return true;
31+
}
2532
}

0 commit comments

Comments
 (0)