CompositeQueryTracer is a decorator for pgx v5
that chains multiple tracers together. Assign it to ConnConfig.Tracer and
every database operation is dispatched to all registered tracers in order —
query, batch, connect, prepare, and copy-from.
go get github.com/pgx-contrib/pgxtraceconfig, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
panic(err)
}
config.ConnConfig.Tracer = pgxtrace.CompositeQueryTracer{
&myTracer{},
&anotherTracer{},
}
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
panic(err)
}
defer pool.Close()Each element only needs to implement pgx.QueryTracer. Elements that also
implement pgx.ConnectTracer, pgx.BatchTracer, pgx.PrepareTracer, or
pgx.CopyFromTracer are automatically called for those operations:
config.ConnConfig.Tracer = pgxtrace.CompositeQueryTracer{
&pgxotel.QueryTracer{Name: "my-service"}, // all five interfaces
&myAuditLogger{}, // QueryTracer only
}Open in VS Code with the Dev Containers extension. The environment provides Go, PostgreSQL 18, and Nix automatically.
PGX_DATABASE_URL=postgres://vscode@postgres:5432/pgxtrace?sslmode=disable
nix develop # enter shell with Go
go tool ginkgo run -r# Unit tests only (no database required)
go tool ginkgo run -r
# With integration tests
export PGX_DATABASE_URL="postgres://localhost/pgxtrace?sslmode=disable"
go tool ginkgo run -r