|
| 1 | +package com.newrelic.instrumentation.kotlin.coroutines.tracing; |
| 2 | + |
| 3 | +import java.lang.instrument.Instrumentation; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.concurrent.Executors; |
| 7 | +import java.util.concurrent.ScheduledExecutorService; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.logging.Level; |
| 10 | + |
| 11 | +import com.newrelic.agent.TracerService; |
| 12 | +import com.newrelic.agent.core.CoreService; |
| 13 | +import com.newrelic.agent.instrumentation.ClassTransformerService; |
| 14 | +import com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory; |
| 15 | +import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; |
| 16 | +import com.newrelic.agent.service.ServiceFactory; |
| 17 | +import com.newrelic.api.agent.NewRelic; |
| 18 | + |
| 19 | +public class CoroutinesPreMain { |
| 20 | + |
| 21 | + private static int max_retries = 20; |
| 22 | + private static ScheduledExecutorService executor = null; |
| 23 | + |
| 24 | + public static void premain(String args, Instrumentation inst) { |
| 25 | + boolean b = setup(); |
| 26 | + if(!b) { |
| 27 | + executor = Executors.newSingleThreadScheduledExecutor(); |
| 28 | + executor.schedule(new Setup(), 100L, TimeUnit.MILLISECONDS); |
| 29 | + } |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static boolean setup() { |
| 34 | + |
| 35 | + TracerService tracerService = ServiceFactory.getTracerService(); |
| 36 | + ClassTransformerService classTransformerService = ServiceFactory.getClassTransformerService(); |
| 37 | + CoreService coreService = ServiceFactory.getCoreService(); |
| 38 | + |
| 39 | + if(tracerService != null && classTransformerService != null && coreService != null) { |
| 40 | + tracerService.registerTracerFactory(SuspendTracerFactory.TRACER_FACTORY_NAME, new SuspendTracerFactory()); |
| 41 | + InstrumentationContextManager contextMgr = classTransformerService.getContextManager(); |
| 42 | + |
| 43 | + if(contextMgr != null) { |
| 44 | + SuspendClassTransformer suspendTransformer = new SuspendClassTransformer(contextMgr); |
| 45 | + SuspendClassAndMethod suspendMatcher = new SuspendClassAndMethod(); |
| 46 | + ClassMatchVisitorFactory suspendMatchVistor = suspendTransformer.addMatcher(suspendMatcher); |
| 47 | + |
| 48 | + Set<ClassMatchVisitorFactory> factories = new HashSet<>(); |
| 49 | + factories.add(suspendMatchVistor); |
| 50 | + Class<?>[] allLoadedClasses = coreService.getInstrumentation().getAllLoadedClasses(); |
| 51 | + |
| 52 | + classTransformerService.retransformMatchingClassesImmediately(allLoadedClasses, factories); |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + private static class Setup implements Runnable { |
| 62 | + |
| 63 | + private static int count = 0; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void run() { |
| 67 | + count++; |
| 68 | + NewRelic.getAgent().getLogger().log(Level.FINE, "Call {0} to attempt setting up Suspend ClassTransformer",count); |
| 69 | + boolean b = setup(); |
| 70 | + |
| 71 | + if(!b) { |
| 72 | + if(count < max_retries) { |
| 73 | + executor.schedule(this, 2L, TimeUnit.SECONDS); |
| 74 | + } else { |
| 75 | + NewRelic.getAgent().getLogger().log(Level.FINE, "Failed to initiate Suspend Client Transformer after {0} tries", max_retries); |
| 76 | + executor.shutdownNow(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments