@@ -780,9 +780,13 @@ def test_custom_tracer_provider(self):
780
780
HTTPXClientInstrumentor ().uninstrument ()
781
781
782
782
def test_response_hook (self ):
783
+ response_hook_key = "async_response_hook" if asyncio .iscoroutinefunction (self .response_hook ) else "response_hook"
784
+ response_hook_kwargs = {
785
+ response_hook_key : self .response_hook
786
+ }
783
787
HTTPXClientInstrumentor ().instrument (
784
788
tracer_provider = self .tracer_provider ,
785
- response_hook = self . response_hook ,
789
+ ** response_hook_kwargs
786
790
)
787
791
client = self .create_client ()
788
792
result = self .perform_request (self .URL , client = client )
@@ -823,9 +827,13 @@ def test_response_hook_sync_async_kwargs(self):
823
827
HTTPXClientInstrumentor ().uninstrument ()
824
828
825
829
def test_request_hook (self ):
830
+ request_hook_key = "async_request_hook" if asyncio .iscoroutinefunction (self .request_hook ) else "request_hook"
831
+ request_hook_kwargs = {
832
+ request_hook_key : self .request_hook
833
+ }
826
834
HTTPXClientInstrumentor ().instrument (
827
835
tracer_provider = self .tracer_provider ,
828
- request_hook = self . request_hook ,
836
+ ** request_hook_kwargs ,
829
837
)
830
838
client = self .create_client ()
831
839
result = self .perform_request (self .URL , client = client )
@@ -1214,3 +1222,36 @@ def test_basic_multiple(self):
1214
1222
self .perform_request (self .URL , client = self .client )
1215
1223
self .perform_request (self .URL , client = self .client2 )
1216
1224
self .assert_span (num_spans = 2 )
1225
+
1226
+ def test_async_response_hook_does_nothing_if_not_coroutine (self ):
1227
+ HTTPXClientInstrumentor ().instrument (
1228
+ tracer_provider = self .tracer_provider ,
1229
+ async_response_hook = _response_hook ,
1230
+ )
1231
+ client = self .create_client ()
1232
+ result = self .perform_request (self .URL , client = client )
1233
+
1234
+ self .assertEqual (result .text , "Hello!" )
1235
+ span = self .assert_span ()
1236
+ self .assertEqual (
1237
+ dict (span .attributes ),
1238
+ {
1239
+ SpanAttributes .HTTP_METHOD : "GET" ,
1240
+ SpanAttributes .HTTP_URL : self .URL ,
1241
+ SpanAttributes .HTTP_STATUS_CODE : 200 ,
1242
+ },
1243
+ )
1244
+ HTTPXClientInstrumentor ().uninstrument ()
1245
+
1246
+ def test_async_request_hook_does_nothing_if_not_coroutine (self ):
1247
+ HTTPXClientInstrumentor ().instrument (
1248
+ tracer_provider = self .tracer_provider ,
1249
+ async_request_hook = _request_hook ,
1250
+ )
1251
+ client = self .create_client ()
1252
+ result = self .perform_request (self .URL , client = client )
1253
+
1254
+ self .assertEqual (result .text , "Hello!" )
1255
+ span = self .assert_span ()
1256
+ self .assertEqual (span .name , "GET" )
1257
+ HTTPXClientInstrumentor ().uninstrument ()
0 commit comments