diff --git a/options.go b/options.go index 253366d..f987d4d 100644 --- a/options.go +++ b/options.go @@ -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 diff --git a/tracer.go b/tracer.go index ce1a130..6a46379 100644 --- a/tracer.go +++ b/tracer.go @@ -80,6 +80,7 @@ type Tracer struct { logSQLStatement bool logConnectionDetails bool includeParams bool + disableAcquireTracer bool } type tracerConfig struct { @@ -95,6 +96,7 @@ type tracerConfig struct { logSQLStatement bool logConnectionDetails bool includeParams bool + disableAcquireTracer bool } // NewTracer returns a new Tracer. @@ -114,6 +116,7 @@ func NewTracer(opts ...Option) *Tracer { logSQLStatement: true, logConnectionDetails: true, includeParams: false, + disableAcquireTracer: false, } for _, opt := range opts { @@ -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() @@ -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() { @@ -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)