Skip to content

Commit dbd47db

Browse files
authored
Indy-ready - restlet (#15063)
1 parent 180a6cf commit dbd47db

File tree

4 files changed

+110
-72
lines changed

4 files changed

+110
-72
lines changed

instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/RestletInstrumentationModule.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.Arrays;
1213
import java.util.List;
1314

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

1719
public RestletInstrumentationModule() {
1820
super("restlet", "restlet-1.1");
@@ -22,4 +24,9 @@ public RestletInstrumentationModule() {
2224
public List<TypeInstrumentation> typeInstrumentations() {
2325
return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation());
2426
}
27+
28+
@Override
29+
public boolean isIndyReady() {
30+
return true;
31+
}
2532
}

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
}

instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.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.Arrays;
1213
import java.util.List;
1314

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

1719
public RestletInstrumentationModule() {
1820
super("restlet", "restlet-2.0");
@@ -22,4 +24,9 @@ public RestletInstrumentationModule() {
2224
public List<TypeInstrumentation> typeInstrumentations() {
2325
return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation());
2426
}
27+
28+
@Override
29+
public boolean isIndyReady() {
30+
return true;
31+
}
2532
}

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)