|
| 1 | +package com.newrelic.instrumentation.kotlin.coroutines.tracing; |
| 2 | + |
| 3 | +import java.lang.instrument.IllegalClassFormatException; |
| 4 | +import java.security.ProtectionDomain; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.logging.Level; |
| 8 | + |
| 9 | +import com.newrelic.agent.deps.org.objectweb.asm.commons.Method; |
| 10 | +import com.newrelic.agent.instrumentation.InstrumentationType; |
| 11 | +import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; |
| 12 | +import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match; |
| 13 | +import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcherBuilder; |
| 14 | +import com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory; |
| 15 | +import com.newrelic.agent.instrumentation.context.ContextClassTransformer; |
| 16 | +import com.newrelic.agent.instrumentation.context.InstrumentationContext; |
| 17 | +import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; |
| 18 | +import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; |
| 19 | +import com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder; |
| 20 | +import com.newrelic.api.agent.NewRelic; |
| 21 | + |
| 22 | +public class SuspendClassTransformer implements ContextClassTransformer { |
| 23 | + |
| 24 | + private Map<String, ClassMatchVisitorFactory> matchers = null; |
| 25 | + private final InstrumentationContextManager contextMgr; |
| 26 | + |
| 27 | + public SuspendClassTransformer(InstrumentationContextManager mgr) { |
| 28 | + contextMgr = mgr; |
| 29 | + matchers = new HashMap<>(); |
| 30 | + } |
| 31 | + |
| 32 | + protected ClassMatchVisitorFactory addMatcher(ClassAndMethodMatcher matcher) { |
| 33 | + NewRelic.getAgent().getLogger().log(Level.FINE, "Adding matcher {0} to service classtransformer", matcher); |
| 34 | + OptimizedClassMatcherBuilder builder = OptimizedClassMatcherBuilder.newBuilder(); |
| 35 | + builder.addClassMethodMatcher(matcher); |
| 36 | + ClassMatchVisitorFactory matchVistor = builder.build(); |
| 37 | + matchers.put(matcher.getClass().getSimpleName(), matchVistor); |
| 38 | + contextMgr.addContextClassTransformer(matchVistor, this); |
| 39 | + return matchVistor; |
| 40 | + } |
| 41 | + |
| 42 | + protected void removeMatcher(ClassAndMethodMatcher matcher) { |
| 43 | + ClassMatchVisitorFactory matchVisitor = matchers.remove(matcher.getClass().getSimpleName()); |
| 44 | + if(matchVisitor != null) { |
| 45 | + contextMgr.removeMatchVisitor(matchVisitor); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, |
| 51 | + ProtectionDomain protectionDomain, byte[] classfileBuffer, InstrumentationContext context, Match match) |
| 52 | + throws IllegalClassFormatException { |
| 53 | + for(Method method : match.getMethods()) { |
| 54 | + for(ClassAndMethodMatcher matcher : match.getClassMatches().keySet()) { |
| 55 | + if (matcher.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, method.getName(), |
| 56 | + method.getDescriptor(), match.getMethodAnnotations(method))) { |
| 57 | + |
| 58 | +// context.putTraceAnnotation(method, TraceDetailsBuilder.newBuilder().setTracerFactoryName(SuspendTracerFactory.TRACER_FACTORY_NAME).setDispatcher(true).setInstrumentationSourceName("CoroutinesCore").setInstrumentationType(InstrumentationType.TraceAnnotation).build()); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments