2121
2222import httpx
2323import respx
24+ from wrapt import ObjectProxy
2425
2526import opentelemetry .instrumentation .httpx
2627from opentelemetry import trace
@@ -168,6 +169,10 @@ def setUp(self):
168169
169170 HTTPXClientInstrumentor ().instrument ()
170171
172+ def print_spans (self , spans ):
173+ for span in spans :
174+ print (span .name , span .attributes )
175+
171176 # pylint: disable=invalid-name
172177 def tearDown (self ):
173178 super ().tearDown ()
@@ -181,7 +186,9 @@ def assert_span(
181186 if exporter is None :
182187 exporter = self .memory_exporter
183188 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+ )
185192 if num_spans == 0 :
186193 return None
187194 if num_spans == 1 :
@@ -760,14 +767,25 @@ def create_proxy_mounts(self):
760767 ),
761768 }
762769
763- def assert_proxy_mounts (self , mounts , num_mounts , transport_type ):
770+ def assert_proxy_mounts (self , mounts , num_mounts , transport_type = None ):
764771 self .assertEqual (len (mounts ), num_mounts )
765772 for transport in mounts :
766773 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+ )
771789
772790 def test_custom_tracer_provider (self ):
773791 resource = resources .Resource .create ({})
@@ -990,9 +1008,25 @@ def test_instrument_proxy(self):
9901008 self .assert_proxy_mounts (
9911009 client ._mounts .values (),
9921010 2 ,
993- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
9941011 )
9951012
1013+ def print_handler (self , client ):
1014+ transport = client ._transport
1015+ handler = getattr (
1016+ transport ,
1017+ "handle_request" ,
1018+ getattr (transport , "handle_async_request" , None ),
1019+ )
1020+ print (
1021+ handler ,
1022+ (
1023+ getattr (handler , "__wrapped__" , "no wrapped" )
1024+ if handler
1025+ else "no handler"
1026+ ),
1027+ )
1028+ return handler
1029+
9961030 def test_instrument_client_with_proxy (self ):
9971031 HTTPXClientInstrumentor ().uninstrument ()
9981032 proxy_mounts = self .create_proxy_mounts ()
@@ -1009,7 +1043,6 @@ def test_instrument_client_with_proxy(self):
10091043 self .assert_proxy_mounts (
10101044 client ._mounts .values (),
10111045 2 ,
1012- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
10131046 )
10141047 HTTPXClientInstrumentor ().uninstrument_client (client )
10151048
@@ -1019,13 +1052,13 @@ def test_uninstrument_client_with_proxy(self):
10191052 self .assert_proxy_mounts (
10201053 client ._mounts .values (),
10211054 2 ,
1022- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
10231055 )
10241056
10251057 HTTPXClientInstrumentor ().uninstrument_client (client )
10261058 result = self .perform_request (self .URL , client = client )
10271059
10281060 self .assertEqual (result .text , "Hello!" )
1061+ # FIXME: this does fail if uninstrument() has been called before and is a change of behaviour from before
10291062 self .assert_span (num_spans = 0 )
10301063 self .assert_proxy_mounts (
10311064 client ._mounts .values (),
0 commit comments