Skip to content

Commit 2e64c80

Browse files
committed
Migrating ServerInstrumentation
1 parent f99bfb3 commit 2e64c80

File tree

1 file changed

+46
-35
lines changed
  • instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1

1 file changed

+46
-35
lines changed

instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/ServerInstrumentation.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.javaagent.instrumentation.restlet.v1_1;
77

88
import static io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource.CONTROLLER;
9-
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
109
import static io.opentelemetry.javaagent.instrumentation.restlet.v1_1.RestletSingletons.instrumenter;
1110
import static io.opentelemetry.javaagent.instrumentation.restlet.v1_1.RestletSingletons.serverSpanName;
1211
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -19,6 +18,7 @@
1918
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
2019
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2120
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
21+
import javax.annotation.Nullable;
2222
import net.bytebuddy.asm.Advice;
2323
import net.bytebuddy.description.type.TypeDescription;
2424
import net.bytebuddy.matcher.ElementMatcher;
@@ -45,53 +45,64 @@ public void transform(TypeTransformer transformer) {
4545
@SuppressWarnings("unused")
4646
public static class ServerHandleAdvice {
4747

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) {
48+
public static class AdviceScope {
49+
private final Context context;
50+
private final Scope scope;
5451

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 = Context.current();
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(@Advice.Argument(0) Request request) {
94+
return AdviceScope.start(request);
95+
}
9396

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

0 commit comments

Comments
 (0)