|
8 | 8 | from ddtrace.contrib.internal.asyncpg.patch import patch |
9 | 9 | from ddtrace.contrib.internal.asyncpg.patch import unpatch |
10 | 10 | from ddtrace.contrib.internal.trace_utils import iswrapped |
| 11 | +from ddtrace.internal.utils.version import parse_version |
11 | 12 | from ddtrace.trace import Pin |
12 | 13 | from ddtrace.trace import tracer |
13 | 14 | from tests.contrib.asyncio.utils import AsyncioTestCase |
@@ -315,6 +316,58 @@ async def test(): |
315 | 316 | assert err == b"" |
316 | 317 |
|
317 | 318 |
|
| 319 | +@pytest.mark.skipif( |
| 320 | + parse_version(getattr(asyncpg, "__version__", "0.0.0")) < (0, 30, 0), |
| 321 | + reason="the custom connect parameter for create_pool requires asyncpg >= 0.30.0", |
| 322 | +) |
| 323 | +@pytest.mark.snapshot() |
| 324 | +@pytest.mark.asyncio |
| 325 | +async def test_pool_custom_connect(): |
| 326 | + """ |
| 327 | + Test that if someone uses a custom connect parameter when creating a pool, |
| 328 | + the tracer doesn't cause a _TracedConnection error or throw any other errors |
| 329 | + The connect option was introduced in 0.30.0 |
| 330 | + """ |
| 331 | + database_url = f"postgresql://{POSTGRES_CONFIG['user']}:{POSTGRES_CONFIG['password']}@{POSTGRES_CONFIG['host']}:{POSTGRES_CONFIG['port']}/{POSTGRES_CONFIG['dbname']}" |
| 332 | + |
| 333 | + try: |
| 334 | + # The default is 10 connection pools so the integration will create 10 spans in the snapshot |
| 335 | + pool = await asyncpg.create_pool(database_url, connect=asyncpg.connect) |
| 336 | + |
| 337 | + async with pool.acquire() as conn: |
| 338 | + result = await conn.fetchval("SELECT 1") |
| 339 | + assert result == 1 |
| 340 | + |
| 341 | + await pool.close() |
| 342 | + except Exception as err: |
| 343 | + raise err |
| 344 | + |
| 345 | + assert True |
| 346 | + |
| 347 | + |
| 348 | +@pytest.mark.snapshot() |
| 349 | +@pytest.mark.asyncio |
| 350 | +async def test_pool_without_custom_connect(): |
| 351 | + """ |
| 352 | + Test that create_pool without the connect option still works |
| 353 | + """ |
| 354 | + database_url = f"postgresql://{POSTGRES_CONFIG['user']}:{POSTGRES_CONFIG['password']}@{POSTGRES_CONFIG['host']}:{POSTGRES_CONFIG['port']}/{POSTGRES_CONFIG['dbname']}" |
| 355 | + |
| 356 | + try: |
| 357 | + # The default is 10 connection pools so the integration will create 10 spans in the snapshot |
| 358 | + pool = await asyncpg.create_pool(database_url) |
| 359 | + |
| 360 | + async with pool.acquire() as conn: |
| 361 | + result = await conn.fetchval("SELECT 1") |
| 362 | + assert result == 1 |
| 363 | + |
| 364 | + await pool.close() |
| 365 | + except Exception as err: |
| 366 | + raise err |
| 367 | + |
| 368 | + assert True |
| 369 | + |
| 370 | + |
318 | 371 | def test_patch_unpatch_asyncpg(): |
319 | 372 | assert iswrapped(asyncpg.connect) |
320 | 373 | assert iswrapped(asyncpg.protocol.Protocol.execute) |
|
0 commit comments