|
23 | 23 |
|
24 | 24 | .. code-block:: python
|
25 | 25 |
|
| 26 | + import asyncio |
26 | 27 | import aiopg
|
27 | 28 | from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
|
28 | 29 | # Call instrument() to wrap all database connections
|
29 | 30 | AiopgInstrumentor().instrument()
|
30 | 31 |
|
31 |
| - cnx = await aiopg.connect(database='Database') |
32 |
| - cursor = await cnx.cursor() |
33 |
| - await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
34 |
| - await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
35 |
| - cursor.close() |
36 |
| - cnx.close() |
| 32 | + dsn = 'user=user password=password host=127.0.0.1' |
37 | 33 |
|
38 |
| - pool = await aiopg.create_pool(database='Database') |
| 34 | + async def connect(): |
| 35 | + cnx = await aiopg.connect(dsn) |
| 36 | + cursor = await cnx.cursor() |
| 37 | + await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
| 38 | + await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
| 39 | + cursor.close() |
| 40 | + cnx.close() |
39 | 41 |
|
40 |
| - cnx = await pool.acquire() |
41 |
| - cursor = await cnx.cursor() |
42 |
| - await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
43 |
| - await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
44 |
| - cursor.close() |
45 |
| - cnx.close() |
| 42 | + async def create_pool(): |
| 43 | + pool = await aiopg.create_pool(dsn) |
| 44 | + cnx = await pool.acquire() |
| 45 | + cursor = await cnx.cursor() |
| 46 | + await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
| 47 | + await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
| 48 | + cursor.close() |
| 49 | + cnx.close() |
| 50 | +
|
| 51 | + asyncio.run(connect()) |
| 52 | + asyncio.run(create_pool()) |
46 | 53 |
|
47 | 54 | .. code-block:: python
|
48 | 55 |
|
| 56 | + import asyncio |
49 | 57 | import aiopg
|
50 | 58 | from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
|
51 | 59 |
|
| 60 | + dsn = 'user=user password=password host=127.0.0.1' |
| 61 | +
|
52 | 62 | # Alternatively, use instrument_connection for an individual connection
|
53 |
| - cnx = await aiopg.connect(database='Database') |
54 |
| - instrumented_cnx = AiopgInstrumentor().instrument_connection(cnx) |
55 |
| - cursor = await instrumented_cnx.cursor() |
56 |
| - await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
57 |
| - await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
58 |
| - cursor.close() |
59 |
| - instrumented_cnx.close() |
| 63 | + async def go(): |
| 64 | + cnx = await aiopg.connect(dsn) |
| 65 | + instrumented_cnx = AiopgInstrumentor().instrument_connection(cnx) |
| 66 | + cursor = await instrumented_cnx.cursor() |
| 67 | + await cursor.execute("CREATE TABLE IF NOT EXISTS test (testField INTEGER)") |
| 68 | + await cursor.execute("INSERT INTO test (testField) VALUES (123)") |
| 69 | + cursor.close() |
| 70 | + instrumented_cnx.close() |
| 71 | +
|
| 72 | + asyncio.run(go()) |
60 | 73 |
|
61 | 74 | API
|
62 | 75 | ---
|
|
0 commit comments