Skip to content

Commit

Permalink
feat: add an option to remove the query span name prefix (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael DEMACON <quantumsheep@users.noreply.github.com>
Co-authored-by: Nathanael DEMACON <quantumsheep@users.noreply.github.com>
  • Loading branch information
quantumsheep and quantumsheep authored Jun 12, 2024
1 parent 32c5de8 commit 0ffcf87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func WithSpanNameFunc(fn SpanNameFunc) Option {
})
}

// WithDisableQuerySpanNamePrefix will disable the default prefix for the span
// name. By default, the span name is prefixed with "batch query" or "query".
func WithDisableQuerySpanNamePrefix() Option {
return optionFunc(func(cfg *tracerConfig) {
cfg.prefixQuerySpanName = false
})
}

// WithDisableSQLStatementInAttributes will disable logging the SQL statement in the span's
// attributes.
func WithDisableSQLStatementInAttributes() Option {
Expand Down
60 changes: 36 additions & 24 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ const (
// Tracer is a wrapper around the pgx tracer interfaces which instrument
// queries.
type Tracer struct {
tracer trace.Tracer
attrs []attribute.KeyValue
trimQuerySpanName bool
spanNameFunc SpanNameFunc
logSQLStatement bool
includeParams bool
tracer trace.Tracer
attrs []attribute.KeyValue
trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
includeParams bool
}

type tracerConfig struct {
tp trace.TracerProvider
attrs []attribute.KeyValue
trimQuerySpanName bool
spanNameFunc SpanNameFunc
logSQLStatement bool
includeParams bool
tp trace.TracerProvider
attrs []attribute.KeyValue
trimQuerySpanName bool
spanNameFunc SpanNameFunc
prefixQuerySpanName bool
logSQLStatement bool
includeParams bool
}

// NewTracer returns a new Tracer.
Expand All @@ -64,23 +66,25 @@ func NewTracer(opts ...Option) *Tracer {
attrs: []attribute.KeyValue{
semconv.DBSystemPostgreSQL,
},
trimQuerySpanName: false,
spanNameFunc: nil,
logSQLStatement: true,
includeParams: false,
trimQuerySpanName: false,
spanNameFunc: nil,
prefixQuerySpanName: true,
logSQLStatement: true,
includeParams: false,
}

for _, opt := range opts {
opt.apply(cfg)
}

return &Tracer{
tracer: cfg.tp.Tracer(tracerName, trace.WithInstrumentationVersion(findOwnImportedVersion())),
attrs: cfg.attrs,
trimQuerySpanName: cfg.trimQuerySpanName,
spanNameFunc: cfg.spanNameFunc,
logSQLStatement: cfg.logSQLStatement,
includeParams: cfg.includeParams,
tracer: cfg.tp.Tracer(tracerName, trace.WithInstrumentationVersion(findOwnImportedVersion())),
attrs: cfg.attrs,
trimQuerySpanName: cfg.trimQuerySpanName,
spanNameFunc: cfg.spanNameFunc,
prefixQuerySpanName: cfg.prefixQuerySpanName,
logSQLStatement: cfg.logSQLStatement,
includeParams: cfg.includeParams,
}
}

Expand Down Expand Up @@ -258,9 +262,17 @@ func (t *Tracer) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pgx.T

}

spanName := "batch query " + data.SQL
var spanName string
if t.trimQuerySpanName {
spanName = "query " + t.sqlOperationName(data.SQL)
spanName = t.sqlOperationName(data.SQL)
if t.prefixQuerySpanName {
spanName = "query " + spanName
}
} else {
spanName = data.SQL
if t.prefixQuerySpanName {
spanName = "batch query " + spanName
}
}

_, span := t.tracer.Start(ctx, spanName, opts...)
Expand Down

0 comments on commit 0ffcf87

Please sign in to comment.