Skip to content
Draft
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- `opentelemetry-instrumentation-httpx` Instrument transport instead of HTTPX client
([#3106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3106))

## Version 1.29.0/0.50b0 (2024-12-11)

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ class SyncOpenTelemetryTransport(httpx.BaseTransport):
that is called right before the span ends
"""

_is_instrumented_by_opentelemetry = True

def __init__(
self,
transport: httpx.BaseTransport,
Expand Down Expand Up @@ -527,6 +529,8 @@ class AsyncOpenTelemetryTransport(httpx.AsyncBaseTransport):
that is called right before the span ends
"""

_is_instrumented_by_opentelemetry = True

def __init__(
self,
transport: httpx.AsyncBaseTransport,
Expand Down Expand Up @@ -895,6 +899,10 @@ def instrument_client(
"Attempting to instrument Httpx client while already instrumented"
)
return
if getattr(
client._transport, "is_instrumented_by_opentelemetry", False
):
return

_OpenTelemetrySemanticConventionStability._initialize()
sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
Expand Down Expand Up @@ -946,6 +954,7 @@ def instrument_client(
response_hook=response_hook,
),
)
client._transport._is_instrumented_by_opentelemetry = True
client._is_instrumented_by_opentelemetry = True
if hasattr(client._transport, "handle_async_request"):
wrap_function_wrapper(
Expand All @@ -972,6 +981,7 @@ def instrument_client(
async_response_hook=async_response_hook,
),
)
client._transport._is_instrumented_by_opentelemetry = True
client._is_instrumented_by_opentelemetry = True

@staticmethod
Expand All @@ -987,9 +997,11 @@ def uninstrument_client(
unwrap(client._transport, "handle_request")
for transport in client._mounts.values():
unwrap(transport, "handle_request")
client._transport._is_instrumented_by_opentelemetry = False
client._is_instrumented_by_opentelemetry = False
elif hasattr(client._transport, "handle_async_request"):
unwrap(client._transport, "handle_async_request")
for transport in client._mounts.values():
unwrap(transport, "handle_async_request")
client._transport._is_instrumented_by_opentelemetry = False
client._is_instrumented_by_opentelemetry = False
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

# pylint: disable=too-many-lines

from __future__ import annotations

import abc
import asyncio
import typing
Expand Down Expand Up @@ -593,10 +595,10 @@ def create_transport(
@abc.abstractmethod
def create_client(
self,
transport: typing.Union[
SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport, None
] = None,
**kwargs,
transport: SyncOpenTelemetryTransport
| AsyncOpenTelemetryTransport
| None = None,
**kwargs: typing.Any,
):
pass

Expand Down Expand Up @@ -730,9 +732,9 @@ class BaseInstrumentorTest(BaseTest, metaclass=abc.ABCMeta):
@abc.abstractmethod
def create_client(
self,
transport: typing.Union[
SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport, None
] = None,
transport: SyncOpenTelemetryTransport
| AsyncOpenTelemetryTransport
| None = None,
**kwargs,
):
pass
Expand Down Expand Up @@ -926,6 +928,17 @@ def test_instrument_client_called_on_the_class(self):
self.assertEqual(result.text, "Hello!")
self.assert_span(num_spans=1)

def test_instrument_multiple_clients_with_the_same_transport(self):
client1 = self.create_client()
client2 = self.create_client(transport=client1._transport)

HTTPXClientInstrumentor().instrument_client(client1)
HTTPXClientInstrumentor().instrument_client(client2)

result = self.perform_request(self.URL, client=client1)
self.assertEqual(result.text, "Hello!")
self.assert_span(num_spans=1)

def test_instrumentation_without_client(self):
HTTPXClientInstrumentor().instrument()
results = [
Expand Down Expand Up @@ -1210,7 +1223,7 @@ def create_client(
transport: typing.Optional[SyncOpenTelemetryTransport] = None,
**kwargs,
):
return httpx.Client(**kwargs)
return httpx.Client(transport=transport, **kwargs)

def perform_request(
self,
Expand Down Expand Up @@ -1260,7 +1273,7 @@ def create_client(
transport: typing.Optional[AsyncOpenTelemetryTransport] = None,
**kwargs,
):
return httpx.AsyncClient(**kwargs)
return httpx.AsyncClient(transport=transport, **kwargs)

def perform_request(
self,
Expand Down
Loading