Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pgconn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ type Config struct {
// that you close on FATAL errors by returning false.
OnPgError PgErrorHandler

// Tracer is an io.Writer to which the PostgreSQL frontend/backend protocol messages will be logged.
// The format roughly mimics the format produced by the libpq C function PQtrace. Messages are logged
// from the connection handshake onwards, providing visibility into authentication, parameter status
// messages, backend key data, and all subsequent protocol traffic.
Tracer io.Writer

// TracerOptions controls tracing behavior. Only relevant when Tracer is set.
TracerOptions pgproto3.TracerOptions

createdByParseConfig bool // Used to enforce created by ParseConfig rule.
}

Expand Down
4 changes: 4 additions & 0 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ func connectOne(ctx context.Context, config *Config, connectConfig *connectOneCo
pgConn.bgReaderStarted = make(chan struct{})
pgConn.frontend = config.BuildFrontend(pgConn.bgReader, pgConn.conn)

if config.Tracer != nil {
pgConn.frontend.Trace(config.Tracer, config.TracerOptions)
}

startupMsg := pgproto3.StartupMessage{
ProtocolVersion: pgproto3.ProtocolVersionNumber,
Parameters: make(map[string]string),
Expand Down
2 changes: 0 additions & 2 deletions pgproto3/backend.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pgproto3

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -105,7 +104,6 @@ func (b *Backend) Flush() error {
func (b *Backend) Trace(w io.Writer, options TracerOptions) {
b.tracer = &tracer{
w: w,
buf: &bytes.Buffer{},
TracerOptions: options,
}
}
Expand Down
2 changes: 0 additions & 2 deletions pgproto3/frontend.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pgproto3

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -124,7 +123,6 @@ func (f *Frontend) Flush() error {
func (f *Frontend) Trace(w io.Writer, options TracerOptions) {
f.tracer = &tracer{
w: w,
buf: &bytes.Buffer{},
TracerOptions: options,
}
}
Expand Down
Loading