@@ -167,8 +167,6 @@ def setUp(self):
167167 )
168168 )
169169
170- HTTPXClientInstrumentor ().instrument ()
171-
172170 def print_spans (self , spans ):
173171 for span in spans :
174172 print (span .name , span .attributes )
@@ -751,8 +749,9 @@ def create_proxy_transport(self, url: str):
751749
752750 def setUp (self ):
753751 super ().setUp ()
754- HTTPXClientInstrumentor ().instrument ()
755752 self .client = self .create_client ()
753+ # FIXME: calling instrument() instead fixes 13*2 tests :(
754+ HTTPXClientInstrumentor ().instrument_client (self .client )
756755
757756 def tearDown (self ):
758757 HTTPXClientInstrumentor ().uninstrument ()
@@ -792,7 +791,6 @@ def test_custom_tracer_provider(self):
792791 result = self .create_tracer_provider (resource = resource )
793792 tracer_provider , exporter = result
794793
795- HTTPXClientInstrumentor ().uninstrument ()
796794 HTTPXClientInstrumentor ().instrument (
797795 tracer_provider = tracer_provider
798796 )
@@ -802,7 +800,6 @@ def test_custom_tracer_provider(self):
802800 self .assertEqual (result .text , "Hello!" )
803801 span = self .assert_span (exporter = exporter )
804802 self .assertIs (span .resource , resource )
805- HTTPXClientInstrumentor ().uninstrument ()
806803
807804 def test_response_hook (self ):
808805 response_hook_key = (
@@ -811,7 +808,6 @@ def test_response_hook(self):
811808 else "response_hook"
812809 )
813810 response_hook_kwargs = {response_hook_key : self .response_hook }
814- HTTPXClientInstrumentor ().uninstrument ()
815811 HTTPXClientInstrumentor ().instrument (
816812 tracer_provider = self .tracer_provider ,
817813 ** response_hook_kwargs ,
@@ -830,10 +826,8 @@ def test_response_hook(self):
830826 HTTP_RESPONSE_BODY : "Hello!" ,
831827 },
832828 )
833- HTTPXClientInstrumentor ().uninstrument ()
834829
835830 def test_response_hook_sync_async_kwargs (self ):
836- HTTPXClientInstrumentor ().uninstrument ()
837831 HTTPXClientInstrumentor ().instrument (
838832 tracer_provider = self .tracer_provider ,
839833 response_hook = _response_hook ,
@@ -845,15 +839,14 @@ def test_response_hook_sync_async_kwargs(self):
845839 self .assertEqual (result .text , "Hello!" )
846840 span = self .assert_span ()
847841 self .assertEqual (
848- dict ( span .attributes ) ,
842+ span .attributes ,
849843 {
850844 SpanAttributes .HTTP_METHOD : "GET" ,
851845 SpanAttributes .HTTP_URL : self .URL ,
852846 SpanAttributes .HTTP_STATUS_CODE : 200 ,
853847 HTTP_RESPONSE_BODY : "Hello!" ,
854848 },
855849 )
856- HTTPXClientInstrumentor ().uninstrument ()
857850
858851 def test_request_hook (self ):
859852 request_hook_key = (
@@ -862,7 +855,6 @@ def test_request_hook(self):
862855 else "request_hook"
863856 )
864857 request_hook_kwargs = {request_hook_key : self .request_hook }
865- HTTPXClientInstrumentor ().uninstrument ()
866858 HTTPXClientInstrumentor ().instrument (
867859 tracer_provider = self .tracer_provider ,
868860 ** request_hook_kwargs ,
@@ -873,10 +865,8 @@ def test_request_hook(self):
873865 self .assertEqual (result .text , "Hello!" )
874866 span = self .assert_span ()
875867 self .assertEqual (span .name , "GET" + self .URL )
876- HTTPXClientInstrumentor ().uninstrument ()
877868
878869 def test_request_hook_sync_async_kwargs (self ):
879- HTTPXClientInstrumentor ().uninstrument ()
880870 HTTPXClientInstrumentor ().instrument (
881871 tracer_provider = self .tracer_provider ,
882872 request_hook = _request_hook ,
@@ -888,10 +878,8 @@ def test_request_hook_sync_async_kwargs(self):
888878 self .assertEqual (result .text , "Hello!" )
889879 span = self .assert_span ()
890880 self .assertEqual (span .name , "GET" + self .URL )
891- HTTPXClientInstrumentor ().uninstrument ()
892881
893882 def test_request_hook_no_span_update (self ):
894- HTTPXClientInstrumentor ().uninstrument ()
895883 HTTPXClientInstrumentor ().instrument (
896884 tracer_provider = self .tracer_provider ,
897885 request_hook = self .no_update_request_hook ,
@@ -902,10 +890,8 @@ def test_request_hook_no_span_update(self):
902890 self .assertEqual (result .text , "Hello!" )
903891 span = self .assert_span ()
904892 self .assertEqual (span .name , "GET" )
905- HTTPXClientInstrumentor ().uninstrument ()
906893
907894 def test_not_recording (self ):
908- HTTPXClientInstrumentor ().uninstrument ()
909895 with mock .patch ("opentelemetry.trace.INVALID_SPAN" ) as mock_span :
910896 HTTPXClientInstrumentor ().instrument (
911897 tracer_provider = trace .NoOpTracerProvider ()
@@ -921,21 +907,17 @@ def test_not_recording(self):
921907 self .assertTrue (mock_span .is_recording .called )
922908 self .assertFalse (mock_span .set_attribute .called )
923909 self .assertFalse (mock_span .set_status .called )
924- HTTPXClientInstrumentor ().uninstrument ()
925910
926911 def test_suppress_instrumentation_new_client (self ):
927- HTTPXClientInstrumentor ().uninstrument ()
928912 HTTPXClientInstrumentor ().instrument ()
929913 with suppress_http_instrumentation ():
930914 client = self .create_client ()
931915 result = self .perform_request (self .URL , client = client )
932916 self .assertEqual (result .text , "Hello!" )
933917
934918 self .assert_span (num_spans = 0 )
935- HTTPXClientInstrumentor ().uninstrument ()
936919
937920 def test_instrument_client (self ):
938- HTTPXClientInstrumentor ().uninstrument ()
939921 client = self .create_client ()
940922 HTTPXClientInstrumentor ().instrument_client (client )
941923 result = self .perform_request (self .URL , client = client )
@@ -962,6 +944,7 @@ def test_instrumentation_without_client(self):
962944 )
963945
964946 def test_uninstrument (self ):
947+ HTTPXClientInstrumentor ().instrument ()
965948 HTTPXClientInstrumentor ().uninstrument ()
966949 client = self .create_client ()
967950 result = self .perform_request (self .URL , client = client )
@@ -971,7 +954,6 @@ def test_uninstrument(self):
971954 self .assert_span (num_spans = 0 )
972955
973956 def test_uninstrument_client (self ):
974- HTTPXClientInstrumentor ().uninstrument ()
975957 HTTPXClientInstrumentor ().uninstrument_client (self .client )
976958
977959 result = self .perform_request (self .URL )
@@ -980,6 +962,7 @@ def test_uninstrument_client(self):
980962 self .assert_span (num_spans = 0 )
981963
982964 def test_uninstrument_new_client (self ):
965+ HTTPXClientInstrumentor ().instrument ()
983966 client1 = self .create_client ()
984967 HTTPXClientInstrumentor ().uninstrument_client (client1 )
985968
@@ -1002,6 +985,7 @@ def test_uninstrument_new_client(self):
1002985
1003986 def test_instrument_proxy (self ):
1004987 proxy_mounts = self .create_proxy_mounts ()
988+ HTTPXClientInstrumentor ().instrument ()
1005989 client = self .create_client (mounts = proxy_mounts )
1006990 self .perform_request (self .URL , client = client )
1007991 self .assert_span (num_spans = 1 )
@@ -1028,7 +1012,6 @@ def print_handler(self, client):
10281012 return handler
10291013
10301014 def test_instrument_client_with_proxy (self ):
1031- HTTPXClientInstrumentor ().uninstrument ()
10321015 proxy_mounts = self .create_proxy_mounts ()
10331016 client = self .create_client (mounts = proxy_mounts )
10341017 self .assert_proxy_mounts (
@@ -1048,6 +1031,7 @@ def test_instrument_client_with_proxy(self):
10481031
10491032 def test_uninstrument_client_with_proxy (self ):
10501033 proxy_mounts = self .create_proxy_mounts ()
1034+ HTTPXClientInstrumentor ().instrument ()
10511035 client = self .create_client (mounts = proxy_mounts )
10521036 self .assert_proxy_mounts (
10531037 client ._mounts .values (),
@@ -1110,7 +1094,7 @@ def create_client(
11101094 transport : typing .Optional [SyncOpenTelemetryTransport ] = None ,
11111095 ** kwargs ,
11121096 ):
1113- return httpx .Client (** kwargs )
1097+ return httpx .Client (transport = transport , ** kwargs )
11141098
11151099 def perform_request (
11161100 self ,
@@ -1231,6 +1215,7 @@ class TestAsyncInstrumentationIntegration(BaseTestCases.BaseInstrumentorTest):
12311215 def setUp (self ):
12321216 super ().setUp ()
12331217 self .client2 = self .create_client ()
1218+ HTTPXClientInstrumentor ().instrument_client (self .client2 )
12341219
12351220 def create_client (
12361221 self ,
@@ -1284,7 +1269,6 @@ def test_async_response_hook_does_nothing_if_not_coroutine(self):
12841269 SpanAttributes .HTTP_STATUS_CODE : 200 ,
12851270 },
12861271 )
1287- HTTPXClientInstrumentor ().uninstrument ()
12881272
12891273 def test_async_request_hook_does_nothing_if_not_coroutine (self ):
12901274 HTTPXClientInstrumentor ().instrument (
@@ -1297,4 +1281,3 @@ def test_async_request_hook_does_nothing_if_not_coroutine(self):
12971281 self .assertEqual (result .text , "Hello!" )
12981282 span = self .assert_span ()
12991283 self .assertEqual (span .name , "GET" )
1300- HTTPXClientInstrumentor ().uninstrument ()
0 commit comments