From 5742abe5105b5e29ea57c359b84777fd63acd870 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Sat, 3 Feb 2024 14:31:05 +0200 Subject: [PATCH] Allow dynamic import for io.opentelemetry when matching (#10385) --- .../internal/osgi/EclipseOsgiInstrumentation.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/instrumentation/internal/internal-eclipse-osgi-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/osgi/EclipseOsgiInstrumentation.java b/instrumentation/internal/internal-eclipse-osgi-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/osgi/EclipseOsgiInstrumentation.java index 8f3b4d81a162..e4a8bda14df1 100644 --- a/instrumentation/internal/internal-eclipse-osgi-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/osgi/EclipseOsgiInstrumentation.java +++ b/instrumentation/internal/internal-eclipse-osgi-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/osgi/EclipseOsgiInstrumentation.java @@ -23,7 +23,8 @@ * *

Any side-effect of the ClassLoaderMatcher's call to ClassLoader.getResource() is generally * undesirable, and so this instrumentation patches the behavior and suppresses the "dynamic import" - * of the missing package/bundle when the call is originating from ClassLoaderMatcher.. + * of the missing package/bundle when the call is originating from ClassLoaderMatcher, unless the + * request is for a package for which we explicitly allow the dynamic imports. */ class EclipseOsgiInstrumentation implements TypeInstrumentation { @@ -45,8 +46,10 @@ public static class IsDynamicallyImportedAdvice { // "skipOn" is used to skip execution of the instrumented method when a ClassLoaderMatcher is // currently executing, since we will be returning false regardless in onExit below @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class) - public static boolean onEnter() { - return InClassLoaderMatcher.get(); + public static boolean onEnter(@Advice.Argument(0) String packageName) { + // disable dynamic imports for everything except io.opentelemetry classes to allow dynamic + // import of @WithSpan etc. + return InClassLoaderMatcher.get() && !packageName.startsWith("io.opentelemetry."); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)