Skip to content

Commit a15a6ef

Browse files
committed
fixed problem with non-ending segments and added support for Yield
1 parent c61572c commit a15a6ef

File tree

6 files changed

+99
-108
lines changed

6 files changed

+99
-108
lines changed
Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package kotlinx.coroutines;
22

3-
import com.newrelic.agent.tracers.Tracer;
43
import com.newrelic.api.agent.NewRelic;
5-
import com.newrelic.api.agent.Segment;
64
import com.newrelic.api.agent.Token;
75
import com.newrelic.api.agent.Trace;
8-
import com.newrelic.api.agent.TracedMethod;
9-
import com.newrelic.api.agent.Transaction;
106
import com.newrelic.api.agent.weaver.MatchType;
117
import com.newrelic.api.agent.weaver.NewField;
128
import com.newrelic.api.agent.weaver.Weave;
@@ -15,36 +11,30 @@
1511

1612
@Weave
1713
public abstract class EventLoopImplBase {
18-
14+
1915
@Weave(type = MatchType.BaseClass)
2016
public static abstract class DelayedTask {
21-
17+
2218
public DelayedTask(long nanos) {
2319
if(Utils.DELAYED_ENABLED) {
24-
segment = NewRelic.getAgent().getTransaction().startSegment("Custom/Kotlin/Coroutines/" + getClass().getSimpleName());
2520
token = NewRelic.getAgent().getTransaction().getToken();
2621
}
2722
}
28-
29-
@NewField
30-
protected Segment segment = null;
31-
23+
24+
public long nanoTime = Weaver.callOriginal();
25+
3226
@NewField
3327
protected Token token = null;
3428

3529
public void dispose() {
36-
if(segment != null) {
37-
segment.ignore();
38-
segment = null;
39-
}
4030
if(token != null) {
4131
token.expire();
4232
token = null;
4333
}
4434
Weaver.callOriginal();
4535
}
4636
}
47-
37+
4838
@Weave
4939
static class DelayedResumeTask extends DelayedTask {
5040

@@ -55,25 +45,12 @@ public DelayedResumeTask(long nanos, kotlinx.coroutines.CancellableContinuation<
5545

5646
@Trace(async = true)
5747
public void run() {
58-
if(segment != null) {
59-
Transaction txn = segment.getTransaction();
60-
if(txn != null) {
61-
TracedMethod traced = txn.getTracedMethod();
62-
if(traced != null) {
63-
if(traced instanceof Tracer) {
64-
Tracer tracer = (Tracer)traced;
65-
if(tracer.getParentTracer() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullParent");
66-
if(tracer.getTransactionActivity() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullTransactionActivity");
67-
}
68-
}
69-
}
70-
segment.end();
71-
segment = null;
72-
}
7348
if(token != null) {
7449
token.linkAndExpire();
7550
token = null;
7651
}
52+
53+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
7754
Weaver.callOriginal();
7855
}
7956
}
@@ -87,20 +64,17 @@ public DelayedRunnableTask(long nanos, Runnable r) {
8764

8865
@Trace(async = true)
8966
public void run() {
90-
if(segment != null) {
91-
segment.end();
92-
segment = null;
93-
}
9467
if(token != null) {
9568
token.linkAndExpire();
9669
token = null;
9770
}
71+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
9872
Weaver.callOriginal();
9973
}
10074
}
10175

10276
@Weave
10377
public static class DelayedTaskQueue {
104-
78+
10579
}
10680
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kotlinx.coroutines;
2+
3+
import com.newrelic.api.agent.NewRelic;
4+
import com.newrelic.api.agent.Trace;
5+
import com.newrelic.api.agent.weaver.Weave;
6+
import com.newrelic.api.agent.weaver.Weaver;
7+
import com.newrelic.instrumentation.kotlin.coroutines_14.Utils;
8+
9+
import kotlin.Unit;
10+
import kotlin.coroutines.Continuation;
11+
12+
@Weave
13+
public class YieldKt {
14+
15+
@Trace
16+
public static Object yield(Continuation<? super Unit> cont) {
17+
String cont_string = Utils.getContinuationString(cont);
18+
if(cont_string != null) {
19+
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Kotlin","Yield","yield",cont_string);
20+
}
21+
return Weaver.callOriginal();
22+
}
23+
}
Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package kotlinx.coroutines;
22

3-
import com.newrelic.agent.tracers.Tracer;
43
import com.newrelic.api.agent.NewRelic;
5-
import com.newrelic.api.agent.Segment;
64
import com.newrelic.api.agent.Token;
75
import com.newrelic.api.agent.Trace;
8-
import com.newrelic.api.agent.TracedMethod;
9-
import com.newrelic.api.agent.Transaction;
106
import com.newrelic.api.agent.weaver.MatchType;
117
import com.newrelic.api.agent.weaver.NewField;
128
import com.newrelic.api.agent.weaver.Weave;
@@ -15,36 +11,30 @@
1511

1612
@Weave
1713
public abstract class EventLoopImplBase {
18-
14+
1915
@Weave(type = MatchType.BaseClass)
2016
public static abstract class DelayedTask {
21-
17+
2218
public DelayedTask(long nanos) {
2319
if(Utils.DELAYED_ENABLED) {
24-
segment = NewRelic.getAgent().getTransaction().startSegment("Custom/Kotlin/Coroutines/" + getClass().getSimpleName());
2520
token = NewRelic.getAgent().getTransaction().getToken();
2621
}
2722
}
28-
29-
@NewField
30-
protected Segment segment = null;
31-
23+
24+
public long nanoTime = Weaver.callOriginal();
25+
3226
@NewField
3327
protected Token token = null;
3428

3529
public void dispose() {
36-
if(segment != null) {
37-
segment.ignore();
38-
segment = null;
39-
}
4030
if(token != null) {
4131
token.expire();
4232
token = null;
4333
}
4434
Weaver.callOriginal();
4535
}
4636
}
47-
37+
4838
@Weave
4939
static class DelayedResumeTask extends DelayedTask {
5040

@@ -55,25 +45,12 @@ public DelayedResumeTask(long nanos, kotlinx.coroutines.CancellableContinuation<
5545

5646
@Trace(async = true)
5747
public void run() {
58-
if(segment != null) {
59-
Transaction txn = segment.getTransaction();
60-
if(txn != null) {
61-
TracedMethod traced = txn.getTracedMethod();
62-
if(traced != null) {
63-
if(traced instanceof Tracer) {
64-
Tracer tracer = (Tracer)traced;
65-
if(tracer.getParentTracer() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullParent");
66-
if(tracer.getTransactionActivity() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullTransactionActivity");
67-
}
68-
}
69-
}
70-
segment.end();
71-
segment = null;
72-
}
7348
if(token != null) {
7449
token.linkAndExpire();
7550
token = null;
7651
}
52+
53+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
7754
Weaver.callOriginal();
7855
}
7956
}
@@ -87,20 +64,17 @@ public DelayedRunnableTask(long nanos, Runnable r) {
8764

8865
@Trace(async = true)
8966
public void run() {
90-
if(segment != null) {
91-
segment.end();
92-
segment = null;
93-
}
9467
if(token != null) {
9568
token.linkAndExpire();
9669
token = null;
9770
}
71+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
9872
Weaver.callOriginal();
9973
}
10074
}
10175

10276
@Weave
10377
public static class DelayedTaskQueue {
104-
78+
10579
}
10680
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kotlinx.coroutines;
2+
3+
import com.newrelic.api.agent.NewRelic;
4+
import com.newrelic.api.agent.Trace;
5+
import com.newrelic.api.agent.weaver.Weave;
6+
import com.newrelic.api.agent.weaver.Weaver;
7+
import com.newrelic.instrumentation.kotlin.coroutines_15.Utils;
8+
9+
import kotlin.Unit;
10+
import kotlin.coroutines.Continuation;
11+
12+
@Weave
13+
public class YieldKt {
14+
15+
@Trace
16+
public static Object yield(Continuation<? super Unit> cont) {
17+
String cont_string = Utils.getContinuationString(cont);
18+
if(cont_string != null) {
19+
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Kotlin","Yield","yield",cont_string);
20+
}
21+
return Weaver.callOriginal();
22+
}
23+
}
Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package kotlinx.coroutines;
22

3-
import com.newrelic.agent.tracers.Tracer;
43
import com.newrelic.api.agent.NewRelic;
5-
import com.newrelic.api.agent.Segment;
64
import com.newrelic.api.agent.Token;
75
import com.newrelic.api.agent.Trace;
8-
import com.newrelic.api.agent.TracedMethod;
9-
import com.newrelic.api.agent.Transaction;
106
import com.newrelic.api.agent.weaver.MatchType;
117
import com.newrelic.api.agent.weaver.NewField;
128
import com.newrelic.api.agent.weaver.Weave;
@@ -15,36 +11,30 @@
1511

1612
@Weave
1713
public abstract class EventLoopImplBase {
18-
14+
1915
@Weave(type = MatchType.BaseClass)
2016
public static abstract class DelayedTask {
21-
17+
2218
public DelayedTask(long nanos) {
2319
if(Utils.DELAYED_ENABLED) {
24-
segment = NewRelic.getAgent().getTransaction().startSegment("Custom/Kotlin/Coroutines/" + getClass().getSimpleName());
2520
token = NewRelic.getAgent().getTransaction().getToken();
2621
}
2722
}
28-
29-
@NewField
30-
protected Segment segment = null;
31-
23+
24+
public long nanoTime = Weaver.callOriginal();
25+
3226
@NewField
3327
protected Token token = null;
3428

3529
public void dispose() {
36-
if(segment != null) {
37-
segment.ignore();
38-
segment = null;
39-
}
4030
if(token != null) {
4131
token.expire();
4232
token = null;
4333
}
4434
Weaver.callOriginal();
4535
}
4636
}
47-
37+
4838
@Weave
4939
static class DelayedResumeTask extends DelayedTask {
5040

@@ -55,25 +45,12 @@ public DelayedResumeTask(long nanos, kotlinx.coroutines.CancellableContinuation<
5545

5646
@Trace(async = true)
5747
public void run() {
58-
if(segment != null) {
59-
Transaction txn = segment.getTransaction();
60-
if(txn != null) {
61-
TracedMethod traced = txn.getTracedMethod();
62-
if(traced != null) {
63-
if(traced instanceof Tracer) {
64-
Tracer tracer = (Tracer)traced;
65-
if(tracer.getParentTracer() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullParent");
66-
if(tracer.getTransactionActivity() == null) NewRelic.incrementCounter("NRLabs/Kotlin/DelayedResumeTask/NullTransactionActivity");
67-
}
68-
}
69-
}
70-
segment.end();
71-
segment = null;
72-
}
7348
if(token != null) {
7449
token.linkAndExpire();
7550
token = null;
7651
}
52+
53+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
7754
Weaver.callOriginal();
7855
}
7956
}
@@ -87,20 +64,17 @@ public DelayedRunnableTask(long nanos, Runnable r) {
8764

8865
@Trace(async = true)
8966
public void run() {
90-
if(segment != null) {
91-
segment.end();
92-
segment = null;
93-
}
9467
if(token != null) {
9568
token.linkAndExpire();
9669
token = null;
9770
}
71+
NewRelic.recordResponseTimeMetric("Custom/Kotlin/DelayedResumeTask", nanoTime);
9872
Weaver.callOriginal();
9973
}
10074
}
10175

10276
@Weave
10377
public static class DelayedTaskQueue {
104-
78+
10579
}
10680
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kotlinx.coroutines;
2+
3+
import com.newrelic.api.agent.NewRelic;
4+
import com.newrelic.api.agent.Trace;
5+
import com.newrelic.api.agent.weaver.Weave;
6+
import com.newrelic.api.agent.weaver.Weaver;
7+
import com.newrelic.instrumentation.kotlin.coroutines_17.Utils;
8+
9+
import kotlin.Unit;
10+
import kotlin.coroutines.Continuation;
11+
12+
@Weave
13+
public class YieldKt {
14+
15+
@Trace
16+
public static Object yield(Continuation<? super Unit> cont) {
17+
String cont_string = Utils.getContinuationString(cont);
18+
if(cont_string != null) {
19+
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","Kotlin","Yield","yield",cont_string);
20+
}
21+
return Weaver.callOriginal();
22+
}
23+
}

0 commit comments

Comments
 (0)