Skip to content
Merged
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
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ func WithTrimSQLInSpanName() Option {
})
}

// WithDisableAcquireTracer disables tracing for connection acquire events from
// the connection pool. By default, acquire tracing is enabled.
func WithDisableAcquireTracer() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.disableAcquireTracer = true
})
}

// SpanNameFunc is a function that can be used to generate a span name for a
// SQL. The function will be called with the SQL statement as a parameter.
type SpanNameFunc func(stmt string) string
Expand Down
28 changes: 21 additions & 7 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Tracer struct {
logSQLStatement bool
logConnectionDetails bool
includeParams bool
disableAcquireTracer bool
}

type tracerConfig struct {
Expand All @@ -95,6 +96,7 @@ type tracerConfig struct {
logSQLStatement bool
logConnectionDetails bool
includeParams bool
disableAcquireTracer bool
}

// NewTracer returns a new Tracer.
Expand All @@ -114,6 +116,7 @@ func NewTracer(opts ...Option) *Tracer {
logSQLStatement: true,
logConnectionDetails: true,
includeParams: false,
disableAcquireTracer: false,
}

for _, opt := range opts {
Expand All @@ -135,13 +138,14 @@ func NewTracer(opts ...Option) *Tracer {
return &s
},
},
tracerAttrs: cfg.tracerAttrs,
meterAttrs: cfg.meterAttrs,
trimQuerySpanName: cfg.trimQuerySpanName,
spanNameCtxFunc: cfg.spanNameCtxFunc,
prefixQuerySpanName: cfg.prefixQuerySpanName,
logSQLStatement: cfg.logSQLStatement,
includeParams: cfg.includeParams,
tracerAttrs: cfg.tracerAttrs,
meterAttrs: cfg.meterAttrs,
trimQuerySpanName: cfg.trimQuerySpanName,
spanNameCtxFunc: cfg.spanNameCtxFunc,
prefixQuerySpanName: cfg.prefixQuerySpanName,
logSQLStatement: cfg.logSQLStatement,
includeParams: cfg.includeParams,
disableAcquireTracer: cfg.disableAcquireTracer,
}

tracer.createMetrics()
Expand Down Expand Up @@ -596,7 +600,12 @@ func (t *Tracer) TracePrepareEnd(ctx context.Context, _ *pgx.Conn, data pgx.Trac

// TraceAcquireStart is called at the beginning of Acquire.
// The returned context is used for the rest of the call and will be passed to the TraceAcquireEnd.
// If WithDisableAcquireTracer was set, then the function is no-op.
func (t *Tracer) TraceAcquireStart(ctx context.Context, pool *pgxpool.Pool, data pgxpool.TraceAcquireStartData) context.Context {
if t.disableAcquireTracer {
return ctx
}

ctx = context.WithValue(ctx, startTimeCtxKey{}, time.Now())

if !trace.SpanFromContext(ctx).IsRecording() {
Expand Down Expand Up @@ -629,7 +638,12 @@ func (t *Tracer) TraceAcquireStart(ctx context.Context, pool *pgxpool.Pool, data
}

// TraceAcquireEnd is called when a connection has been acquired.
// If WithDisableAcquireTracer was set, then the function is no-op.
func (t *Tracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, data pgxpool.TraceAcquireEndData) {
if t.disableAcquireTracer {
return
}

span := trace.SpanFromContext(ctx)
t.incrementOperationErrorCount(ctx, data.Err, pgxOperationAcquire)
t.recordOperationDuration(ctx, pgxOperationAcquire)
Expand Down