11package kotlinx .coroutines ;
22
33import com .newrelic .api .agent .NewRelic ;
4+ import com .newrelic .api .agent .Token ;
45import com .newrelic .api .agent .Trace ;
56import com .newrelic .api .agent .weaver .Weave ;
67import com .newrelic .api .agent .weaver .Weaver ;
@@ -21,9 +22,14 @@ public class BuildersKt {
2122
2223 @ Trace (dispatcher = true )
2324 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+ }
2733 }
2834 String name = Utils .getCoroutineName (context );
2935 if (name != null ) {
@@ -43,23 +49,32 @@ public static final <T> T runBlocking(CoroutineContext context, Function2<? supe
4349
4450 @ Trace (dispatcher = true )
4551 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+ }
5562
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 ();
6378 }
6479 return Weaver .callOriginal ();
6580 }
@@ -86,24 +101,38 @@ public static final <T> Object invoke(CoroutineDispatcher dispatcher, Function2<
86101
87102 @ Trace (dispatcher = true )
88103 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+ }
96134 } 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 ();
107136 }
108137 Job j = Weaver .callOriginal ();
109138 return j ;
@@ -121,9 +150,14 @@ public static final <T> Object withContext(CoroutineContext context,Function2<?
121150 NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Completion" , completion .toString ());
122151 }
123152
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+ }
127161 }
128162 if (!(block instanceof NRFunction2Wrapper )) {
129163 NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
0 commit comments