Skip to content

Commit bb1b40d

Browse files
committed
changed entry point for tracking suspends
1 parent 130c614 commit bb1b40d

File tree

28 files changed

+65
-864
lines changed

28 files changed

+65
-864
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,35 @@
11
package com.newrelic.instrumentation.kotlin.coroutines.tracing;
22

3-
import java.util.ArrayList;
43
import java.util.Collection;
54

65
import com.newrelic.agent.deps.org.objectweb.asm.ClassReader;
76
import com.newrelic.agent.instrumentation.classmatchers.ChildClassMatcher;
87
import com.newrelic.agent.instrumentation.classmatchers.ClassMatcher;
9-
import com.newrelic.agent.instrumentation.classmatchers.ExactClassMatcher;
10-
import com.newrelic.agent.instrumentation.classmatchers.NotMatcher;
8+
import com.newrelic.agent.instrumentation.classmatchers.OrClassMatcher;
119

1210
public class SuspendClassMatcher extends ClassMatcher {
1311

14-
ChildClassMatcher matcher;
15-
NotMatcher nrMatcher;
12+
private ClassMatcher orMatcher;
1613

1714
public SuspendClassMatcher() {
18-
matcher = new ChildClassMatcher("kotlin.coroutines.jvm.internal.BaseContinuationImpl",false);
19-
nrMatcher = new NotMatcher(new ExactClassMatcher("com.newrelic.instrumentation.kotlin.coroutines.NRWrappedSuspend"));
15+
ChildClassMatcher lambdamatcher = new ChildClassMatcher("kotlin.coroutines.jvm.internal.SuspendLambda",true);
16+
ChildClassMatcher restrictedlambdamatcher = new ChildClassMatcher("kotlin.coroutines.jvm.internal.RestrictedSuspendLambda",true);
17+
orMatcher = OrClassMatcher.getClassMatcher(lambdamatcher,restrictedlambdamatcher);
2018
}
2119

2220
@Override
2321
public boolean isMatch(ClassLoader loader, ClassReader cr) {
24-
return matcher.isMatch(loader, cr) && nrMatcher.isMatch(loader, cr);
22+
return orMatcher.isMatch(loader, cr);
2523
}
2624

2725
@Override
2826
public boolean isMatch(Class<?> clazz) {
29-
return matcher.isMatch(clazz) && nrMatcher.isMatch(clazz);
27+
return orMatcher.isMatch(clazz);
3028
}
3129

3230
@Override
3331
public Collection<String> getClassNames() {
34-
Collection<String> childClasses = matcher.getClassNames();
35-
Collection<String> nrClasses = nrMatcher.getClassNames();
36-
if(childClasses == null && nrClasses == null) return null;
37-
38-
ArrayList<String> list = new ArrayList<>();
39-
if(childClasses != null) {
40-
list.addAll(childClasses);
41-
}
42-
if(nrClasses != null) {
43-
list.addAll(nrClasses);
44-
}
45-
46-
return list;
32+
return orMatcher.getClassNames();
4733
}
4834

4935
}

Kotlin-Coroutines_1.4/src/main/java/com/newrelic/instrumentation/kotlin/coroutines_14/NRContinuationWrapper.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

Kotlin-Coroutines_1.4/src/main/java/com/newrelic/instrumentation/kotlin/coroutines_14/NRFunction1Wrapper.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

Kotlin-Coroutines_1.4/src/main/java/com/newrelic/instrumentation/kotlin/coroutines_14/NRFunction2Wrapper.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

Kotlin-Coroutines_1.4/src/main/java/kotlin/coroutines/ContinuationKt.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import com.newrelic.api.agent.Trace;
44
import com.newrelic.api.agent.weaver.Weave;
55
import com.newrelic.api.agent.weaver.Weaver;
6-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRFunction1Wrapper;
7-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRFunction2Wrapper;
86

97
import kotlin.jvm.functions.Function1;
108
import kotlin.jvm.functions.Function2;
@@ -14,19 +12,11 @@ public abstract class ContinuationKt {
1412

1513
@Trace
1614
public static <T> void startCoroutine(Function1<? super Continuation<? super T>, ? extends Object> f, Continuation<? super T> cont ) {
17-
if(!(f instanceof NRFunction1Wrapper)) {
18-
NRFunction1Wrapper<? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction1Wrapper<>(f);
19-
f = wrapper;
20-
}
2115
Weaver.callOriginal();
2216
}
2317

2418
@Trace
2519
public static <R, T> void startCoroutine(Function2<? super R, ? super Continuation<? super T>, ? extends Object> f, R receiver, Continuation<? super T> cont) {
26-
if(!(f instanceof NRFunction2Wrapper)) {
27-
NRFunction2Wrapper<? super R, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper<>(f);
28-
f = wrapper;
29-
}
3020
Weaver.callOriginal();
3121
}
3222

Kotlin-Coroutines_1.4/src/main/java/kotlinx/coroutines/AbstractCoroutine.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package kotlinx.coroutines;
22

33
import com.newrelic.api.agent.NewRelic;
4+
import com.newrelic.api.agent.Token;
45
import com.newrelic.api.agent.Trace;
56
import com.newrelic.api.agent.TracedMethod;
67
import com.newrelic.api.agent.TransactionNamePriority;
78
import com.newrelic.api.agent.weaver.MatchType;
89
import com.newrelic.api.agent.weaver.Weave;
910
import com.newrelic.api.agent.weaver.Weaver;
10-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRFunction1Wrapper;
11-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRFunction2Wrapper;
1211
import com.newrelic.instrumentation.kotlin.coroutines_14.Utils;
1312

1413
import kotlin.coroutines.Continuation;
@@ -39,10 +38,6 @@ protected void onCancelled(Throwable t, boolean b) {
3938

4039
@Trace
4140
public void start(CoroutineStart start, Function1<? super Continuation<? super T>, ? extends Object> block) {
42-
if(!(block instanceof NRFunction1Wrapper)) {
43-
NRFunction1Wrapper<? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction1Wrapper<>(block);
44-
block = wrapper;
45-
}
4641
String ctxName = Utils.getCoroutineName(getContext());
4742
String name = ctxName != null ? ctxName : nameString$kotlinx_coroutines_core();
4843
TracedMethod traced = NewRelic.getAgent().getTracedMethod();
@@ -60,10 +55,6 @@ public void start(CoroutineStart start, Function1<? super Continuation<? super T
6055

6156
@Trace
6257
public <R> void start(CoroutineStart start, R receiver, Function2<? super R, ? super Continuation<? super T>, ? extends Object> block) {
63-
if(!(block instanceof NRFunction2Wrapper)) {
64-
NRFunction2Wrapper<? super R, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper<>(block);
65-
block = wrapper;
66-
}
6758
String ctxName = Utils.getCoroutineName(getContext());
6859
String name = ctxName != null ? ctxName : nameString$kotlinx_coroutines_core();
6960
TracedMethod traced = NewRelic.getAgent().getTracedMethod();
@@ -80,4 +71,12 @@ public <R> void start(CoroutineStart start, R receiver, Function2<? super R, ? s
8071
Weaver.callOriginal();
8172
}
8273

74+
@Trace(async = true)
75+
public void resumeWith(Object result) {
76+
Token token = Utils.getToken(getContext());
77+
if(token != null) {
78+
token.link();
79+
}
80+
Weaver.callOriginal();
81+
}
8382
}

Kotlin-Coroutines_1.4/src/main/java/kotlinx/coroutines/BuildersKt.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import com.newrelic.api.agent.Trace;
66
import com.newrelic.api.agent.weaver.Weave;
77
import com.newrelic.api.agent.weaver.Weaver;
8-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRContinuationWrapper;
98
import com.newrelic.instrumentation.kotlin.coroutines_14.NRCoroutineToken;
10-
import com.newrelic.instrumentation.kotlin.coroutines_14.NRFunction2Wrapper;
119
import com.newrelic.instrumentation.kotlin.coroutines_14.Utils;
1210

1311
import kotlin.Unit;
1412
import kotlin.coroutines.Continuation;
1513
import kotlin.coroutines.CoroutineContext;
16-
import kotlin.coroutines.jvm.internal.SuspendFunction;
1714
import kotlin.jvm.functions.Function2;
1815

19-
@SuppressWarnings({ "unchecked", "rawtypes" })
2016
@Weave
2117
public class BuildersKt {
2218

@@ -39,10 +35,6 @@ public static final <T> T runBlocking(CoroutineContext context, Function2<? supe
3935
}
4036
NewRelic.getAgent().getTracedMethod().addCustomAttribute("Block", block.toString());
4137

42-
if (!(block instanceof NRFunction2Wrapper)) {
43-
NRFunction2Wrapper<? super CoroutineScope, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper(block);
44-
block = wrapper;
45-
}
4638
T t = Weaver.callOriginal();
4739
return t;
4840
}
@@ -69,10 +61,6 @@ public static final <T> Deferred<T> async(CoroutineScope scope, CoroutineContext
6961
context = context.plus(nrContextToken);
7062
}
7163
}
72-
if(!(block instanceof NRFunction2Wrapper)) {
73-
NRFunction2Wrapper<? super CoroutineScope, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper(block);
74-
block = wrapper;
75-
}
7664
} else {
7765
NewRelic.getAgent().getTransaction().ignore();
7866
}
@@ -83,18 +71,6 @@ public static final <T> Deferred<T> async(CoroutineScope scope, CoroutineContext
8371
public static final <T> Object invoke(CoroutineDispatcher dispatcher, Function2<? super CoroutineScope, ? super Continuation<? super T>, ? extends Object> block, Continuation<? super T> c) {
8472

8573
NewRelic.getAgent().getTracedMethod().addCustomAttribute("Continuation", c.toString());
86-
if(!(block instanceof NRFunction2Wrapper)) {
87-
NRFunction2Wrapper<? super CoroutineScope, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper(block);
88-
block = wrapper;
89-
}
90-
if(c != null && !Utils.ignoreContinuation(c.toString())) {
91-
boolean isSuspend = c instanceof SuspendFunction;
92-
if(!isSuspend) {
93-
String cont_string = Utils.getContinuationString(c);
94-
NRContinuationWrapper wrapper = new NRContinuationWrapper<>(c, cont_string);
95-
c = wrapper;
96-
}
97-
}
9874
Object t = Weaver.callOriginal();
9975
return t;
10076
}
@@ -126,11 +102,6 @@ public static final kotlinx.coroutines.Job launch(CoroutineScope scope, Coroutin
126102
context = context.plus(nrContextToken);
127103
}
128104
}
129-
if (!(block instanceof NRFunction2Wrapper)) {
130-
NRFunction2Wrapper<? super CoroutineScope, ? super Continuation<? super Unit>, ? extends Object> wrapper = new NRFunction2Wrapper(
131-
block);
132-
block = wrapper;
133-
}
134105
} else {
135106
NewRelic.getAgent().getTransaction().ignore();
136107
}
@@ -159,17 +130,6 @@ public static final <T> Object withContext(CoroutineContext context,Function2<?
159130
context = context.plus(nrContextToken);
160131
}
161132
}
162-
if(!(block instanceof NRFunction2Wrapper)) {
163-
NRFunction2Wrapper<? super CoroutineScope, ? super Continuation<? super T>, ? extends Object> wrapper = new NRFunction2Wrapper(block);
164-
block = wrapper;
165-
}
166-
if(completion != null && !Utils.ignoreContinuation(completion.toString())) {
167-
if(!(completion instanceof NRContinuationWrapper)) {
168-
String cont_string = Utils.getContinuationString(completion);
169-
NRContinuationWrapper wrapper = new NRContinuationWrapper<>(completion, cont_string);
170-
completion = wrapper;
171-
}
172-
}
173133
return Weaver.callOriginal();
174134
}
175135
}

0 commit comments

Comments
 (0)