Skip to content

Commit 12da395

Browse files
authored
fix instrumentation of connection when pool.acquire was called multiple times (#381)
1 parent c9bca4e commit 12da395

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
([#350](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/350))
2424
- `opentelemetry-exporter-datadog` Fix warning when DatadogFormat encounters a request with
2525
no DD_ORIGIN headers ([#368](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/368)).
26+
- `opentelemetry-instrumentation-aiopg` Fix multiple nested spans when
27+
`aiopg.pool` is used
28+
([#336](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/381)).
2629
- Updated instrumentations to use `opentelemetry.trace.use_span` instead of `Tracer.use_span()`
2730
([#364](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/364))
2831
- `opentelemetry-propagator-ot-trace` Do not throw an exception when headers are not present

instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ def acquire(self):
8686
async def _acquire(self):
8787
# pylint: disable=protected-access
8888
connection = await self.__wrapped__._acquire()
89-
return get_traced_connection_proxy(
90-
connection, db_api_integration, *args, **kwargs
91-
)
89+
if not isinstance(connection, AsyncProxyObject):
90+
connection = get_traced_connection_proxy(
91+
connection, db_api_integration, *args, **kwargs
92+
)
93+
return connection
9294

9395
return TracedPoolProxy(pool, *args, **kwargs)
9496

tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py

+20
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def setUpClass(cls):
117117
cls._cursor = None
118118
cls._tracer = cls.tracer_provider.get_tracer(__name__)
119119
AiopgInstrumentor().instrument(tracer_provider=cls.tracer_provider)
120+
cls._dsn = (
121+
f"dbname='{POSTGRES_DB_NAME}' user='{POSTGRES_USER}' password='{POSTGRES_PASSWORD}'"
122+
f" host='{POSTGRES_HOST}' port='{POSTGRES_PORT}'"
123+
)
120124
cls._pool = async_call(
121125
aiopg.create_pool(
122126
dbname=POSTGRES_DB_NAME,
@@ -185,3 +189,19 @@ def test_callproc(self):
185189
):
186190
async_call(self._cursor.callproc("test", ()))
187191
self.validate_spans("test")
192+
193+
def test_instrumented_pool_with_multiple_acquires(self, *_, **__):
194+
async def double_acquire():
195+
pool = await aiopg.create_pool(dsn=self._dsn)
196+
async with pool.acquire() as conn:
197+
async with conn.cursor() as cursor:
198+
query = "SELECT 1"
199+
await cursor.execute(query)
200+
async with pool.acquire() as conn:
201+
async with conn.cursor() as cursor:
202+
query = "SELECT 1"
203+
await cursor.execute(query)
204+
205+
async_call(double_acquire())
206+
spans = self.memory_exporter.get_finished_spans()
207+
self.assertEqual(len(spans), 2)

0 commit comments

Comments
 (0)