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

Ensure httpx non-client methods are instrumented #2538

Merged
merged 5 commits into from
May 30, 2024
Merged
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
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
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
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):
xrmx marked this conversation as resolved.
Show resolved Hide resolved
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
Loading