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 ({})
@@ -989,9 +1007,25 @@ def test_instrument_proxy(self):
9891007 self .assert_proxy_mounts (
9901008 client ._mounts .values (),
9911009 2 ,
992- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
9931010 )
9941011
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+
9951029 def test_instrument_client_with_proxy (self ):
9961030 HTTPXClientInstrumentor ().uninstrument ()
9971031 proxy_mounts = self .create_proxy_mounts ()
@@ -1008,7 +1042,6 @@ def test_instrument_client_with_proxy(self):
10081042 self .assert_proxy_mounts (
10091043 client ._mounts .values (),
10101044 2 ,
1011- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
10121045 )
10131046 HTTPXClientInstrumentor ().uninstrument_client (client )
10141047
@@ -1018,13 +1051,13 @@ def test_uninstrument_client_with_proxy(self):
10181051 self .assert_proxy_mounts (
10191052 client ._mounts .values (),
10201053 2 ,
1021- (SyncOpenTelemetryTransport , AsyncOpenTelemetryTransport ),
10221054 )
10231055
10241056 HTTPXClientInstrumentor ().uninstrument_client (client )
10251057 result = self .perform_request (self .URL , client = client )
10261058
10271059 self .assertEqual (result .text , "Hello!" )
1060+ # FIXME: this does fail if uninstrument() has been called before and is a change of behaviour from before
10281061 self .assert_span (num_spans = 0 )
10291062 self .assert_proxy_mounts (
10301063 client ._mounts .values (),
0 commit comments