|
| 1 | +# asyncpg |
| 2 | + |
| 3 | +The [`logfire.instrument_asyncpg()`][logfire.Logfire.instrument_asyncpg] function can be used to instrument the [asyncpg][asyncpg] PostgreSQL driver with **Logfire**. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install `logfire` with the `asyncpg` extra: |
| 8 | + |
| 9 | +{{ install_logfire(extras=['asyncpg']) }} |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Let's setup a PostgreSQL database using Docker and run a Python script that connects to the database using asyncpg to |
| 14 | +demonstrate how to use **Logfire** with asyncpg. |
| 15 | + |
| 16 | +### Setup a PostgreSQL Database Using Docker |
| 17 | + |
| 18 | +First, we need to initialize a PostgreSQL database. This can be easily done using Docker with the following command: |
| 19 | + |
| 20 | +```bash |
| 21 | +docker run --name postgres \ |
| 22 | + -e POSTGRES_USER=user \ |
| 23 | + -e POSTGRES_PASSWORD=secret \ |
| 24 | + -e POSTGRES_DB=database \ |
| 25 | + -p 5432:5432 -d postgres |
| 26 | +``` |
| 27 | + |
| 28 | +This command accomplishes the following: |
| 29 | + |
| 30 | +- `--name postgres`: This defines the name of the Docker container. |
| 31 | +- `-e POSTGRES_USER=user`: This sets a user for the PostgreSQL server. |
| 32 | +- `-e POSTGRES_PASSWORD=secret`: This sets a password for the PostgreSQL server. |
| 33 | +- `-e POSTGRES_DB=database`: This creates a new database named "database", the same as the one used in your Python script. |
| 34 | +- `-p 5432:5432`: This makes the PostgreSQL instance available on your local machine under port 5432. |
| 35 | +- `-d postgres`: This denotes the Docker image to be used, in this case, "postgres". |
| 36 | + |
| 37 | +### Run the Python script |
| 38 | + |
| 39 | +The following Python script connects to the PostgreSQL database and executes some SQL queries: |
| 40 | + |
| 41 | +```py |
| 42 | +import asyncio |
| 43 | + |
| 44 | +import asyncpg |
| 45 | + |
| 46 | +import logfire |
| 47 | + |
| 48 | +logfire.configure() |
| 49 | +logfire.instrument_asyncpg() |
| 50 | + |
| 51 | + |
| 52 | +async def main(): |
| 53 | + connection: asyncpg.Connection = await asyncpg.connect( |
| 54 | + user='user', password='secret', database='database', host='0.0.0.0', port=5432 |
| 55 | + ) |
| 56 | + |
| 57 | + with logfire.span('Create table and insert data'): |
| 58 | + await connection.execute('CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, num integer, data varchar);') |
| 59 | + |
| 60 | + # Insert some data |
| 61 | + await connection.execute('INSERT INTO test (num, data) VALUES ($1, $2)', 100, 'abc') |
| 62 | + await connection.execute('INSERT INTO test (num, data) VALUES ($1, $2)', 200, 'def') |
| 63 | + |
| 64 | + # Query the data |
| 65 | + for record in await connection.fetch('SELECT * FROM test'): |
| 66 | + logfire.info('Retrieved {record=}', record=record) |
| 67 | + |
| 68 | + |
| 69 | +asyncio.run(main()) |
| 70 | +``` |
| 71 | + |
| 72 | +If you go to your project on the UI, you will see the span created by the script. |
| 73 | + |
| 74 | +[opentelemetry-asyncpg]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asyncpg/asyncpg.html |
| 75 | +[opentelemetry-asyncpg2]: https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/asyncpg2/asyncpg2.html |
| 76 | +[asyncpg]: https://magicstack.github.io/asyncpg/ |
0 commit comments