Skip to content

Commit b15b83c

Browse files
committed
green tests \o/
1 parent f0de350 commit b15b83c

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,18 @@ def _instrument(self, **kwargs):
736736
tracer_provider = kwargs.get("tracer_provider")
737737
self._request_hook = kwargs.get("request_hook")
738738
self._response_hook = kwargs.get("response_hook")
739-
self._async_request_hook = kwargs.get("async_request_hook")
740-
self._async_response_hook = kwargs.get("async_response_hook")
739+
_async_request_hook = kwargs.get("async_request_hook")
740+
self._async_request_hook = (
741+
_async_request_hook
742+
if iscoroutinefunction(_async_request_hook)
743+
else None
744+
)
745+
_async_response_hook = kwargs.get("async_response_hook")
746+
self._async_response_hook = (
747+
_async_response_hook
748+
if iscoroutinefunction(_async_response_hook)
749+
else None
750+
)
741751

742752
_OpenTelemetrySemanticConventionStability._initialize()
743753
self._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
@@ -829,7 +839,7 @@ def _handle_request_wrapper(self, wrapped, instance, args, kwargs):
829839
span.set_attribute(
830840
ERROR_TYPE, type(exception).__qualname__
831841
)
832-
raise exception.with_traceback(exception.__traceback__)
842+
raise exception
833843

834844
return response
835845

@@ -898,7 +908,7 @@ async def _handle_async_request_wrapper(
898908
span.set_attribute(
899909
ERROR_TYPE, type(exception).__qualname__
900910
)
901-
raise exception.with_traceback(exception.__traceback__)
911+
raise exception
902912

903913
return response
904914

@@ -930,6 +940,19 @@ def instrument_client(
930940
)
931941
return
932942

943+
# FIXME: sharing state in the instrumentor instance maybe it's not that great, need to pass tracer and semconv to each
944+
# instance separately
945+
_OpenTelemetrySemanticConventionStability._initialize()
946+
self._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
947+
_OpenTelemetryStabilitySignalType.HTTP,
948+
)
949+
self._tracer = get_tracer(
950+
__name__,
951+
instrumenting_library_version=__version__,
952+
tracer_provider=tracer_provider,
953+
schema_url=_get_schema_url(self._sem_conv_opt_in_mode),
954+
)
955+
933956
if iscoroutinefunction(request_hook):
934957
self._async_request_hook = request_hook
935958
self._request_hook = None
@@ -950,13 +973,27 @@ def instrument_client(
950973
"handle_request",
951974
self._handle_request_wrapper,
952975
)
976+
for transport in client._mounts.values():
977+
# FIXME: check it's not wrapped already?
978+
wrap_function_wrapper(
979+
transport,
980+
"handle_request",
981+
self._handle_request_wrapper,
982+
)
953983
client._is_instrumented_by_opentelemetry = True
954984
if hasattr(client._transport, "handle_async_request"):
955985
wrap_function_wrapper(
956986
client._transport,
957987
"handle_async_request",
958988
self._handle_async_request_wrapper,
959989
)
990+
for transport in client._mounts.values():
991+
# FIXME: check it's not wrapped already?
992+
wrap_function_wrapper(
993+
transport,
994+
"handle_async_request",
995+
self._handle_async_request_wrapper,
996+
)
960997
client._is_instrumented_by_opentelemetry = True
961998

962999
@staticmethod
@@ -970,7 +1007,11 @@ def uninstrument_client(
9701007
"""
9711008
if hasattr(client._transport, "handle_request"):
9721009
unwrap(client._transport, "handle_request")
1010+
for transport in client._mounts.values():
1011+
unwrap(transport, "handle_request")
9731012
client._is_instrumented_by_opentelemetry = False
9741013
elif hasattr(client._transport, "handle_async_request"):
9751014
unwrap(client._transport, "handle_async_request")
1015+
for transport in client._mounts.values():
1016+
unwrap(transport, "handle_async_request")
9761017
client._is_instrumented_by_opentelemetry = False

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)