Skip to content

Commit 4bc04f4

Browse files
committed
Migrating ServerInstrumentation from restlet 2
1 parent 2e64c80 commit 4bc04f4

File tree

1 file changed

+48
-35
lines changed
  • instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0

1 file changed

+48
-35
lines changed

instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -44,54 +45,66 @@ public void transform(TypeTransformer transformer) {
4445

4546
@SuppressWarnings("unused")
4647
public static class ServerHandleAdvice {
48+
public static class AdviceScope {
49+
private final Context context;
50+
private final Scope scope;
4751

48-
@Advice.OnMethodEnter(suppress = Throwable.class)
49-
public static void beginRequest(
50-
@Advice.Argument(0) Request request,
51-
@Advice.Argument(1) Response response,
52-
@Advice.Local("otelContext") Context context,
53-
@Advice.Local("otelScope") Scope scope) {
54-
55-
Context parentContext = currentContext();
56-
57-
if (!instrumenter().shouldStart(parentContext, request)) {
58-
return;
52+
private AdviceScope(Context context, Scope scope) {
53+
this.context = context;
54+
this.scope = scope;
5955
}
6056

61-
context = instrumenter().start(parentContext, request);
62-
scope = context.makeCurrent();
63-
}
57+
@Nullable
58+
public static AdviceScope start(Request request) {
59+
Context parentContext = currentContext();
6460

65-
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
66-
public static void finishRequest(
67-
@Advice.Argument(0) Request request,
68-
@Advice.Argument(1) Response response,
69-
@Advice.Thrown Throwable exception,
70-
@Advice.Local("otelContext") Context context,
71-
@Advice.Local("otelScope") Scope scope) {
61+
if (!instrumenter().shouldStart(parentContext, request)) {
62+
return null;
63+
}
7264

73-
if (scope == null) {
74-
return;
65+
Context context = instrumenter().start(parentContext, request);
66+
return new AdviceScope(context, context.makeCurrent());
7567
}
7668

77-
scope.close();
69+
public void end(Throwable exception, Request request, Response response) {
70+
scope.close();
7871

79-
if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) {
80-
HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*");
81-
}
72+
if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) {
73+
HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*");
74+
}
8275

83-
HttpServerResponseCustomizerHolder.getCustomizer()
84-
.customize(context, response, RestletResponseMutator.INSTANCE);
76+
HttpServerResponseCustomizerHolder.getCustomizer()
77+
.customize(context, response, RestletResponseMutator.INSTANCE);
8578

86-
if (exception != null) {
87-
instrumenter().end(context, request, response, exception);
88-
return;
79+
if (exception != null) {
80+
instrumenter().end(context, request, response, exception);
81+
return;
82+
}
83+
84+
// Restlet suppresses exceptions and sets the throwable in status
85+
Throwable statusThrowable = response.getStatus().getThrowable();
86+
87+
instrumenter().end(context, request, response, statusThrowable);
8988
}
89+
}
9090

91-
// Restlet suppresses exceptions and sets the throwable in status
92-
Throwable statusThrowable = response.getStatus().getThrowable();
91+
@Nullable
92+
@Advice.OnMethodEnter(suppress = Throwable.class)
93+
public static AdviceScope beginRequest(
94+
@Advice.Argument(0) Request request, @Advice.Argument(1) Response response) {
95+
return AdviceScope.start(request);
96+
}
9397

94-
instrumenter().end(context, request, response, statusThrowable);
98+
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
99+
public static void finishRequest(
100+
@Advice.Argument(0) Request request,
101+
@Advice.Argument(1) Response response,
102+
@Advice.Thrown @Nullable Throwable exception,
103+
@Advice.Enter @Nullable AdviceScope adviceScope) {
104+
105+
if (adviceScope != null) {
106+
adviceScope.end(exception, request, response);
107+
}
95108
}
96109
}
97110
}

0 commit comments

Comments
 (0)