21
21
22
22
import httpx
23
23
import respx
24
+ from wrapt import ObjectProxy
24
25
25
26
import opentelemetry .instrumentation .httpx
26
27
from opentelemetry import trace
@@ -168,6 +169,10 @@ def setUp(self):
168
169
169
170
HTTPXClientInstrumentor ().instrument ()
170
171
172
+ def print_spans (self , spans ):
173
+ for span in spans :
174
+ print (span .name , span .attributes )
175
+
171
176
# pylint: disable=invalid-name
172
177
def tearDown (self ):
173
178
super ().tearDown ()
@@ -181,7 +186,9 @@ def assert_span(
181
186
if exporter is None :
182
187
exporter = self .memory_exporter
183
188
span_list = exporter .get_finished_spans ()
184
- self .assertEqual (num_spans , len (span_list ))
189
+ self .assertEqual (
190
+ num_spans , len (span_list ), self .print_spans (span_list )
191
+ )
185
192
if num_spans == 0 :
186
193
return None
187
194
if num_spans == 1 :
@@ -760,14 +767,25 @@ def create_proxy_mounts(self):
760
767
),
761
768
}
762
769
763
- def assert_proxy_mounts (self , mounts , num_mounts , transport_type ):
770
+ def assert_proxy_mounts (self , mounts , num_mounts , transport_type = None ):
764
771
self .assertEqual (len (mounts ), num_mounts )
765
772
for transport in mounts :
766
773
with self .subTest (transport ):
767
- self .assertIsInstance (
768
- transport ,
769
- transport_type ,
770
- )
774
+ if transport_type :
775
+ self .assertIsInstance (
776
+ transport ,
777
+ transport_type ,
778
+ )
779
+ else :
780
+ handler = getattr (transport , "handle_request" , None )
781
+ if not handler :
782
+ handler = getattr (
783
+ transport , "handle_async_request"
784
+ )
785
+ self .assertTrue (
786
+ isinstance (handler , ObjectProxy )
787
+ and getattr (handler , "__wrapped__" )
788
+ )
771
789
772
790
def test_custom_tracer_provider (self ):
773
791
resource = resources .Resource .create ({})
@@ -989,9 +1007,25 @@ def test_instrument_proxy(self):
989
1007
self .assert_proxy_mounts (
990
1008
client ._mounts .values (),
991
1009
2 ,
992
- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
993
1010
)
994
1011
1012
+ def print_handler (self , client ):
1013
+ transport = client ._transport
1014
+ handler = getattr (
1015
+ transport ,
1016
+ "handle_request" ,
1017
+ getattr (transport , "handle_async_request" , None ),
1018
+ )
1019
+ print (
1020
+ handler ,
1021
+ (
1022
+ getattr (handler , "__wrapped__" , "no wrapped" )
1023
+ if handler
1024
+ else "no handler"
1025
+ ),
1026
+ )
1027
+ return handler
1028
+
995
1029
def test_instrument_client_with_proxy (self ):
996
1030
HTTPXClientInstrumentor ().uninstrument ()
997
1031
proxy_mounts = self .create_proxy_mounts ()
@@ -1008,7 +1042,6 @@ def test_instrument_client_with_proxy(self):
1008
1042
self .assert_proxy_mounts (
1009
1043
client ._mounts .values (),
1010
1044
2 ,
1011
- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
1012
1045
)
1013
1046
HTTPXClientInstrumentor ().uninstrument_client (client )
1014
1047
@@ -1018,13 +1051,13 @@ def test_uninstrument_client_with_proxy(self):
1018
1051
self .assert_proxy_mounts (
1019
1052
client ._mounts .values (),
1020
1053
2 ,
1021
- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
1022
1054
)
1023
1055
1024
1056
HTTPXClientInstrumentor ().uninstrument_client (client )
1025
1057
result = self .perform_request (self .URL , client = client )
1026
1058
1027
1059
self .assertEqual (result .text , "Hello!" )
1060
+ # FIXME: this does fail if uninstrument() has been called before and is a change of behaviour from before
1028
1061
self .assert_span (num_spans = 0 )
1029
1062
self .assert_proxy_mounts (
1030
1063
client ._mounts .values (),
0 commit comments