From 5acb6e107b27ead4300440db2c59eb4f74fc6ed9 Mon Sep 17 00:00:00 2001 From: Doug Hilpipre Date: Mon, 21 Oct 2024 15:49:19 -0500 Subject: [PATCH] temporary removal of Kotlin-Coroutines-Suspends due to errors --- Kotlin-Coroutines-Suspends/build.gradle | 32 ------- .../kotlin/coroutines/SuspendIgnores.java | 47 ----------- .../kotlin/coroutines/Utils.java | 41 --------- .../coroutines/tracing/CoroutinesPreMain.java | 84 ------------------- .../tracing/SuspendClassAndMethod.java | 27 ------ .../tracing/SuspendClassMatcher.java | 49 ----------- .../tracing/SuspendClassTransformer.java | 65 -------------- .../tracing/SuspendMethodMatcher.java | 27 ------ .../tracing/SuspendTracerFactory.java | 25 ------ 9 files changed, 397 deletions(-) delete mode 100644 Kotlin-Coroutines-Suspends/build.gradle delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/SuspendIgnores.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/Utils.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/CoroutinesPreMain.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassAndMethod.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassMatcher.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassTransformer.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendMethodMatcher.java delete mode 100644 Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendTracerFactory.java diff --git a/Kotlin-Coroutines-Suspends/build.gradle b/Kotlin-Coroutines-Suspends/build.gradle deleted file mode 100644 index 149b584..0000000 --- a/Kotlin-Coroutines-Suspends/build.gradle +++ /dev/null @@ -1,32 +0,0 @@ - -// Build.gradle generated for instrumentation module Kotlin-Coroutines-Core - -apply plugin: 'java' - -dependencies { - implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.4.0' - - // New Relic Java Agent dependencies - implementation 'com.newrelic.agent.java:newrelic-agent:6.4.1' - implementation 'com.newrelic.agent.java:newrelic-api:6.4.1' - implementation fileTree(include: ['*.jar'], dir: '../libs') - implementation fileTree(include: ['*.jar'], dir: '../test-lib') -} - -jar { - manifest { - attributes 'Implementation-Title': 'com.newrelic.instrumentation.labs.Kotlin-Coroutines-Suspends' - attributes 'Implementation-Vendor': 'New Relic Labs' - attributes 'Implementation-Vendor-Id': 'com.newrelic.labs' - attributes 'Implementation-Version': 2.0 - attributes 'Agent-Class': 'com.newrelic.instrumentation.kotlin.coroutines.tracing.CoroutinesPreMain' - } -} - -verifyInstrumentation { - // Verifier plugin documentation: - // https://github.com/newrelic/newrelic-gradle-verify-instrumentation - // Example: - // passes 'javax.servlet:servlet-api:[2.2,2.5]' - // exclude 'javax.servlet:servlet-api:2.4.public_draft' -} \ No newline at end of file diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/SuspendIgnores.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/SuspendIgnores.java deleted file mode 100644 index 712ce01..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/SuspendIgnores.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines; - -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; - -import com.newrelic.api.agent.Config; -import com.newrelic.api.agent.NewRelic; - -public class SuspendIgnores { - - private static final List ignoredSuspends = new ArrayList(); - private static final String SUSPENDSIGNORECONFIG = "Coroutines.ignores.suspends"; - - static { - Config config = NewRelic.getAgent().getConfig(); - String value = config.getValue(SUSPENDSIGNORECONFIG); - init(value); - - } - - private static void init(String value) { - if(value != null && !value.isEmpty()) { - String[] ignores = value.split(","); - for(String ignore : ignores) { - addIgnore(ignore); - } - } - } - - public static void reset(Config config) { - ignoredSuspends.clear(); - String value = config.getValue(SUSPENDSIGNORECONFIG); - init(value); - } - - public static void addIgnore(String s) { - if(!ignoredSuspends.contains(s)) { - ignoredSuspends.add(s); - NewRelic.getAgent().getLogger().log(Level.FINE, "Will ignore suspends named {0}", s); - } - } - - public static boolean ignoreSuspend(Object obj) { - return ignoredSuspends.contains(obj.toString()) || ignoredSuspends.contains(obj.getClass().getName()); - } -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/Utils.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/Utils.java deleted file mode 100644 index da25bf9..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/Utils.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines; - -import com.newrelic.agent.config.AgentConfig; -import com.newrelic.agent.config.AgentConfigListener; -import com.newrelic.agent.service.ServiceFactory; -import com.newrelic.api.agent.Config; -import com.newrelic.api.agent.NewRelic; - -public class Utils implements AgentConfigListener { - - private static final Utils INSTANCE = new Utils(); - public static final String CREATEMETHOD1 = "Continuation at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$4"; - public static final String CREATEMETHOD2 = "Continuation at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$3"; - public static String sub = "createCoroutineFromSuspendFunction"; - private static final String CONT_LOC = "Continuation at"; - - static { - ServiceFactory.getConfigService().addIAgentConfigListener(INSTANCE); - Config config = NewRelic.getAgent().getConfig(); - SuspendIgnores.reset(config); - } - - @Override - public void configChanged(String appName, AgentConfig agentConfig) { - SuspendIgnores.reset(agentConfig); - } - - public static String getSuspendString(String cont_string, Object obj) { - if(cont_string.equals(CREATEMETHOD1) || cont_string.equals(CREATEMETHOD2)) return sub; - if(cont_string.startsWith(CONT_LOC)) { - return cont_string; - } - - int index = cont_string.indexOf('@'); - if(index > -1) { - return cont_string.substring(0, index); - } - - return obj.getClass().getName(); - } -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/CoroutinesPreMain.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/CoroutinesPreMain.java deleted file mode 100644 index e1dfd9a..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/CoroutinesPreMain.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import java.lang.instrument.Instrumentation; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; - -import com.newrelic.agent.TracerService; -import com.newrelic.agent.core.CoreService; -import com.newrelic.agent.instrumentation.ClassTransformerService; -import com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory; -import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; -import com.newrelic.agent.service.ServiceFactory; -import com.newrelic.api.agent.NewRelic; - -public class CoroutinesPreMain { - - private static int max_retries = 20; - private static ScheduledExecutorService executor = null; - - public static void premain(String args, Instrumentation inst) { - boolean b = setup(); - if(!b) { - executor = Executors.newSingleThreadScheduledExecutor(); - executor.schedule(new Setup(), 100L, TimeUnit.MILLISECONDS); - } - - } - - public static boolean setup() { - - TracerService tracerService = ServiceFactory.getTracerService(); - ClassTransformerService classTransformerService = ServiceFactory.getClassTransformerService(); - CoreService coreService = ServiceFactory.getCoreService(); - - if(tracerService != null && classTransformerService != null && coreService != null) { - tracerService.registerTracerFactory(SuspendTracerFactory.TRACER_FACTORY_NAME, new SuspendTracerFactory()); - InstrumentationContextManager contextMgr = classTransformerService.getContextManager(); - - if(contextMgr != null) { - SuspendClassTransformer suspendTransformer = new SuspendClassTransformer(contextMgr); - SuspendClassAndMethod suspendMatcher = new SuspendClassAndMethod(); - ClassMatchVisitorFactory suspendMatchVistor = suspendTransformer.addMatcher(suspendMatcher); - - Set factories = new HashSet<>(); - factories.add(suspendMatchVistor); - Class[] allLoadedClasses = coreService.getInstrumentation().getAllLoadedClasses(); - - classTransformerService.retransformMatchingClassesImmediately(allLoadedClasses, factories); - - return true; - } - } - - return false; - } - - private static class Setup implements Runnable { - - private static int count = 0; - - @Override - public void run() { - count++; - NewRelic.getAgent().getLogger().log(Level.FINE, "Call {0} to attempt setting up Suspend ClassTransformer",count); - boolean b = setup(); - - if(!b) { - if(count < max_retries) { - executor.schedule(this, 2L, TimeUnit.SECONDS); - } else { - NewRelic.getAgent().getLogger().log(Level.FINE, "Failed to initiate Suspend Client Transformer after {0} tries", max_retries); - executor.shutdownNow(); - } - } - - } - - } - -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassAndMethod.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassAndMethod.java deleted file mode 100644 index bf392fd..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassAndMethod.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; -import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; -import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; - -public class SuspendClassAndMethod implements ClassAndMethodMatcher { - - private ClassMatcher classMatcher; - private MethodMatcher methodMatcher; - - public SuspendClassAndMethod() { - classMatcher = new SuspendClassMatcher(); - methodMatcher = new SuspendMethodMatcher(); - } - - @Override - public ClassMatcher getClassMatcher() { - return classMatcher; - } - - @Override - public MethodMatcher getMethodMatcher() { - return methodMatcher; - } - -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassMatcher.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassMatcher.java deleted file mode 100644 index b9093ac..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassMatcher.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import java.util.ArrayList; -import java.util.Collection; - -import com.newrelic.agent.deps.org.objectweb.asm.ClassReader; -import com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher; -import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher; -import com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher; -import com.newrelic.agent.instrumentation.classmatchers.NotMatcher; - -public class SuspendClassMatcher extends ClassMatcher { - - ChildClassMatcher matcher; - NotMatcher nrMatcher; - - public SuspendClassMatcher() { - matcher = new ChildClassMatcher("kotlin.coroutines.jvm.internal.BaseContinuationImpl",false); - nrMatcher = new NotMatcher(new ExactClassMatcher("com.newrelic.instrumentation.kotlin.coroutines.NRWrappedSuspend")); - } - - @Override - public boolean isMatch(ClassLoader loader, ClassReader cr) { - return matcher.isMatch(loader, cr) && nrMatcher.isMatch(loader, cr); - } - - @Override - public boolean isMatch(Class clazz) { - return matcher.isMatch(clazz) && nrMatcher.isMatch(clazz); - } - - @Override - public Collection getClassNames() { - Collection childClasses = matcher.getClassNames(); - Collection nrClasses = nrMatcher.getClassNames(); - if(childClasses == null && nrClasses == null) return null; - - ArrayList list = new ArrayList<>(); - if(childClasses != null) { - list.addAll(childClasses); - } - if(nrClasses != null) { - list.addAll(nrClasses); - } - - return list; - } - -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassTransformer.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassTransformer.java deleted file mode 100644 index ee81be0..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendClassTransformer.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import java.lang.instrument.IllegalClassFormatException; -import java.security.ProtectionDomain; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - -import com.newrelic.agent.deps.org.objectweb.asm.commons.Method; -import com.newrelic.agent.instrumentation.InstrumentationType; -import com.newrelic.agent.instrumentation.classmatchers.ClassAndMethodMatcher; -import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcher.Match; -import com.newrelic.agent.instrumentation.classmatchers.OptimizedClassMatcherBuilder; -import com.newrelic.agent.instrumentation.context.ClassMatchVisitorFactory; -import com.newrelic.agent.instrumentation.context.ContextClassTransformer; -import com.newrelic.agent.instrumentation.context.InstrumentationContext; -import com.newrelic.agent.instrumentation.context.InstrumentationContextManager; -import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; -import com.newrelic.agent.instrumentation.tracing.TraceDetailsBuilder; -import com.newrelic.api.agent.NewRelic; - -public class SuspendClassTransformer implements ContextClassTransformer { - - private Map matchers = null; - private final InstrumentationContextManager contextMgr; - - public SuspendClassTransformer(InstrumentationContextManager mgr) { - contextMgr = mgr; - matchers = new HashMap<>(); - } - - protected ClassMatchVisitorFactory addMatcher(ClassAndMethodMatcher matcher) { - NewRelic.getAgent().getLogger().log(Level.FINE, "Adding matcher {0} to service classtransformer", matcher); - OptimizedClassMatcherBuilder builder = OptimizedClassMatcherBuilder.newBuilder(); - builder.addClassMethodMatcher(matcher); - ClassMatchVisitorFactory matchVistor = builder.build(); - matchers.put(matcher.getClass().getSimpleName(), matchVistor); - contextMgr.addContextClassTransformer(matchVistor, this); - return matchVistor; - } - - protected void removeMatcher(ClassAndMethodMatcher matcher) { - ClassMatchVisitorFactory matchVisitor = matchers.remove(matcher.getClass().getSimpleName()); - if(matchVisitor != null) { - contextMgr.removeMatchVisitor(matchVisitor); - } - } - - @Override - public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, - ProtectionDomain protectionDomain, byte[] classfileBuffer, InstrumentationContext context, Match match) - throws IllegalClassFormatException { - for(Method method : match.getMethods()) { - for(ClassAndMethodMatcher matcher : match.getClassMatches().keySet()) { - if (matcher.getMethodMatcher().matches(MethodMatcher.UNSPECIFIED_ACCESS, method.getName(), - method.getDescriptor(), match.getMethodAnnotations(method))) { - context.putTraceAnnotation(method, TraceDetailsBuilder.newBuilder().setTracerFactoryName(SuspendTracerFactory.TRACER_FACTORY_NAME).setDispatcher(true).setInstrumentationSourceName("CoroutinesCore").setInstrumentationType(InstrumentationType.TraceAnnotation).build()); - } - - } - } - return null; - } - -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendMethodMatcher.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendMethodMatcher.java deleted file mode 100644 index be48c40..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendMethodMatcher.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import java.util.Set; - -import com.newrelic.agent.deps.org.objectweb.asm.commons.Method; -import com.newrelic.agent.instrumentation.methodmatchers.MethodMatcher; -import com.newrelic.agent.instrumentation.methodmatchers.NameMethodMatcher; - -public class SuspendMethodMatcher implements MethodMatcher { - - NameMethodMatcher matcher = null; - - public SuspendMethodMatcher() { - matcher = new NameMethodMatcher("invokeSuspend"); - } - - @Override - public boolean matches(int access, String name, String desc, Set annotations) { - return matcher.matches(access, name, desc, annotations); - } - - @Override - public Method[] getExactMethods() { - return matcher.getExactMethods(); - } - -} diff --git a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendTracerFactory.java b/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendTracerFactory.java deleted file mode 100644 index 570ad90..0000000 --- a/Kotlin-Coroutines-Suspends/src/main/java/com/newrelic/instrumentation/kotlin/coroutines/tracing/SuspendTracerFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.newrelic.instrumentation.kotlin.coroutines.tracing; - -import com.newrelic.agent.Transaction; -import com.newrelic.agent.tracers.ClassMethodSignature; -import com.newrelic.agent.tracers.DefaultTracer; -import com.newrelic.agent.tracers.Tracer; -import com.newrelic.agent.tracers.TracerFactory; -import com.newrelic.agent.tracers.metricname.SimpleMetricNameFormat; -import com.newrelic.instrumentation.kotlin.coroutines.SuspendIgnores; -import com.newrelic.instrumentation.kotlin.coroutines.Utils; - -public class SuspendTracerFactory implements TracerFactory { - - protected static final String TRACER_FACTORY_NAME = "SUSPEND_TRACER_FACTORY"; - - @Override - public Tracer getTracer(Transaction transaction, ClassMethodSignature sig, Object object, Object[] args) { - - if(SuspendIgnores.ignoreSuspend(object)) { - return null; - } - return new DefaultTracer(transaction, sig, object, new SimpleMetricNameFormat("Custom/SuspendFunction/"+Utils.getSuspendString(object.toString(), object))); - } - -}