Skip to content

Commit dc711e8

Browse files
rbagdocelotl
andauthored
Ensure httpx non-client methods are instrumented (#2538)
* Ensure httpx non-client methods are instrumented * Update changelog * Added subTest to distinguish tests inside a loop * Updated changelog * Add a comment explaining private attribute usage --------- Co-authored-by: Diego Hurtado <[email protected]>
1 parent 728976f commit dc711e8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4242

4343
### Fixed
4444

45-
- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version (#2500)[https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500]
45+
- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version
46+
([#2500](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500))
47+
- `opentelemetry-instrumentation-httpx` Ensure httpx.get or httpx.request like methods are instrumented
48+
([#2538](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2538))
4649
- `opentelemetry-instrumentation-grpc` AioClientInterceptor should propagate with a Metadata object
4750
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
4851
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource

instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,13 @@ def _instrument(self, **kwargs):
564564
tracer_provider = kwargs.get("tracer_provider")
565565
_InstrumentedClient._tracer_provider = tracer_provider
566566
_InstrumentedAsyncClient._tracer_provider = tracer_provider
567-
httpx.Client = _InstrumentedClient
567+
# Intentionally using a private attribute here, see:
568+
# https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2538#discussion_r1610603719
569+
httpx.Client = httpx._api.Client = _InstrumentedClient
568570
httpx.AsyncClient = _InstrumentedAsyncClient
569571

570572
def _uninstrument(self, **kwargs):
571-
httpx.Client = self._original_client
573+
httpx.Client = httpx._api.Client = self._original_client
572574
httpx.AsyncClient = self._original_async_client
573575
_InstrumentedClient._tracer_provider = None
574576
_InstrumentedClient._request_hook = None

instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,36 @@ def test_instrument_client(self):
532532
self.assertEqual(result.text, "Hello!")
533533
self.assert_span(num_spans=1)
534534

535+
def test_instrumentation_without_client(self):
536+
537+
HTTPXClientInstrumentor().instrument()
538+
results = [
539+
httpx.get(self.URL),
540+
httpx.request("GET", self.URL),
541+
]
542+
with httpx.stream("GET", self.URL) as stream:
543+
stream.read()
544+
results.append(stream)
545+
546+
spans = self.assert_span(num_spans=len(results))
547+
for idx, res in enumerate(results):
548+
with self.subTest(idx=idx, res=res):
549+
self.assertEqual(res.text, "Hello!")
550+
self.assertEqual(
551+
spans[idx].attributes[SpanAttributes.HTTP_URL],
552+
self.URL,
553+
)
554+
555+
HTTPXClientInstrumentor().uninstrument()
556+
535557
def test_uninstrument(self):
536558
HTTPXClientInstrumentor().instrument()
537559
HTTPXClientInstrumentor().uninstrument()
538560
client = self.create_client()
539561
result = self.perform_request(self.URL, client=client)
562+
result_no_client = httpx.get(self.URL)
540563
self.assertEqual(result.text, "Hello!")
564+
self.assertEqual(result_no_client.text, "Hello!")
541565
self.assert_span(num_spans=0)
542566

543567
def test_uninstrument_client(self):

0 commit comments

Comments
 (0)