diff --git a/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/RestletInstrumentationModule.java b/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/RestletInstrumentationModule.java index a346d76d93d1..3910864551fe 100644 --- a/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/RestletInstrumentationModule.java +++ b/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/RestletInstrumentationModule.java @@ -8,11 +8,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.Arrays; import java.util.List; @AutoService(InstrumentationModule.class) -public class RestletInstrumentationModule extends InstrumentationModule { +public class RestletInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public RestletInstrumentationModule() { super("restlet", "restlet-1.1"); @@ -22,4 +24,9 @@ public RestletInstrumentationModule() { public List typeInstrumentations() { return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } } diff --git a/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/ServerInstrumentation.java b/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/ServerInstrumentation.java index 1fe56462efba..f4544aa0c128 100644 --- a/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/ServerInstrumentation.java +++ b/instrumentation/restlet/restlet-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v1_1/ServerInstrumentation.java @@ -6,7 +6,6 @@ package io.opentelemetry.javaagent.instrumentation.restlet.v1_1; import static io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource.CONTROLLER; -import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; import static io.opentelemetry.javaagent.instrumentation.restlet.v1_1.RestletSingletons.instrumenter; import static io.opentelemetry.javaagent.instrumentation.restlet.v1_1.RestletSingletons.serverSpanName; import static net.bytebuddy.matcher.ElementMatchers.isMethod; @@ -19,6 +18,7 @@ import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import javax.annotation.Nullable; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -45,53 +45,64 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class ServerHandleAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void beginRequest( - @Advice.Argument(0) Request request, - @Advice.Argument(1) Response response, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + public static class AdviceScope { + private final Context context; + private final Scope scope; - Context parentContext = currentContext(); - - if (!instrumenter().shouldStart(parentContext, request)) { - return; + private AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; } - context = instrumenter().start(parentContext, request); - scope = context.makeCurrent(); - } + @Nullable + public static AdviceScope start(Request request) { + Context parentContext = Context.current(); - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void finishRequest( - @Advice.Argument(0) Request request, - @Advice.Argument(1) Response response, - @Advice.Thrown Throwable exception, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } - if (scope == null) { - return; + Context context = instrumenter().start(parentContext, request); + return new AdviceScope(context, context.makeCurrent()); } - scope.close(); + public void end(Throwable exception, Request request, Response response) { + scope.close(); - if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { - HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*"); - } + if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { + HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*"); + } - HttpServerResponseCustomizerHolder.getCustomizer() - .customize(context, response, RestletResponseMutator.INSTANCE); + HttpServerResponseCustomizerHolder.getCustomizer() + .customize(context, response, RestletResponseMutator.INSTANCE); - if (exception != null) { - instrumenter().end(context, request, response, exception); - return; + if (exception != null) { + instrumenter().end(context, request, response, exception); + return; + } + + // Restlet suppresses exceptions and sets the throwable in status + Throwable statusThrowable = response.getStatus().getThrowable(); + + instrumenter().end(context, request, response, statusThrowable); } + } - // Restlet suppresses exceptions and sets the throwable in status - Throwable statusThrowable = response.getStatus().getThrowable(); + @Nullable + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope beginRequest(@Advice.Argument(0) Request request) { + return AdviceScope.start(request); + } - instrumenter().end(context, request, response, statusThrowable); + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void finishRequest( + @Advice.Argument(0) Request request, + @Advice.Argument(1) Response response, + @Advice.Thrown @Nullable Throwable exception, + @Advice.Enter @Nullable AdviceScope adviceScope) { + if (adviceScope != null) { + adviceScope.end(exception, request, response); + } } } } diff --git a/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.java b/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.java index 6e3e5c8c1371..0afe7b787562 100644 --- a/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.java +++ b/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/RestletInstrumentationModule.java @@ -8,11 +8,13 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.Arrays; import java.util.List; @AutoService(InstrumentationModule.class) -public class RestletInstrumentationModule extends InstrumentationModule { +public class RestletInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public RestletInstrumentationModule() { super("restlet", "restlet-2.0"); @@ -22,4 +24,9 @@ public RestletInstrumentationModule() { public List typeInstrumentations() { return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } } diff --git a/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.java b/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.java index 09bfa1fbd7e4..d6c6a14712e1 100644 --- a/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.java +++ b/instrumentation/restlet/restlet-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/restlet/v2_0/ServerInstrumentation.java @@ -19,6 +19,7 @@ import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import javax.annotation.Nullable; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -44,54 +45,66 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class ServerHandleAdvice { + public static class AdviceScope { + private final Context context; + private final Scope scope; - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void beginRequest( - @Advice.Argument(0) Request request, - @Advice.Argument(1) Response response, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { - - Context parentContext = currentContext(); - - if (!instrumenter().shouldStart(parentContext, request)) { - return; + private AdviceScope(Context context, Scope scope) { + this.context = context; + this.scope = scope; } - context = instrumenter().start(parentContext, request); - scope = context.makeCurrent(); - } + @Nullable + public static AdviceScope start(Request request) { + Context parentContext = currentContext(); - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) - public static void finishRequest( - @Advice.Argument(0) Request request, - @Advice.Argument(1) Response response, - @Advice.Thrown Throwable exception, - @Advice.Local("otelContext") Context context, - @Advice.Local("otelScope") Scope scope) { + if (!instrumenter().shouldStart(parentContext, request)) { + return null; + } - if (scope == null) { - return; + Context context = instrumenter().start(parentContext, request); + return new AdviceScope(context, context.makeCurrent()); } - scope.close(); + public void end(Throwable exception, Request request, Response response) { + scope.close(); - if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { - HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*"); - } + if (Status.CLIENT_ERROR_NOT_FOUND.equals(response.getStatus())) { + HttpServerRoute.update(context, CONTROLLER, serverSpanName(), "/*"); + } - HttpServerResponseCustomizerHolder.getCustomizer() - .customize(context, response, RestletResponseMutator.INSTANCE); + HttpServerResponseCustomizerHolder.getCustomizer() + .customize(context, response, RestletResponseMutator.INSTANCE); - if (exception != null) { - instrumenter().end(context, request, response, exception); - return; + if (exception != null) { + instrumenter().end(context, request, response, exception); + return; + } + + // Restlet suppresses exceptions and sets the throwable in status + Throwable statusThrowable = response.getStatus().getThrowable(); + + instrumenter().end(context, request, response, statusThrowable); } + } - // Restlet suppresses exceptions and sets the throwable in status - Throwable statusThrowable = response.getStatus().getThrowable(); + @Nullable + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope beginRequest( + @Advice.Argument(0) Request request, @Advice.Argument(1) Response response) { + return AdviceScope.start(request); + } - instrumenter().end(context, request, response, statusThrowable); + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + public static void finishRequest( + @Advice.Argument(0) Request request, + @Advice.Argument(1) Response response, + @Advice.Thrown @Nullable Throwable exception, + @Advice.Enter @Nullable AdviceScope adviceScope) { + + if (adviceScope != null) { + adviceScope.end(exception, request, response); + } } } }