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

Fix httpx resource warnings #1695

Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix exception in Urllib3 when dealing with filelike body.
([#1399](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1399))

- Fix httpx resource warnings
([#1695](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1695))

### Added

- Add connection attributes to sqlalchemy connect span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def response_hook(span, request, response):
"""
import logging
import typing
from types import TracebackType

import httpx

Expand Down Expand Up @@ -293,6 +294,18 @@ def __init__(
self._request_hook = request_hook
self._response_hook = response_hook

def __enter__(self) -> "SyncOpenTelemetryTransport":
self._transport.__enter__()
return self

def __exit__(
self,
exc_type: typing.Optional[typing.Type[BaseException]] = None,
exc_value: typing.Optional[BaseException] = None,
traceback: typing.Optional[TracebackType] = None,
) -> None:
self._transport.__exit__(exc_type, exc_value, traceback)

def handle_request(
self,
*args,
Expand Down Expand Up @@ -343,6 +356,9 @@ def handle_request(

return response

def close(self) -> None:
self._transport.close()


class AsyncOpenTelemetryTransport(httpx.AsyncBaseTransport):
"""Async transport class that will trace all requests made with a client.
Expand Down Expand Up @@ -372,6 +388,18 @@ def __init__(
self._request_hook = request_hook
self._response_hook = response_hook

async def __aenter__(self) -> "AsyncOpenTelemetryTransport":
await self._transport.__aenter__()
return self

async def __aexit__(
self,
exc_type: typing.Optional[typing.Type[BaseException]] = None,
exc_value: typing.Optional[BaseException] = None,
traceback: typing.Optional[TracebackType] = None,
) -> None:
await self._transport.__aexit__(exc_type, exc_value, traceback)

async def handle_async_request(
self, *args, **kwargs
) -> typing.Union[
Expand Down Expand Up @@ -423,6 +451,9 @@ async def handle_async_request(

return response

async def aclose(self) -> None:
await self._transport.aclose()


class _InstrumentedClient(httpx.Client):
_tracer_provider = None
Expand Down