diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e434c2e6..9bd207b86c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## Fixed + +- Fix aiopg instrumentation to work with aiopg < 2.0.0 + ([#1473](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1473)) - `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION` ([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507)) + ## Version 1.15.0/0.36b0 (2022-12-10) - Add uninstrument test for sqlalchemy diff --git a/instrumentation/README.md b/instrumentation/README.md index 0439bc1cae..98ebb4c728 100644 --- a/instrumentation/README.md +++ b/instrumentation/README.md @@ -3,7 +3,7 @@ | --------------- | ------------------ | --------------- | | [opentelemetry-instrumentation-aio-pika](./opentelemetry-instrumentation-aio-pika) | aio_pika ~= 7.2.0 | No | [opentelemetry-instrumentation-aiohttp-client](./opentelemetry-instrumentation-aiohttp-client) | aiohttp ~= 3.0 | No -| [opentelemetry-instrumentation-aiopg](./opentelemetry-instrumentation-aiopg) | aiopg >= 0.13.0, < 1.3.0 | No +| [opentelemetry-instrumentation-aiopg](./opentelemetry-instrumentation-aiopg) | aiopg >= 0.13.0, < 2.0.0 | No | [opentelemetry-instrumentation-asgi](./opentelemetry-instrumentation-asgi) | asgiref ~= 3.0 | No | [opentelemetry-instrumentation-asyncpg](./opentelemetry-instrumentation-asyncpg) | asyncpg >= 0.12.0 | No | [opentelemetry-instrumentation-aws-lambda](./opentelemetry-instrumentation-aws-lambda) | aws_lambda | No diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/pyproject.toml b/instrumentation/opentelemetry-instrumentation-aiopg/pyproject.toml index f89853b403..b2326d8586 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/pyproject.toml +++ b/instrumentation/opentelemetry-instrumentation-aiopg/pyproject.toml @@ -33,7 +33,7 @@ dependencies = [ [project.optional-dependencies] instruments = [ - "aiopg >= 0.13.0, < 1.3.0", + "aiopg >= 0.13.0, < 2.0.0", ] test = [ "opentelemetry-instrumentation-aiopg[instruments]", diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py index d140c18838..bc3006b843 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py @@ -1,7 +1,8 @@ +import asyncio import typing +from collections.abc import Coroutine import wrapt -from aiopg.utils import _ContextManager, _PoolAcquireContextManager from opentelemetry.instrumentation.dbapi import ( CursorTracer, @@ -150,3 +151,78 @@ async def callproc(self, *args, **kwargs): return result return AsyncCursorTracerProxy(cursor, *args, **kwargs) + + +class _ContextManager(Coroutine): + __slots__ = ("_coro", "_obj") + + def __init__(self, coro): + self._coro = coro + self._obj = None + + def send(self, value): + return self._coro.send(value) + + def throw(self, typ, val=None, tb=None): + if val is None: + return self._coro.throw(typ) + if tb is None: + return self._coro.throw(typ, val) + return self._coro.throw(typ, val, tb) + + def close(self): + return self._coro.close() + + @property + def gi_frame(self): + return self._coro.gi_frame + + @property + def gi_running(self): + return self._coro.gi_running + + @property + def gi_code(self): + return self._coro.gi_code + + def __next__(self): + return self.send(None) + + def __await__(self): + resp = self._coro.__await__() + return resp + + async def __aenter__(self): + self._obj = await self._coro + return self._obj + + async def __aexit__(self, exc_type, exc, t_b): + try: + if asyncio.iscoroutinefunction(self._obj.close): + await self._obj.close() + else: + self._obj.close() + finally: + self._obj = None + + +class _PoolContextManager(_ContextManager): + __slots__ = () + + async def __aexit__(self, exc_type, exc, tb): + self._obj.close() + await self._obj.wait_closed() + self._obj = None + + +class _PoolAcquireContextManager(_ContextManager): + __slots__ = ("_coro", "_obj", "_pool") + + def __init__(self, coro, pool): + super().__init__(coro) + self._pool = pool + + async def __aexit__(self, exc_type, exc, tb): + await self._pool.release(self._obj) + self._pool = None + self._obj = None diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/package.py b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/package.py index 660a8577c7..75aa9bedb8 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/package.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/package.py @@ -13,4 +13,4 @@ # limitations under the License. -_instruments = ("aiopg >= 0.13.0, < 1.3.0",) +_instruments = ("aiopg >= 0.13.0, < 2.0.0",) diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/wrappers.py b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/wrappers.py index f786772c49..c4252615b8 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/wrappers.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/wrappers.py @@ -34,14 +34,12 @@ import aiopg import wrapt -from aiopg.utils import ( # pylint: disable=no-name-in-module - _ContextManager, - _PoolContextManager, -) from opentelemetry.instrumentation.aiopg.aiopg_integration import ( AiopgIntegration, AsyncProxyObject, + _ContextManager, + _PoolContextManager, get_traced_connection_proxy, ) from opentelemetry.instrumentation.aiopg.version import __version__ diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py b/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py index 3917d277f7..b5a248f488 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py @@ -17,16 +17,14 @@ from unittest.mock import MagicMock import aiopg -from aiopg.utils import ( # pylint: disable=no-name-in-module - _ContextManager, - _PoolAcquireContextManager, -) import opentelemetry.instrumentation.aiopg from opentelemetry import trace as trace_api from opentelemetry.instrumentation.aiopg import AiopgInstrumentor, wrappers from opentelemetry.instrumentation.aiopg.aiopg_integration import ( AiopgIntegration, + _ContextManager, + _PoolAcquireContextManager, ) from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py index 80a3dfb7dd..36fda70ab1 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py @@ -25,7 +25,7 @@ "instrumentation": "opentelemetry-instrumentation-aiohttp-client==0.37b0.dev", }, "aiopg": { - "library": "aiopg >= 0.13.0, < 1.3.0", + "library": "aiopg >= 0.13.0, < 2.0.0", "instrumentation": "opentelemetry-instrumentation-aiopg==0.37b0.dev", }, "asgiref": {