Skip to content

Commit

Permalink
Ensure httpx non-client methods are instrumented (#2538)
Browse files Browse the repository at this point in the history
* 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 <ocelotl@users.noreply.github.com>
  • Loading branch information
rbagd and ocelotl committed May 30, 2024
1 parent 728976f commit dc711e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version (#2500)[https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500]
- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version
([#2500](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500))
- `opentelemetry-instrumentation-httpx` Ensure httpx.get or httpx.request like methods are instrumented
([#2538](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2538))
- `opentelemetry-instrumentation-grpc` AioClientInterceptor should propagate with a Metadata object
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,13 @@ def _instrument(self, **kwargs):
tracer_provider = kwargs.get("tracer_provider")
_InstrumentedClient._tracer_provider = tracer_provider
_InstrumentedAsyncClient._tracer_provider = tracer_provider
httpx.Client = _InstrumentedClient
# Intentionally using a private attribute here, see:
# https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2538#discussion_r1610603719
httpx.Client = httpx._api.Client = _InstrumentedClient
httpx.AsyncClient = _InstrumentedAsyncClient

def _uninstrument(self, **kwargs):
httpx.Client = self._original_client
httpx.Client = httpx._api.Client = self._original_client
httpx.AsyncClient = self._original_async_client
_InstrumentedClient._tracer_provider = None
_InstrumentedClient._request_hook = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,36 @@ def test_instrument_client(self):
self.assertEqual(result.text, "Hello!")
self.assert_span(num_spans=1)

def test_instrumentation_without_client(self):

HTTPXClientInstrumentor().instrument()
results = [
httpx.get(self.URL),
httpx.request("GET", self.URL),
]
with httpx.stream("GET", self.URL) as stream:
stream.read()
results.append(stream)

spans = self.assert_span(num_spans=len(results))
for idx, res in enumerate(results):
with self.subTest(idx=idx, res=res):
self.assertEqual(res.text, "Hello!")
self.assertEqual(
spans[idx].attributes[SpanAttributes.HTTP_URL],
self.URL,
)

HTTPXClientInstrumentor().uninstrument()

def test_uninstrument(self):
HTTPXClientInstrumentor().instrument()
HTTPXClientInstrumentor().uninstrument()
client = self.create_client()
result = self.perform_request(self.URL, client=client)
result_no_client = httpx.get(self.URL)
self.assertEqual(result.text, "Hello!")
self.assertEqual(result_no_client.text, "Hello!")
self.assert_span(num_spans=0)

def test_uninstrument_client(self):
Expand Down

0 comments on commit dc711e8

Please sign in to comment.