Skip to content

Commit e7c8efe

Browse files
committed
green tests \o/
1 parent dd3d236 commit e7c8efe

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-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
@@ -733,8 +733,18 @@ def _instrument(self, **kwargs):
733733
tracer_provider = kwargs.get("tracer_provider")
734734
self._request_hook = kwargs.get("request_hook")
735735
self._response_hook = kwargs.get("response_hook")
736-
self._async_request_hook = kwargs.get("async_request_hook")
737-
self._async_response_hook = kwargs.get("async_response_hook")
736+
_async_request_hook = kwargs.get("async_request_hook")
737+
self._async_request_hook = (
738+
_async_request_hook
739+
if iscoroutinefunction(_async_request_hook)
740+
else None
741+
)
742+
_async_response_hook = kwargs.get("async_response_hook")
743+
self._async_response_hook = (
744+
_async_response_hook
745+
if iscoroutinefunction(_async_response_hook)
746+
else None
747+
)
738748

739749
_OpenTelemetrySemanticConventionStability._initialize()
740750
self._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
@@ -826,7 +836,7 @@ def _handle_request_wrapper(self, wrapped, instance, args, kwargs):
826836
span.set_attribute(
827837
ERROR_TYPE, type(exception).__qualname__
828838
)
829-
raise exception.with_traceback(exception.__traceback__)
839+
raise exception
830840

831841
return response
832842

@@ -895,7 +905,7 @@ async def _handle_async_request_wrapper(
895905
span.set_attribute(
896906
ERROR_TYPE, type(exception).__qualname__
897907
)
898-
raise exception.with_traceback(exception.__traceback__)
908+
raise exception
899909

900910
return response
901911

@@ -927,6 +937,19 @@ def instrument_client(
927937
)
928938
return
929939

940+
# FIXME: sharing state in the instrumentor instance maybe it's not that great, need to pass tracer and semconv to each
941+
# instance separately
942+
_OpenTelemetrySemanticConventionStability._initialize()
943+
self._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
944+
_OpenTelemetryStabilitySignalType.HTTP,
945+
)
946+
self._tracer = get_tracer(
947+
__name__,
948+
instrumenting_library_version=__version__,
949+
tracer_provider=tracer_provider,
950+
schema_url=_get_schema_url(self._sem_conv_opt_in_mode),
951+
)
952+
930953
if iscoroutinefunction(request_hook):
931954
self._async_request_hook = request_hook
932955
self._request_hook = None
@@ -947,13 +970,27 @@ def instrument_client(
947970
"handle_request",
948971
self._handle_request_wrapper,
949972
)
973+
for transport in client._mounts.values():
974+
# FIXME: check it's not wrapped already?
975+
wrap_function_wrapper(
976+
transport,
977+
"handle_request",
978+
self._handle_request_wrapper,
979+
)
950980
client._is_instrumented_by_opentelemetry = True
951981
if hasattr(client._transport, "handle_async_request"):
952982
wrap_function_wrapper(
953983
client._transport,
954984
"handle_async_request",
955985
self._handle_async_request_wrapper,
956986
)
987+
for transport in client._mounts.values():
988+
# FIXME: check it's not wrapped already?
989+
wrap_function_wrapper(
990+
transport,
991+
"handle_async_request",
992+
self._handle_async_request_wrapper,
993+
)
957994
client._is_instrumented_by_opentelemetry = True
958995

959996
@staticmethod
@@ -967,7 +1004,11 @@ def uninstrument_client(
9671004
"""
9681005
if hasattr(client._transport, "handle_request"):
9691006
unwrap(client._transport, "handle_request")
1007+
for transport in client._mounts.values():
1008+
unwrap(transport, "handle_request")
9701009
client._is_instrumented_by_opentelemetry = False
9711010
elif hasattr(client._transport, "handle_async_request"):
9721011
unwrap(client._transport, "handle_async_request")
1012+
for transport in client._mounts.values():
1013+
unwrap(transport, "handle_async_request")
9731014
client._is_instrumented_by_opentelemetry = False

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

Lines changed: 11 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,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

Comments
 (0)