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,36 @@ 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+ } else {
115+ NewRelic .getAgent ().getTracedMethod ().setMetricName ("Custom" , "Builders" , "launch" );
116+ }
117+ NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Block" , block .toString ());
118+ Token token = Utils .getToken (context );
119+ if (token != null ) {
120+ token .link ();
121+ } else {
122+ NRCoroutineToken nrContextToken = Utils .setToken (context );
123+ if (nrContextToken != null ) {
124+ context = context .plus (nrContextToken );
125+ }
126+ }
127+ if (!(block instanceof NRFunction2Wrapper )) {
128+ NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super Unit >, ? extends Object > wrapper = new NRFunction2Wrapper (
129+ block );
130+ block = wrapper ;
131+ }
96132 } 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 ;
133+ NewRelic .getAgent ().getTransaction ().ignore ();
107134 }
108135 Job j = Weaver .callOriginal ();
109136 return j ;
@@ -121,9 +148,14 @@ public static final <T> Object withContext(CoroutineContext context,Function2<?
121148 NewRelic .getAgent ().getTracedMethod ().addCustomAttribute ("Completion" , completion .toString ());
122149 }
123150
124- NRCoroutineToken nrContextToken = Utils .setToken (context );
125- if (nrContextToken != null ) {
126- context = context .plus (nrContextToken );
151+ Token token = Utils .getToken (context );
152+ if (token != null ) {
153+ token .link ();
154+ } else {
155+ NRCoroutineToken nrContextToken = Utils .setToken (context );
156+ if (nrContextToken != null ) {
157+ context = context .plus (nrContextToken );
158+ }
127159 }
128160 if (!(block instanceof NRFunction2Wrapper )) {
129161 NRFunction2Wrapper <? super CoroutineScope , ? super Continuation <? super T >, ? extends Object > wrapper = new NRFunction2Wrapper (block );
0 commit comments