@@ -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,28 +907,26 @@ 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 )
942924 self .assertEqual (result .text , "Hello!" )
943925 self .assert_span (num_spans = 1 )
944926
945927 def test_instrumentation_without_client (self ):
928+
929+ HTTPXClientInstrumentor ().instrument ()
946930 results = [
947931 httpx .get (self .URL ),
948932 httpx .request ("GET" , self .URL ),
@@ -961,6 +945,7 @@ def test_instrumentation_without_client(self):
961945 )
962946
963947 def test_uninstrument (self ):
948+ HTTPXClientInstrumentor ().instrument ()
964949 HTTPXClientInstrumentor ().uninstrument ()
965950 client = self .create_client ()
966951 result = self .perform_request (self .URL , client = client )
@@ -970,7 +955,6 @@ def test_uninstrument(self):
970955 self .assert_span (num_spans = 0 )
971956
972957 def test_uninstrument_client (self ):
973- HTTPXClientInstrumentor ().uninstrument ()
974958 HTTPXClientInstrumentor ().uninstrument_client (self .client )
975959
976960 result = self .perform_request (self .URL )
@@ -979,6 +963,7 @@ def test_uninstrument_client(self):
979963 self .assert_span (num_spans = 0 )
980964
981965 def test_uninstrument_new_client (self ):
966+ HTTPXClientInstrumentor ().instrument ()
982967 client1 = self .create_client ()
983968 HTTPXClientInstrumentor ().uninstrument_client (client1 )
984969
@@ -1001,6 +986,7 @@ def test_uninstrument_new_client(self):
1001986
1002987 def test_instrument_proxy (self ):
1003988 proxy_mounts = self .create_proxy_mounts ()
989+ HTTPXClientInstrumentor ().instrument ()
1004990 client = self .create_client (mounts = proxy_mounts )
1005991 self .perform_request (self .URL , client = client )
1006992 self .assert_span (num_spans = 1 )
@@ -1027,7 +1013,6 @@ def print_handler(self, client):
10271013 return handler
10281014
10291015 def test_instrument_client_with_proxy (self ):
1030- HTTPXClientInstrumentor ().uninstrument ()
10311016 proxy_mounts = self .create_proxy_mounts ()
10321017 client = self .create_client (mounts = proxy_mounts )
10331018 self .assert_proxy_mounts (
@@ -1047,6 +1032,7 @@ def test_instrument_client_with_proxy(self):
10471032
10481033 def test_uninstrument_client_with_proxy (self ):
10491034 proxy_mounts = self .create_proxy_mounts ()
1035+ HTTPXClientInstrumentor ().instrument ()
10501036 client = self .create_client (mounts = proxy_mounts )
10511037 self .assert_proxy_mounts (
10521038 client ._mounts .values (),
@@ -1109,7 +1095,7 @@ def create_client(
11091095 transport : typing .Optional [SyncOpenTelemetryTransport ] = None ,
11101096 ** kwargs ,
11111097 ):
1112- return httpx .Client (** kwargs )
1098+ return httpx .Client (transport = transport , ** kwargs )
11131099
11141100 def perform_request (
11151101 self ,
@@ -1230,6 +1216,7 @@ class TestAsyncInstrumentationIntegration(BaseTestCases.BaseInstrumentorTest):
12301216 def setUp (self ):
12311217 super ().setUp ()
12321218 self .client2 = self .create_client ()
1219+ HTTPXClientInstrumentor ().instrument_client (self .client2 )
12331220
12341221 def create_client (
12351222 self ,
@@ -1283,7 +1270,6 @@ def test_async_response_hook_does_nothing_if_not_coroutine(self):
12831270 SpanAttributes .HTTP_STATUS_CODE : 200 ,
12841271 },
12851272 )
1286- HTTPXClientInstrumentor ().uninstrument ()
12871273
12881274 def test_async_request_hook_does_nothing_if_not_coroutine (self ):
12891275 HTTPXClientInstrumentor ().instrument (
@@ -1296,4 +1282,3 @@ def test_async_request_hook_does_nothing_if_not_coroutine(self):
12961282 self .assertEqual (result .text , "Hello!" )
12971283 span = self .assert_span ()
12981284 self .assertEqual (span .name , "GET" )
1299- HTTPXClientInstrumentor ().uninstrument ()
0 commit comments