From 859c974203edb4b686ecb6b7ddf07fa93cf7e702 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 14 Nov 2024 19:08:03 +0100 Subject: [PATCH] opentelemetry-instrumentation-httpx: make instrument_client a staticmethod again (#3003) * opentelemetry-instrumentation-httpx: make instrument_client a staticmethod again * ADd changelog * Can be a classmethod --------- Co-authored-by: Leighton Chen --- CHANGELOG.md | 3 +++ .../opentelemetry/instrumentation/httpx/__init__.py | 11 ++++++----- .../tests/test_httpx_integration.py | 9 ++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cae2dffb52..58e21e4194 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `opentelemetry-instrumentation-httpx`: instrument_client is a static method again + ([#3003](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3003)) + ### Breaking changes - `opentelemetry-instrumentation-sqlalchemy` teach instruments version diff --git a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py index d3a2cecfe6..4a2026e6de 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py @@ -938,8 +938,9 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals return response + @classmethod def instrument_client( - self, + cls, client: typing.Union[httpx.Client, httpx.AsyncClient], tracer_provider: TracerProvider = None, request_hook: typing.Union[ @@ -996,7 +997,7 @@ def instrument_client( client._transport, "handle_request", partial( - self._handle_request_wrapper, + cls._handle_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, request_hook=request_hook, @@ -1008,7 +1009,7 @@ def instrument_client( transport, "handle_request", partial( - self._handle_request_wrapper, + cls._handle_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, request_hook=request_hook, @@ -1021,7 +1022,7 @@ def instrument_client( client._transport, "handle_async_request", partial( - self._handle_async_request_wrapper, + cls._handle_async_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, async_request_hook=async_request_hook, @@ -1033,7 +1034,7 @@ def instrument_client( transport, "handle_async_request", partial( - self._handle_async_request_wrapper, + cls._handle_async_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, async_request_hook=async_request_hook, diff --git a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py index 07699700c4..b934ae0861 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py @@ -910,13 +910,20 @@ def test_suppress_instrumentation_new_client(self): self.assert_span(num_spans=0) - def test_instrument_client(self): + def test_instrument_client_called_on_the_instance(self): client = self.create_client() HTTPXClientInstrumentor().instrument_client(client) result = self.perform_request(self.URL, client=client) self.assertEqual(result.text, "Hello!") self.assert_span(num_spans=1) + def test_instrument_client_called_on_the_class(self): + client = self.create_client() + HTTPXClientInstrumentor.instrument_client(client) + result = self.perform_request(self.URL, client=client) + self.assertEqual(result.text, "Hello!") + self.assert_span(num_spans=1) + def test_instrumentation_without_client(self): HTTPXClientInstrumentor().instrument() results = [