Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpx: add the is_instrumented flag to the transport #3106

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `opentelemetry-instrumentation-httpx` Fix `RequestInfo`/`ResponseInfo` type hints
([#3105](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3105))
- `opentelemetry-instrumentation-httpx` Add the `is_instrumented` flag to the transport
([#3106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3106))


## Version 1.29.0/0.50b0 (2024-12-11)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,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 @@ -529,6 +531,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 @@ -897,6 +901,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 @@ -948,6 +956,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 @@ -974,6 +983,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 @@ -989,9 +999,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