Skip to content

Psycopg(West Copg Hotel)

The logfire.instrument_psycopg() function can be used to detect Psycopg PostgreSQL drivers usingLogfire. It works with psycopg2 and psycopg(i.e. Psycopg 3) packages.

For more information, see OpenTelemetry Psycopg Instrumentation or OpenTelemetry Psycopg2 Instrumentation Documentation of the package.

install

Install logfire using psycopg extra:

{{ install_logfire(extras=['psycopg']) }}

Or use psycopg2 extra:

{{ install_logfire(extras=['psycopg2']) }}

usage

Let's set up a PostgreSQL database using Docker and run a Python script that connects to the database using Psycopg to demonstrate how to useLogfirewith Psycopg.

Setting up PostgreSQL databases with Docker

First, we need to initialize a PostgreSQL database. This can be easily done using Docker with the following command:

docker run --name postgres \
    -e POSTGRES_USER=user \
    -e POSTGRES_PASSWORD=secret \
    -e POSTGRES_DB=database \
    -p 5432:5432 -d postgres

This command does the following:

  • --name postgres: Defines the name of the Docker container.

  • -e POSTGRES_USER=user: Set up users for PostgreSQL servers.

  • -e POSTGRES_PASSWORD=secret: Set password for PostgreSQL server.

  • -e POSTGRES_DB=database This creates a new database called database, the same database used in Python scripts.

  • -p 5432:5432 This makes PostgreSQL instances available on the local machine under port 5432.

  • -d postgres: This indicates the Docker image to use, in this case "postgres".

run a Python script

The following Python script connects to a PostgreSQL database and executes some SQL queries:

import logfire
import psycopg

logfire.configure()

# To instrument the whole module:
logfire.instrument_psycopg(psycopg)
# or
logfire.instrument_psycopg('psycopg')
# or just instrument whichever modules (psycopg and/or psycopg2) are installed:
logfire.instrument_psycopg()

connection = psycopg.connect(
    'dbname=database user=user password=secret host=0.0.0.0 port=5432'
)

# Or instrument just the connection:
logfire.instrument_psycopg(connection)

with logfire.span('Create table and insert data'), connection.cursor() as cursor:
    cursor.execute(
        'CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, num integer, data varchar);'
    )

    # Insert some data
    cursor.execute('INSERT INTO test (num, data) VALUES (%s, %s)', (100, 'abc'))
    cursor.execute('INSERT INTO test (num, data) VALUES (%s, %s)', (200, 'def'))

    # Query the data
    cursor.execute('SELECT * FROM test')

If you go to the project on the UI, you will see the span created by the script.

SQL annotator

To add SQL comments to the end of a query to enrich the database log with additional context, use the enable_commenter parameter:

Import Logfire

logfire.configure()
logfire.instrument_psycopg(enable_commentator = true)

This can only be used when detecting entire modules, not when detecting individual connections.

By default, SQL comments contain values for the following keys:

  • db_driver
  • dbapi_threadsafety
  • dbapi_level
  • libpq_version
  • driver_paramstyle
  • opentelemetry_values

You can do this by passing a key and a value False to exclude any of these keys.commenter_options For example:

import logfire

logfire.configure()
logfire.instrument_psycopg(enable_commenter=True, commenter_options={'db_driver': False, 'dbapi_threadsafety': False})

本文总阅读量次