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 <lechen@microsoft.com>
This commit is contained in:
Riccardo Magliocchetti
2024-11-14 19:08:03 +01:00
committed by GitHub
parent c32cc7a3e2
commit 803bb322ba
3 changed files with 17 additions and 6 deletions

View File

@ -20,6 +20,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- `opentelemetry-instrumentation-httpx`: instrument_client is a static method again
([#3003](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3003))
### Breaking changes ### Breaking changes
- `opentelemetry-instrumentation-sqlalchemy` teach instruments version - `opentelemetry-instrumentation-sqlalchemy` teach instruments version

View File

@ -938,8 +938,9 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
return response return response
@classmethod
def instrument_client( def instrument_client(
self, cls,
client: typing.Union[httpx.Client, httpx.AsyncClient], client: typing.Union[httpx.Client, httpx.AsyncClient],
tracer_provider: TracerProvider = None, tracer_provider: TracerProvider = None,
request_hook: typing.Union[ request_hook: typing.Union[
@ -996,7 +997,7 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
client._transport, client._transport,
"handle_request", "handle_request",
partial( partial(
self._handle_request_wrapper, cls._handle_request_wrapper,
tracer=tracer, tracer=tracer,
sem_conv_opt_in_mode=sem_conv_opt_in_mode, sem_conv_opt_in_mode=sem_conv_opt_in_mode,
request_hook=request_hook, request_hook=request_hook,
@ -1008,7 +1009,7 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
transport, transport,
"handle_request", "handle_request",
partial( partial(
self._handle_request_wrapper, cls._handle_request_wrapper,
tracer=tracer, tracer=tracer,
sem_conv_opt_in_mode=sem_conv_opt_in_mode, sem_conv_opt_in_mode=sem_conv_opt_in_mode,
request_hook=request_hook, request_hook=request_hook,
@ -1021,7 +1022,7 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
client._transport, client._transport,
"handle_async_request", "handle_async_request",
partial( partial(
self._handle_async_request_wrapper, cls._handle_async_request_wrapper,
tracer=tracer, tracer=tracer,
sem_conv_opt_in_mode=sem_conv_opt_in_mode, sem_conv_opt_in_mode=sem_conv_opt_in_mode,
async_request_hook=async_request_hook, async_request_hook=async_request_hook,
@ -1033,7 +1034,7 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
transport, transport,
"handle_async_request", "handle_async_request",
partial( partial(
self._handle_async_request_wrapper, cls._handle_async_request_wrapper,
tracer=tracer, tracer=tracer,
sem_conv_opt_in_mode=sem_conv_opt_in_mode, sem_conv_opt_in_mode=sem_conv_opt_in_mode,
async_request_hook=async_request_hook, async_request_hook=async_request_hook,

View File

@ -910,13 +910,20 @@ class BaseTestCases:
self.assert_span(num_spans=0) self.assert_span(num_spans=0)
def test_instrument_client(self): def test_instrument_client_called_on_the_instance(self):
client = self.create_client() client = self.create_client()
HTTPXClientInstrumentor().instrument_client(client) HTTPXClientInstrumentor().instrument_client(client)
result = self.perform_request(self.URL, client=client) result = self.perform_request(self.URL, client=client)
self.assertEqual(result.text, "Hello!") self.assertEqual(result.text, "Hello!")
self.assert_span(num_spans=1) 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): def test_instrumentation_without_client(self):
HTTPXClientInstrumentor().instrument() HTTPXClientInstrumentor().instrument()
results = [ results = [