Skip to content

Commit

Permalink
Fix httpx resource warnings (#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-mosleh authored Mar 8, 2023
1 parent e5d9ac5 commit 4199751
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit 4199751

Please sign in to comment.