1
1
package kotlinx .coroutines ;
2
2
3
3
import com .newrelic .api .agent .NewRelic ;
4
+ import com .newrelic .api .agent .Token ;
4
5
import com .newrelic .api .agent .Trace ;
5
6
import com .newrelic .api .agent .weaver .Weave ;
6
7
import com .newrelic .api .agent .weaver .Weaver ;
@@ -21,9 +22,14 @@ public class BuildersKt {
21
22
22
23
@ Trace (dispatcher = true )
23
24
public static final <T > T runBlocking (CoroutineContext context , Function2 <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > block ) {
24
- NRCoroutineToken nrContextToken = Utils .setToken (context );
25
- if (nrContextToken != null ) {
26
- context = context .plus (nrContextToken );
25
+ Token token = Utils .getToken (context );
26
+ if (token != null ) {
27
+ token .link ();
28
+ } else {
29
+ NRCoroutineToken nrContextToken = Utils .setToken (context );
30
+ if (nrContextToken != null ) {
31
+ context = context .plus (nrContextToken );
32
+ }
27
33
}
28
34
String name = Utils .getCoroutineName (context );
29
35
if (name != null ) {
@@ -43,23 +49,32 @@ public static final <T> T runBlocking(CoroutineContext context, Function2<? supe
43
49
44
50
@ Trace (dispatcher = true )
45
51
public static final <T > Deferred <T > async (CoroutineScope scope , CoroutineContext context , CoroutineStart cStart , Function2 <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > block ) {
46
- String name = Utils .getCoroutineName (context );
47
- if (name == null ) {
48
- name = Utils .getCoroutineName (scope .getCoroutineContext ());
49
- }
50
- if (name != null ) {
51
- NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"async" ,name );
52
- } else {
53
- NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"async" );
54
- }
52
+ if (!Utils .ignoreScope (scope )) {
53
+ String name = Utils .getCoroutineName (context );
54
+ if (name == null ) {
55
+ name = Utils .getCoroutineName (scope .getCoroutineContext ());
56
+ }
57
+ if (name != null ) {
58
+ NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"async" ,name );
59
+ } else {
60
+ NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"async" );
61
+ }
55
62
56
- NRCoroutineToken nrContextToken = Utils .setToken (context );
57
- if (nrContextToken != null ) {
58
- context = context .plus (nrContextToken );
59
- }
60
- if (!(block instanceof NRFunction2Wrapper )) {
61
- NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
62
- block = wrapper ;
63
+ Token token = Utils .getToken (context );
64
+ if (token != null ) {
65
+ token .link ();
66
+ } else {
67
+ NRCoroutineToken nrContextToken = Utils .setToken (context );
68
+ if (nrContextToken != null ) {
69
+ context = context .plus (nrContextToken );
70
+ }
71
+ }
72
+ if (!(block instanceof NRFunction2Wrapper )) {
73
+ NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
74
+ block = wrapper ;
75
+ }
76
+ } else {
77
+ NewRelic .getAgent ().getTransaction ().ignore ();
63
78
}
64
79
return Weaver .callOriginal ();
65
80
}
@@ -86,24 +101,38 @@ public static final <T> Object invoke(CoroutineDispatcher dispatcher, Function2<
86
101
87
102
@ Trace (dispatcher = true )
88
103
public static final kotlinx .coroutines .Job launch (CoroutineScope scope , CoroutineContext context , CoroutineStart cStart , Function2 <? super CoroutineScope , ? super Continuation <? super Unit >, ? extends Object > block ) {
89
- NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("CoroutineStart" , cStart .name ());
90
- String name = Utils .getCoroutineName (context );
91
- if (name == null ) {
92
- name = Utils .getCoroutineName (scope .getCoroutineContext ());
93
- }
94
- if (name != null ) {
95
- NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"launch" ,name );
104
+ if (!Utils .ignoreScope (scope )) {
105
+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("CoroutineStart" , cStart .name ());
106
+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("CoroutineScope-Class" , scope .getClass ().getName ());
107
+
108
+ String name = Utils .getCoroutineName (context );
109
+ if (name == null ) {
110
+ name = Utils .getCoroutineName (scope .getCoroutineContext ());
111
+ }
112
+ if (name != null ) {
113
+ NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" , "Builders" , "launch" , name );
114
+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("CoroutineName" , name );
115
+ } else {
116
+ NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" , "Builders" , "launch" );
117
+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("CoroutineName" , "Could not determine" );
118
+ }
119
+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Block" , block .toString ());
120
+ Token token = Utils .getToken (context );
121
+ if (token != null ) {
122
+ token .link ();
123
+ } else {
124
+ NRCoroutineToken nrContextToken = Utils .setToken (context );
125
+ if (nrContextToken != null ) {
126
+ context = context .plus (nrContextToken );
127
+ }
128
+ }
129
+ if (!(block instanceof NRFunction2Wrapper )) {
130
+ NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super Unit >, ? extends Object > wrapper = new NRFunction2Wrapper (
131
+ block );
132
+ block = wrapper ;
133
+ }
96
134
} else {
97
- NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" ,"Builders" ,"launch" );
98
- }
99
- NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Block" , block .toString ());
100
- NRCoroutineToken nrContextToken = Utils .setToken (context );
101
- if (nrContextToken != null ) {
102
- context = context .plus (nrContextToken );
103
- }
104
- if (!(block instanceof NRFunction2Wrapper )) {
105
- NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super Unit >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
106
- block = wrapper ;
135
+ NewRelic .getAgent ().getTransaction ().ignore ();
107
136
}
108
137
Job j = Weaver .callOriginal ();
109
138
return j ;
@@ -121,9 +150,14 @@ public static final <T> Object withContext(CoroutineContext context,Function2<?
121
150
NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Completion" , completion .toString ());
122
151
}
123
152
124
- NRCoroutineToken nrContextToken = Utils .setToken (context );
125
- if (nrContextToken != null ) {
126
- context = context .plus (nrContextToken );
153
+ Token token = Utils .getToken (context );
154
+ if (token != null ) {
155
+ token .link ();
156
+ } else {
157
+ NRCoroutineToken nrContextToken = Utils .setToken (context );
158
+ if (nrContextToken != null ) {
159
+ context = context .plus (nrContextToken );
160
+ }
127
161
}
128
162
if (!(block instanceof NRFunction2Wrapper )) {
129
163
NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
0 commit comments