@@ -167,8 +167,6 @@ def setUp(self):
167
167
)
168
168
)
169
169
170
- HTTPXClientInstrumentor ().instrument ()
171
-
172
170
def print_spans (self , spans ):
173
171
for span in spans :
174
172
print (span .name , span .attributes )
@@ -751,8 +749,9 @@ def create_proxy_transport(self, url: str):
751
749
752
750
def setUp (self ):
753
751
super ().setUp ()
754
- HTTPXClientInstrumentor ().instrument ()
755
752
self .client = self .create_client ()
753
+ # FIXME: calling instrument() instead fixes 13*2 tests :(
754
+ HTTPXClientInstrumentor ().instrument_client (self .client )
756
755
757
756
def tearDown (self ):
758
757
HTTPXClientInstrumentor ().uninstrument ()
@@ -792,7 +791,6 @@ def test_custom_tracer_provider(self):
792
791
result = self .create_tracer_provider (resource = resource )
793
792
tracer_provider , exporter = result
794
793
795
- HTTPXClientInstrumentor ().uninstrument ()
796
794
HTTPXClientInstrumentor ().instrument (
797
795
tracer_provider = tracer_provider
798
796
)
@@ -802,7 +800,6 @@ def test_custom_tracer_provider(self):
802
800
self .assertEqual (result .text , "Hello!" )
803
801
span = self .assert_span (exporter = exporter )
804
802
self .assertIs (span .resource , resource )
805
- HTTPXClientInstrumentor ().uninstrument ()
806
803
807
804
def test_response_hook (self ):
808
805
response_hook_key = (
@@ -811,7 +808,6 @@ def test_response_hook(self):
811
808
else "response_hook"
812
809
)
813
810
response_hook_kwargs = {response_hook_key : self .response_hook }
814
- HTTPXClientInstrumentor ().uninstrument ()
815
811
HTTPXClientInstrumentor ().instrument (
816
812
tracer_provider = self .tracer_provider ,
817
813
** response_hook_kwargs ,
@@ -830,10 +826,8 @@ def test_response_hook(self):
830
826
HTTP_RESPONSE_BODY : "Hello!" ,
831
827
},
832
828
)
833
- HTTPXClientInstrumentor ().uninstrument ()
834
829
835
830
def test_response_hook_sync_async_kwargs (self ):
836
- HTTPXClientInstrumentor ().uninstrument ()
837
831
HTTPXClientInstrumentor ().instrument (
838
832
tracer_provider = self .tracer_provider ,
839
833
response_hook = _response_hook ,
@@ -845,15 +839,14 @@ def test_response_hook_sync_async_kwargs(self):
845
839
self .assertEqual (result .text , "Hello!" )
846
840
span = self .assert_span ()
847
841
self .assertEqual (
848
- dict ( span .attributes ) ,
842
+ span .attributes ,
849
843
{
850
844
SpanAttributes .HTTP_METHOD : "GET" ,
851
845
SpanAttributes .HTTP_URL : self .URL ,
852
846
SpanAttributes .HTTP_STATUS_CODE : 200 ,
853
847
HTTP_RESPONSE_BODY : "Hello!" ,
854
848
},
855
849
)
856
- HTTPXClientInstrumentor ().uninstrument ()
857
850
858
851
def test_request_hook (self ):
859
852
request_hook_key = (
@@ -862,7 +855,6 @@ def test_request_hook(self):
862
855
else "request_hook"
863
856
)
864
857
request_hook_kwargs = {request_hook_key : self .request_hook }
865
- HTTPXClientInstrumentor ().uninstrument ()
866
858
HTTPXClientInstrumentor ().instrument (
867
859
tracer_provider = self .tracer_provider ,
868
860
** request_hook_kwargs ,
@@ -873,10 +865,8 @@ def test_request_hook(self):
873
865
self .assertEqual (result .text , "Hello!" )
874
866
span = self .assert_span ()
875
867
self .assertEqual (span .name , "GET" + self .URL )
876
- HTTPXClientInstrumentor ().uninstrument ()
877
868
878
869
def test_request_hook_sync_async_kwargs (self ):
879
- HTTPXClientInstrumentor ().uninstrument ()
880
870
HTTPXClientInstrumentor ().instrument (
881
871
tracer_provider = self .tracer_provider ,
882
872
request_hook = _request_hook ,
@@ -888,10 +878,8 @@ def test_request_hook_sync_async_kwargs(self):
888
878
self .assertEqual (result .text , "Hello!" )
889
879
span = self .assert_span ()
890
880
self .assertEqual (span .name , "GET" + self .URL )
891
- HTTPXClientInstrumentor ().uninstrument ()
892
881
893
882
def test_request_hook_no_span_update (self ):
894
- HTTPXClientInstrumentor ().uninstrument ()
895
883
HTTPXClientInstrumentor ().instrument (
896
884
tracer_provider = self .tracer_provider ,
897
885
request_hook = self .no_update_request_hook ,
@@ -902,10 +890,8 @@ def test_request_hook_no_span_update(self):
902
890
self .assertEqual (result .text , "Hello!" )
903
891
span = self .assert_span ()
904
892
self .assertEqual (span .name , "GET" )
905
- HTTPXClientInstrumentor ().uninstrument ()
906
893
907
894
def test_not_recording (self ):
908
- HTTPXClientInstrumentor ().uninstrument ()
909
895
with mock .patch ("opentelemetry.trace.INVALID_SPAN" ) as mock_span :
910
896
HTTPXClientInstrumentor ().instrument (
911
897
tracer_provider = trace .NoOpTracerProvider ()
@@ -921,28 +907,26 @@ def test_not_recording(self):
921
907
self .assertTrue (mock_span .is_recording .called )
922
908
self .assertFalse (mock_span .set_attribute .called )
923
909
self .assertFalse (mock_span .set_status .called )
924
- HTTPXClientInstrumentor ().uninstrument ()
925
910
926
911
def test_suppress_instrumentation_new_client (self ):
927
- HTTPXClientInstrumentor ().uninstrument ()
928
912
HTTPXClientInstrumentor ().instrument ()
929
913
with suppress_http_instrumentation ():
930
914
client = self .create_client ()
931
915
result = self .perform_request (self .URL , client = client )
932
916
self .assertEqual (result .text , "Hello!" )
933
917
934
918
self .assert_span (num_spans = 0 )
935
- HTTPXClientInstrumentor ().uninstrument ()
936
919
937
920
def test_instrument_client (self ):
938
- HTTPXClientInstrumentor ().uninstrument ()
939
921
client = self .create_client ()
940
922
HTTPXClientInstrumentor ().instrument_client (client )
941
923
result = self .perform_request (self .URL , client = client )
942
924
self .assertEqual (result .text , "Hello!" )
943
925
self .assert_span (num_spans = 1 )
944
926
945
927
def test_instrumentation_without_client (self ):
928
+
929
+ HTTPXClientInstrumentor ().instrument ()
946
930
results = [
947
931
httpx .get (self .URL ),
948
932
httpx .request ("GET" , self .URL ),
@@ -961,6 +945,7 @@ def test_instrumentation_without_client(self):
961
945
)
962
946
963
947
def test_uninstrument (self ):
948
+ HTTPXClientInstrumentor ().instrument ()
964
949
HTTPXClientInstrumentor ().uninstrument ()
965
950
client = self .create_client ()
966
951
result = self .perform_request (self .URL , client = client )
@@ -970,7 +955,6 @@ def test_uninstrument(self):
970
955
self .assert_span (num_spans = 0 )
971
956
972
957
def test_uninstrument_client (self ):
973
- HTTPXClientInstrumentor ().uninstrument ()
974
958
HTTPXClientInstrumentor ().uninstrument_client (self .client )
975
959
976
960
result = self .perform_request (self .URL )
@@ -979,6 +963,7 @@ def test_uninstrument_client(self):
979
963
self .assert_span (num_spans = 0 )
980
964
981
965
def test_uninstrument_new_client (self ):
966
+ HTTPXClientInstrumentor ().instrument ()
982
967
client1 = self .create_client ()
983
968
HTTPXClientInstrumentor ().uninstrument_client (client1 )
984
969
@@ -1001,6 +986,7 @@ def test_uninstrument_new_client(self):
1001
986
1002
987
def test_instrument_proxy (self ):
1003
988
proxy_mounts = self .create_proxy_mounts ()
989
+ HTTPXClientInstrumentor ().instrument ()
1004
990
client = self .create_client (mounts = proxy_mounts )
1005
991
self .perform_request (self .URL , client = client )
1006
992
self .assert_span (num_spans = 1 )
@@ -1027,7 +1013,6 @@ def print_handler(self, client):
1027
1013
return handler
1028
1014
1029
1015
def test_instrument_client_with_proxy (self ):
1030
- HTTPXClientInstrumentor ().uninstrument ()
1031
1016
proxy_mounts = self .create_proxy_mounts ()
1032
1017
client = self .create_client (mounts = proxy_mounts )
1033
1018
self .assert_proxy_mounts (
@@ -1047,6 +1032,7 @@ def test_instrument_client_with_proxy(self):
1047
1032
1048
1033
def test_uninstrument_client_with_proxy (self ):
1049
1034
proxy_mounts = self .create_proxy_mounts ()
1035
+ HTTPXClientInstrumentor ().instrument ()
1050
1036
client = self .create_client (mounts = proxy_mounts )
1051
1037
self .assert_proxy_mounts (
1052
1038
client ._mounts .values (),
@@ -1109,7 +1095,7 @@ def create_client(
1109
1095
transport : typing .Optional [SyncOpenTelemetryTransport ] = None ,
1110
1096
** kwargs ,
1111
1097
):
1112
- return httpx .Client (** kwargs )
1098
+ return httpx .Client (transport = transport , ** kwargs )
1113
1099
1114
1100
def perform_request (
1115
1101
self ,
@@ -1230,6 +1216,7 @@ class TestAsyncInstrumentationIntegration(BaseTestCases.BaseInstrumentorTest):
1230
1216
def setUp (self ):
1231
1217
super ().setUp ()
1232
1218
self .client2 = self .create_client ()
1219
+ HTTPXClientInstrumentor ().instrument_client (self .client2 )
1233
1220
1234
1221
def create_client (
1235
1222
self ,
@@ -1283,7 +1270,6 @@ def test_async_response_hook_does_nothing_if_not_coroutine(self):
1283
1270
SpanAttributes .HTTP_STATUS_CODE : 200 ,
1284
1271
},
1285
1272
)
1286
- HTTPXClientInstrumentor ().uninstrument ()
1287
1273
1288
1274
def test_async_request_hook_does_nothing_if_not_coroutine (self ):
1289
1275
HTTPXClientInstrumentor ().instrument (
@@ -1296,4 +1282,3 @@ def test_async_request_hook_does_nothing_if_not_coroutine(self):
1296
1282
self .assertEqual (result .text , "Hello!" )
1297
1283
span = self .assert_span ()
1298
1284
self .assertEqual (span .name , "GET" )
1299
- HTTPXClientInstrumentor ().uninstrument ()
0 commit comments