From be44afac262c64d23e1b33673555484f58569fac Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 14 Nov 2024 16:35:25 +0100 Subject: [PATCH 1/3] opentelemetry-instrumentation-httpx: make instrument_client a staticmethod again --- .../opentelemetry/instrumentation/httpx/__init__.py | 10 +++++----- .../tests/test_httpx_integration.py | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) 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..489086de47 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,8 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals return response + @staticmethod def instrument_client( - self, client: typing.Union[httpx.Client, httpx.AsyncClient], tracer_provider: TracerProvider = None, request_hook: typing.Union[ @@ -996,7 +996,7 @@ def instrument_client( client._transport, "handle_request", partial( - self._handle_request_wrapper, + HTTPXClientInstrumentor._handle_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, request_hook=request_hook, @@ -1008,7 +1008,7 @@ def instrument_client( transport, "handle_request", partial( - self._handle_request_wrapper, + HTTPXClientInstrumentor._handle_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, request_hook=request_hook, @@ -1021,7 +1021,7 @@ def instrument_client( client._transport, "handle_async_request", partial( - self._handle_async_request_wrapper, + HTTPXClientInstrumentor._handle_async_request_wrapper, tracer=tracer, sem_conv_opt_in_mode=sem_conv_opt_in_mode, async_request_hook=async_request_hook, @@ -1033,7 +1033,7 @@ def instrument_client( transport, "handle_async_request", partial( - self._handle_async_request_wrapper, + HTTPXClientInstrumentor._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..2205f756c8 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py @@ -917,6 +917,13 @@ def test_instrument_client(self): self.assertEqual(result.text, "Hello!") self.assert_span(num_spans=1) + def test_instrument_client_a_static_method(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 = [ From acf5fee72af004efdc3515c681971a15b4e11e3e Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 14 Nov 2024 16:40:53 +0100 Subject: [PATCH 2/3] ADd changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24760db6c8..772384bdde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,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 From 19898dfb5a72dd67b3773f9b0e563c2e78770ad2 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Thu, 14 Nov 2024 18:44:39 +0100 Subject: [PATCH 3/3] Can be a classmethod --- .../opentelemetry/instrumentation/httpx/__init__.py | 11 ++++++----- .../tests/test_httpx_integration.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) 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 489086de47..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 - @staticmethod + @classmethod def instrument_client( + 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( - HTTPXClientInstrumentor._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( - HTTPXClientInstrumentor._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( - HTTPXClientInstrumentor._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( - HTTPXClientInstrumentor._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 2205f756c8..b934ae0861 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py @@ -910,14 +910,14 @@ 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_a_static_method(self): + 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)