|
| 1 | +package datadog.trace.agent.tooling.compatibility; |
| 2 | + |
| 3 | +import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; |
| 4 | + |
| 5 | +import com.google.auto.service.AutoService; |
| 6 | +import datadog.trace.agent.tooling.ExceptionHandlers; |
| 7 | +import datadog.trace.agent.tooling.Instrumenter; |
| 8 | +import datadog.trace.agent.tooling.Utils; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import net.bytebuddy.agent.builder.AgentBuilder; |
| 11 | +import net.bytebuddy.asm.Advice; |
| 12 | +import net.bytebuddy.matcher.ElementMatchers; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +@AutoService(Instrumenter.class) |
| 16 | +public final class GlassfishClassloaderBlacklistCompatibility implements Instrumenter { |
| 17 | + |
| 18 | + private static final String[] NON_BLACKLISTED_PREFIXES = { |
| 19 | + "io.opentracing", |
| 20 | + }; |
| 21 | + |
| 22 | + @Override |
| 23 | + public AgentBuilder instrument(AgentBuilder agentBuilder) { |
| 24 | + |
| 25 | + return agentBuilder |
| 26 | + .type(ElementMatchers.nameEndsWith("InternalMyService")) |
| 27 | + .transform( |
| 28 | + new AgentBuilder.Transformer.ForAdvice() |
| 29 | + .include(Utils.getBootstrapProxy(), Utils.getAgentClassLoader()) |
| 30 | + .withExceptionHandler(ExceptionHandlers.defaultExceptionHandler()) |
| 31 | + .advice(nameStartsWith("internal"), AvoidGlassFishBlacklistAdvice.class.getName())); |
| 32 | + } |
| 33 | + |
| 34 | + public static class AvoidGlassFishBlacklistAdvice { |
| 35 | + |
| 36 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 37 | + public static void preventBlacklistingOfTracerClasses( |
| 38 | + @Advice.Argument(value = 0, readOnly = false) String fullClassName) { |
| 39 | + for (String prefix : NON_BLACKLISTED_PREFIXES) { |
| 40 | + if (fullClassName.startsWith(prefix)) { |
| 41 | + if (log.isDebugEnabled()) { |
| 42 | + log.debug( |
| 43 | + "Prevented blacklisting of class {}. Stack trace is: \n{}", |
| 44 | + fullClassName, |
| 45 | + Utils.getStackTraceAsString()); |
| 46 | + } |
| 47 | + fullClassName = "__datadog_no_blacklist." + fullClassName; |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments