Let's setup a PostgreSQL database using Docker and run a Python script that connects to the database using asyncpg to
demonstrate how to use Logfire with asyncpg.
The following Python script connects to the PostgreSQL database and executes some SQL queries:
importasyncioimportasyncpgimportlogfirelogfire.configure()logfire.instrument_asyncpg()asyncdefmain():connection:asyncpg.Connection=awaitasyncpg.connect(user='user',password='secret',database='database',host='0.0.0.0',port=5432)withlogfire.span('Create table and insert data'):awaitconnection.execute('CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, num integer, data varchar);')# Insert some dataawaitconnection.execute('INSERT INTO test (num, data) VALUES ($1, $2)',100,'abc')awaitconnection.execute('INSERT INTO test (num, data) VALUES ($1, $2)',200,'def')# Query the dataforrecordinawaitconnection.fetch('SELECT * FROM test'):logfire.info('Retrieved {record=}',record=record)asyncio.run(main())
If you go to your project on the UI, you will see the span created by the script.