Skip to content

Commit

Permalink
feat: add ability to disable logging connection details in spans (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavantix authored Jan 27, 2025
1 parent 588e676 commit 52421dd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func WithDisableQuerySpanNamePrefix() Option {
})
}

// WithDisableConnectionDetailsInAttributes will disable logging the connection details.
// in the span's attributes.
func WithDisableConnectionDetailsInAttributes() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.logConnectionDetails = false
})
}

// WithDisableSQLStatementInAttributes will disable logging the SQL statement in the span's
// attributes.
func WithDisableSQLStatementInAttributes() Option {
Expand Down
81 changes: 41 additions & 40 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ type Tracer struct {
operationDuration metric.Int64Histogram
operationErrors metric.Int64Counter

trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
includeParams bool
trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
logConnectionDetails bool
includeParams bool
}

type tracerConfig struct {
Expand All @@ -79,11 +80,12 @@ type tracerConfig struct {
tracerAttrs []attribute.KeyValue
meterAttrs []attribute.KeyValue

trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
includeParams bool
trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
logConnectionDetails bool
includeParams bool
}

// NewTracer returns a new Tracer.
Expand All @@ -97,11 +99,12 @@ func NewTracer(opts ...Option) *Tracer {
meterAttrs: []attribute.KeyValue{
semconv.DBSystemPostgreSQL,
},
trimQuerySpanName: false,
spanNameFunc: nil,
prefixQuerySpanName: true,
logSQLStatement: true,
includeParams: false,
trimQuerySpanName: false,
spanNameFunc: nil,
prefixQuerySpanName: true,
logSQLStatement: true,
logConnectionDetails: true,
includeParams: false,
}

for _, opt := range opts {
Expand Down Expand Up @@ -200,19 +203,17 @@ func (t *Tracer) sqlOperationName(stmt string) string {
return strings.ToUpper(parts[0])
}

// connectionAttributesFromConfig returns a slice of SpanStartOptions that contain
// connectionAttributesFromConfig returns a SpanStartOption that contains
// attributes from the given connection config.
func connectionAttributesFromConfig(config *pgx.ConnConfig) []trace.SpanStartOption {
func connectionAttributesFromConfig(config *pgx.ConnConfig) trace.SpanStartOption {
if config != nil {
return []trace.SpanStartOption{
trace.WithAttributes(
semconv.DBSystemPostgreSQL,
semconv.ServerAddress(config.Host),
semconv.ServerPort(int(config.Port)),
semconv.UserName(config.User),
semconv.DBNamespace(config.Database),
),
}
return trace.WithAttributes(
semconv.DBSystemPostgreSQL,
semconv.ServerAddress(config.Host),
semconv.ServerPort(int(config.Port)),
semconv.UserName(config.User),
semconv.DBNamespace(config.Database),
)
}
return nil
}
Expand All @@ -231,8 +232,8 @@ func (t *Tracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.T
trace.WithAttributes(t.tracerAttrs...),
}

if conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config())...)
if t.logConnectionDetails && conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config()))
}

if t.logSQLStatement {
Expand Down Expand Up @@ -291,8 +292,8 @@ func (t *Tracer) TraceCopyFromStart(ctx context.Context, conn *pgx.Conn, data pg
trace.WithAttributes(semconv.DBCollectionName(data.TableName.Sanitize())),
}

if conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config())...)
if t.logConnectionDetails && conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config()))
}

ctx, _ = t.tracer.Start(ctx, "copy_from "+data.TableName.Sanitize(), opts...)
Expand Down Expand Up @@ -336,8 +337,8 @@ func (t *Tracer) TraceBatchStart(ctx context.Context, conn *pgx.Conn, data pgx.T
trace.WithAttributes(semconv.DBOperationBatchSize(size)),
}

if conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config())...)
if t.logConnectionDetails && conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config()))
}

ctx, _ = t.tracer.Start(ctx, "batch start", opts...)
Expand All @@ -358,8 +359,8 @@ func (t *Tracer) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pgx.T
trace.WithAttributes(t.tracerAttrs...),
}

if conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config())...)
if t.logConnectionDetails && conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config()))
}

if t.logSQLStatement {
Expand Down Expand Up @@ -418,8 +419,8 @@ func (t *Tracer) TraceConnectStart(ctx context.Context, data pgx.TraceConnectSta
trace.WithAttributes(t.tracerAttrs...),
}

if data.ConnConfig != nil {
opts = append(opts, connectionAttributesFromConfig(data.ConnConfig)...)
if t.logConnectionDetails && data.ConnConfig != nil {
opts = append(opts, connectionAttributesFromConfig(data.ConnConfig))
}

ctx, _ = t.tracer.Start(ctx, "connect", opts...)
Expand Down Expand Up @@ -457,8 +458,8 @@ func (t *Tracer) TracePrepareStart(ctx context.Context, conn *pgx.Conn, data pgx
trace.WithAttributes(PrepareStmtNameKey.String(data.Name))
}

if conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config())...)
if t.logConnectionDetails && conn != nil {
opts = append(opts, connectionAttributesFromConfig(conn.Config()))
}

opts = append(opts, trace.WithAttributes(semconv.DBOperationName(t.sqlOperationName(data.SQL))))
Expand Down Expand Up @@ -505,8 +506,8 @@ func (t *Tracer) TraceAcquireStart(ctx context.Context, pool *pgxpool.Pool, data
trace.WithAttributes(t.tracerAttrs...),
}

if pool != nil && pool.Config() != nil && pool.Config().ConnConfig != nil {
opts = append(opts, connectionAttributesFromConfig(pool.Config().ConnConfig)...)
if t.logConnectionDetails && pool != nil && pool.Config() != nil && pool.Config().ConnConfig != nil {
opts = append(opts, connectionAttributesFromConfig(pool.Config().ConnConfig))
}

ctx, _ = t.tracer.Start(ctx, "pool.acquire", opts...)
Expand Down

0 comments on commit 52421dd

Please sign in to comment.