|
| 1 | +package datadog.trace.instrumentation.glassfish; |
| 2 | + |
| 3 | +import static java.util.Collections.singletonMap; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 5 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 6 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 7 | + |
| 8 | +import com.google.auto.service.AutoService; |
| 9 | +import datadog.trace.agent.tooling.Constants; |
| 10 | +import datadog.trace.agent.tooling.Instrumenter; |
| 11 | +import java.util.Map; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import net.bytebuddy.asm.Advice; |
| 14 | +import net.bytebuddy.description.method.MethodDescription; |
| 15 | +import net.bytebuddy.description.type.TypeDescription; |
| 16 | +import net.bytebuddy.matcher.ElementMatcher; |
| 17 | +import net.bytebuddy.matcher.ElementMatchers; |
| 18 | + |
| 19 | +/** |
| 20 | + * This instrumenter prevents a mechanism from GlassFish classloader to produces a class not found |
| 21 | + * exception in our tracer. Link to the GH issue: |
| 22 | + * https://github.com/eclipse-ee4j/glassfish/issues/22566 If a class loading is attempted, as an |
| 23 | + * example, as a resource and is it not found, then it is blacklisted. Successive attempts to load a |
| 24 | + * class as a class (not a resource) will fail because the class is not even tried. We hook into the |
| 25 | + * blacklisting method to avoid specific namespaces to be blacklisted. |
| 26 | + */ |
| 27 | +@Slf4j |
| 28 | +@AutoService(Instrumenter.class) |
| 29 | +public final class GlassfishInstrumentation extends Instrumenter.Default { |
| 30 | + |
| 31 | + public GlassfishInstrumentation() { |
| 32 | + super("glassfish"); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String[] helperClassNames() { |
| 37 | + return new String[] {Constants.class.getName()}; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public ElementMatcher<? super TypeDescription> typeMatcher() { |
| 42 | + return ElementMatchers.named( |
| 43 | + "com.sun.enterprise.v3.server.APIClassLoaderServiceImpl$APIClassLoader"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { |
| 48 | + return singletonMap( |
| 49 | + isMethod().and(named("addToBlackList")).and(takesArguments(1)), |
| 50 | + AvoidGlassFishBlacklistAdvice.class.getName()); |
| 51 | + } |
| 52 | + |
| 53 | + public static class AvoidGlassFishBlacklistAdvice { |
| 54 | + |
| 55 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 56 | + public static void preventBlacklistingOfTracerClasses( |
| 57 | + @Advice.Argument(value = 0, readOnly = false) String name) { |
| 58 | + for (String prefix : Constants.BOOTSTRAP_PACKAGE_PREFIXES) { |
| 59 | + if (name.startsWith(prefix)) { |
| 60 | + name = "__datadog_no_blacklist." + name; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments