@@ -67,84 +67,36 @@ public void transform(TypeTransformer transformer) {
6767 HttpClientInstrumentation .class .getName () + "$SendAsyncAdvice" );
6868 }
6969
70- public static class AdviceScope {
71- private final Context context ;
72- private final Context parentContext ;
73- private final Scope scope ;
74- private final CallDepth callDepth ;
75- private final HttpRequest request ;
76-
77- private AdviceScope (
78- Context parentContext ,
79- Context context ,
80- Scope scope ,
81- @ Nullable CallDepth callDepth ,
82- HttpRequest request ) {
83- this .parentContext = parentContext ;
84- this .context = context ;
85- this .scope = scope ;
86- this .callDepth = callDepth ;
87- this .request = request ;
88- }
70+ @ SuppressWarnings ("unused" )
71+ public static class SendAdvice {
8972
90- @ Nullable
91- public static AdviceScope start ( HttpRequest request ) {
92- return start ( request , null ) ;
93- }
73+ public static class AdviceScope {
74+ private final Context context ;
75+ private final Scope scope ;
76+ private final HttpRequest request ;
9477
95- @ Nullable
96- private static AdviceScope start (HttpRequest request , @ Nullable CallDepth callDepth ) {
97- Context parentContext = currentContext ();
98- if (!instrumenter ().shouldStart (parentContext , request )) {
99- return null ;
78+ private AdviceScope (Context context , Scope scope , HttpRequest request ) {
79+ this .context = context ;
80+ this .scope = scope ;
81+ this .request = request ;
10082 }
10183
102- Context context = instrumenter ().start (parentContext , request );
103- return new AdviceScope (parentContext , context , context .makeCurrent (), callDepth , request );
104- }
84+ @ Nullable
85+ public static AdviceScope start (HttpRequest request ) {
86+ Context parentContext = currentContext ();
87+ if (!instrumenter ().shouldStart (parentContext , request )) {
88+ return null ;
89+ }
10590
106- public static AdviceScope startAsync (HttpRequest httpRequest ) {
107- CallDepth callDepth = CallDepth .forClass (HttpClient .class );
108- if (callDepth .getAndIncrement () > 0 ) {
109- return new AdviceScope (null , null , null , callDepth , httpRequest );
91+ Context context = instrumenter ().start (parentContext , request );
92+ return new AdviceScope (context , context .makeCurrent (), request );
11093 }
111- return start (httpRequest , callDepth );
112- }
113-
114- public boolean end (@ Nullable HttpResponse <?> response , @ Nullable Throwable throwable ) {
115- // non-null call depth only used for async to prevent recursion
116- if (callDepth != null ) {
117- if (callDepth .decrementAndGet () > 0 ) {
118- // async end nested call
119- return false ;
120- }
12194
95+ public void end (@ Nullable HttpResponse <?> response , @ Nullable Throwable throwable ) {
12296 scope .close ();
123- if (throwable != null ) {
124- // async end with exception: ending span and no wrapping needed
125- instrumenter ().end (context , request , response , throwable );
126- return false ;
127- }
128-
129- // async end without exception:
130- return true ;
97+ instrumenter ().end (context , request , response , throwable );
13198 }
132-
133- // synchronous end
134- scope .close ();
135- instrumenter ().end (context , request , response , throwable );
136- return false ;
137- }
138-
139- public CompletableFuture <HttpResponse <?>> wrapFuture (
140- CompletableFuture <HttpResponse <?>> future ) {
141- future = future .whenComplete (new ResponseConsumer (instrumenter (), context , request ));
142- return CompletableFutureWrapper .wrap (future , parentContext );
14399 }
144- }
145-
146- @ SuppressWarnings ("unused" )
147- public static class SendAdvice {
148100
149101 @ Nullable
150102 @ Advice .OnMethodEnter (suppress = Throwable .class )
@@ -168,24 +120,78 @@ public static void methodExit(
168120 @ SuppressWarnings ("unused" )
169121 public static class SendAsyncAdvice {
170122
123+ public static class AsyncAdviceScope {
124+ private final Context context ;
125+ private final Context parentContext ;
126+ private final Scope scope ;
127+ private final HttpRequest request ;
128+ private final CallDepth callDepth ;
129+
130+ public AsyncAdviceScope (
131+ Context context ,
132+ Context parentContext ,
133+ Scope scope ,
134+ HttpRequest request ,
135+ CallDepth callDepth ) {
136+ this .context = context ;
137+ this .parentContext = parentContext ;
138+ this .scope = scope ;
139+ this .request = request ;
140+ this .callDepth = callDepth ;
141+ }
142+
143+ @ Nullable
144+ public static AsyncAdviceScope start (HttpRequest request ) {
145+ CallDepth callDepth = CallDepth .forClass (HttpClient .class );
146+ if (callDepth .getAndIncrement () > 0 ) {
147+ return new AsyncAdviceScope (null , null , null , null , callDepth );
148+ }
149+ Context parentContext = currentContext ();
150+ if (!instrumenter ().shouldStart (parentContext , request )) {
151+ return null ;
152+ }
153+ Context context = instrumenter ().start (parentContext , request );
154+ return new AsyncAdviceScope (
155+ parentContext , context , context .makeCurrent (), request , callDepth );
156+ }
157+
158+ public boolean end (@ Nullable Throwable throwable ) {
159+ if (callDepth .decrementAndGet () > 0 ) {
160+ // async end nested call
161+ return false ;
162+ }
163+ scope .close ();
164+ if (throwable != null ) {
165+ // async end with exception: ending span and no wrapping needed
166+ instrumenter ().end (context , request , null , throwable );
167+ return false ;
168+ }
169+ return true ;
170+ }
171+
172+ public CompletableFuture <HttpResponse <?>> wrapFuture (
173+ CompletableFuture <HttpResponse <?>> future ) {
174+ future = future .whenComplete (new ResponseConsumer (instrumenter (), context , request ));
175+ return CompletableFutureWrapper .wrap (future , parentContext );
176+ }
177+ }
178+
171179 @ Advice .OnMethodEnter (suppress = Throwable .class )
172- public static AdviceScope methodEnter (@ Advice .Argument (value = 0 ) HttpRequest httpRequest ) {
173- return AdviceScope .startAsync (httpRequest );
180+ public static AsyncAdviceScope methodEnter (
181+ @ Advice .Argument (value = 0 ) HttpRequest httpRequest ) {
182+ return AsyncAdviceScope .start (httpRequest );
174183 }
175184
176185 @ AssignReturned .ToReturned
177186 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
178187 public static CompletableFuture <HttpResponse <?>> methodExit (
179- @ Advice .Argument (value = 0 ) HttpRequest httpRequest ,
180188 @ Advice .Return CompletableFuture <HttpResponse <?>> future ,
181189 @ Advice .Thrown @ Nullable Throwable throwable ,
182- @ Advice .Enter @ Nullable AdviceScope scope ) {
183-
190+ @ Advice .Enter @ Nullable AsyncAdviceScope scope ) {
184191 if (scope == null ) {
185192 return future ;
186193 }
187-
188- if (scope .end (null , throwable )) {
194+ if (scope .end (throwable )) {
189195 return scope .wrapFuture (future );
190196 }
191197 return future ;
0 commit comments