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
20 changes: 20 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ func WithTracerProvider(provider trace.TracerProvider) Option {
})
}

// WithBatchQueryTracerProvider specifies a tracer provider to use for creating a tracer
// that is used specifically for spans within a batch's per-query operations.
//
// This is useful for those who may have lengthy batches and wish to either disable,
// or by some means sample the spans created for each individual query within a batch operation.
//
// For instance, one could choose to sample 50% of the per-query spans created within a batch
// by specifying the following tracer provider to WithBatchQueryTracerProvider:
// trace.NewTracerProvider(trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(0.5))))
//
// If WithBatchQueryTracerProvider is not specified, the same tracer provider as specified by WithTracerProvider is used.
// If WithTracerProvider is not specified either, then the same default as WithTracerProvider is used -- the OTel SDK global provider.
func WithBatchQueryTracerProvider(provider trace.TracerProvider) Option {
return optionFunc(func(cfg *tracerConfig) {
if provider != nil {
cfg.batchQueryTracerProvider = provider
}
})
}

// WithMeterProvider specifies a meter provider to use for creating a meter.
// If none is specified, the global provider is used.
func WithMeterProvider(provider metric.MeterProvider) Option {
Expand Down
30 changes: 21 additions & 9 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ var _ pgxpool.AcquireTracer = (*Tracer)(nil)
// Tracer is a wrapper around the pgx tracer interfaces which instrument
// queries with both tracing and metrics.
type Tracer struct {
tracer trace.Tracer
meter metric.Meter
tracerAttrs []attribute.KeyValue
meterAttrs []attribute.KeyValue
batchQueryTracer trace.Tracer
tracer trace.Tracer
meter metric.Meter
tracerAttrs []attribute.KeyValue
meterAttrs []attribute.KeyValue

operationDuration metric.Int64Histogram
operationErrors metric.Int64Counter
Expand All @@ -75,8 +76,9 @@ type Tracer struct {
}

type tracerConfig struct {
tracerProvider trace.TracerProvider
meterProvider metric.MeterProvider
batchQueryTracerProvider trace.TracerProvider
tracerProvider trace.TracerProvider
meterProvider metric.MeterProvider

tracerAttrs []attribute.KeyValue
meterAttrs []attribute.KeyValue
Expand All @@ -92,8 +94,9 @@ type tracerConfig struct {
// NewTracer returns a new Tracer.
func NewTracer(opts ...Option) *Tracer {
cfg := &tracerConfig{
tracerProvider: otel.GetTracerProvider(),
meterProvider: otel.GetMeterProvider(),
batchQueryTracerProvider: nil,
tracerProvider: otel.GetTracerProvider(),
meterProvider: otel.GetMeterProvider(),
tracerAttrs: []attribute.KeyValue{
semconv.DBSystemPostgreSQL,
},
Expand All @@ -112,7 +115,16 @@ func NewTracer(opts ...Option) *Tracer {
opt.apply(cfg)
}

// If a batchQueryTracerProvider was not optionally assigned,
// default to using the same TracerProvider as used for all other operations.
// This is guaranteed to be non-nil as this defaults to the global TracerProvider,
// unless otherwise optionally user-assigned as well.
if cfg.batchQueryTracerProvider == nil {
cfg.batchQueryTracerProvider = cfg.tracerProvider
}

tracer := &Tracer{
batchQueryTracer: cfg.batchQueryTracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(findOwnImportedVersion())),
tracer: cfg.tracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(findOwnImportedVersion())),
meter: cfg.meterProvider.Meter(meterName, metric.WithInstrumentationVersion(findOwnImportedVersion())),
tracerAttrs: cfg.tracerAttrs,
Expand Down Expand Up @@ -399,7 +411,7 @@ func (t *Tracer) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pgx.T
}
}

_, span := t.tracer.Start(ctx, spanName, opts...)
_, span := t.batchQueryTracer.Start(ctx, spanName, opts...)
recordSpanError(span, data.Err)

span.End()
Expand Down