Skip to content

Commit cc7169c

Browse files
authored
Improve aiopg instrumentation examples (#3468)
1 parent 45797ec commit cc7169c

File tree

1 file changed

+33
-20
lines changed
  • instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg

1 file changed

+33
-20
lines changed

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,53 @@
2323
2424
.. code-block:: python
2525
26+
import asyncio
2627
import aiopg
2728
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
2829
# Call instrument() to wrap all database connections
2930
AiopgInstrumentor().instrument()
3031
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'
3733
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()
3941
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())
4653
4754
.. code-block:: python
4855
56+
import asyncio
4957
import aiopg
5058
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
5159
60+
dsn = 'user=user password=password host=127.0.0.1'
61+
5262
# 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())
6073
6174
API
6275
---

0 commit comments

Comments
 (0)