diff --git a/instrumentation/github.com/gocql/gocql/otelgocql/config.go b/instrumentation/github.com/gocql/gocql/otelgocql/config.go index 18685705aaf..f7735e6ebc0 100644 --- a/instrumentation/github.com/gocql/gocql/otelgocql/config.go +++ b/instrumentation/github.com/gocql/gocql/otelgocql/config.go @@ -17,6 +17,7 @@ package otelgocql // import "go.opentelemetry.io/contrib/instrumentation/github. import ( "github.com/gocql/gocql" + "go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql/otelgocql/internal" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" @@ -32,6 +33,10 @@ type config struct { queryObserver gocql.QueryObserver batchObserver gocql.BatchObserver connectObserver gocql.ConnectObserver + + querySpanNameFormatter func(gocql.ObservedQuery) string + batchSpanNameFormatter func(gocql.ObservedBatch) string + connectSpanNameFormatter func(gocql.ObservedConnect) string } // Option applies a configuration option. @@ -117,6 +122,30 @@ func WithConnectInstrumentation(enabled bool) Option { }) } +// WithQuerySpanNameFormatter will allow customizing the span name when +// observing queries. Defaults to the query being executed. +func WithQuerySpanNameFormatter(querySpanNameFormatter func(gocql.ObservedQuery) string) Option { + return optionFunc(func(c *config) { + c.querySpanNameFormatter = querySpanNameFormatter + }) +} + +// WithBatchSpanNameFormatter will allow customizing the span name when +// observing batches. Defaults to "Batch Query". +func WithBatchSpanNameFormatter(batchSpanNameFormatter func(gocql.ObservedBatch) string) Option { + return optionFunc(func(c *config) { + c.batchSpanNameFormatter = batchSpanNameFormatter + }) +} + +// WithConnectSpanNameFormatter will allow customizing the span name when +// observing connects. Defaults to "New Connection". +func WithConnectSpanNameFormatter(connectSpanNameFormatter func(gocql.ObservedConnect) string) Option { + return optionFunc(func(c *config) { + c.connectSpanNameFormatter = connectSpanNameFormatter + }) +} + func newConfig(options ...Option) *config { cfg := &config{ tracerProvider: otel.GetTracerProvider(), @@ -124,6 +153,18 @@ func newConfig(options ...Option) *config { instrumentQuery: true, instrumentBatch: true, instrumentConnect: true, + queryObserver: nil, + batchObserver: nil, + connectObserver: nil, + querySpanNameFormatter: func(query gocql.ObservedQuery) string { + return query.Statement + }, + batchSpanNameFormatter: func(batch gocql.ObservedBatch) string { + return internal.CassBatchQueryName + }, + connectSpanNameFormatter: func(connect gocql.ObservedConnect) string { + return internal.CassConnectName + }, } for _, apply := range options { diff --git a/instrumentation/github.com/gocql/gocql/otelgocql/gocql.go b/instrumentation/github.com/gocql/gocql/otelgocql/gocql.go index 9b490dd3015..b1ecfa72a3a 100644 --- a/instrumentation/github.com/gocql/gocql/otelgocql/gocql.go +++ b/instrumentation/github.com/gocql/gocql/otelgocql/gocql.go @@ -34,23 +34,26 @@ func NewSessionWithTracing(ctx context.Context, cluster *gocql.ClusterConfig, op trace.WithInstrumentationVersion(SemVersion()), ) cluster.QueryObserver = &OTelQueryObserver{ - enabled: cfg.instrumentQuery, - observer: cfg.queryObserver, - tracer: tracer, - inst: instruments, + enabled: cfg.instrumentQuery, + observer: cfg.queryObserver, + tracer: tracer, + inst: instruments, + spanNameFormatter: cfg.querySpanNameFormatter, } cluster.BatchObserver = &OTelBatchObserver{ - enabled: cfg.instrumentBatch, - observer: cfg.batchObserver, - tracer: tracer, - inst: instruments, + enabled: cfg.instrumentBatch, + observer: cfg.batchObserver, + tracer: tracer, + inst: instruments, + spanNameFormatter: cfg.batchSpanNameFormatter, } cluster.ConnectObserver = &OTelConnectObserver{ - ctx: ctx, - enabled: cfg.instrumentConnect, - observer: cfg.connectObserver, - tracer: tracer, - inst: instruments, + ctx: ctx, + enabled: cfg.instrumentConnect, + observer: cfg.connectObserver, + tracer: tracer, + inst: instruments, + spanNameFormatter: cfg.connectSpanNameFormatter, } return cluster.CreateSession() } diff --git a/instrumentation/github.com/gocql/gocql/otelgocql/observer.go b/instrumentation/github.com/gocql/gocql/otelgocql/observer.go index 872205c73aa..e41f898bd23 100644 --- a/instrumentation/github.com/gocql/gocql/otelgocql/observer.go +++ b/instrumentation/github.com/gocql/gocql/otelgocql/observer.go @@ -28,29 +28,32 @@ import ( // OTelQueryObserver implements the gocql.QueryObserver interface // to provide instrumentation to gocql queries. type OTelQueryObserver struct { - enabled bool - observer gocql.QueryObserver - tracer trace.Tracer - inst *instruments + enabled bool + observer gocql.QueryObserver + tracer trace.Tracer + inst *instruments + spanNameFormatter func(gocql.ObservedQuery) string } // OTelBatchObserver implements the gocql.BatchObserver interface // to provide instrumentation to gocql batch queries. type OTelBatchObserver struct { - enabled bool - observer gocql.BatchObserver - tracer trace.Tracer - inst *instruments + enabled bool + observer gocql.BatchObserver + tracer trace.Tracer + inst *instruments + spanNameFormatter func(gocql.ObservedBatch) string } // OTelConnectObserver implements the gocql.ConnectObserver interface // to provide instrumentation to connection attempts made by the session. type OTelConnectObserver struct { - ctx context.Context - enabled bool - observer gocql.ConnectObserver - tracer trace.Tracer - inst *instruments + ctx context.Context + enabled bool + observer gocql.ConnectObserver + tracer trace.Tracer + inst *instruments + spanNameFormatter func(gocql.ObservedConnect) string } // ------------------------------------------ Observer Functions @@ -71,7 +74,7 @@ func (o *OTelQueryObserver) ObserveQuery(ctx context.Context, observedQuery gocq ctx, span := o.tracer.Start( ctx, - observedQuery.Statement, + o.spanNameFormatter(observedQuery), trace.WithTimestamp(observedQuery.Start), trace.WithAttributes(attributes...), trace.WithSpanKind(trace.SpanKindClient), @@ -133,7 +136,7 @@ func (o *OTelBatchObserver) ObserveBatch(ctx context.Context, observedBatch gocq ctx, span := o.tracer.Start( ctx, - internal.CassBatchQueryName, + o.spanNameFormatter(observedBatch), trace.WithTimestamp(observedBatch.Start), trace.WithAttributes(attributes...), trace.WithSpanKind(trace.SpanKindClient), @@ -181,7 +184,7 @@ func (o *OTelConnectObserver) ObserveConnect(observedConnect gocql.ObservedConne _, span := o.tracer.Start( o.ctx, - internal.CassConnectName, + o.spanNameFormatter(observedConnect), trace.WithTimestamp(observedConnect.Start), trace.WithAttributes(attributes...), trace.WithSpanKind(trace.SpanKindClient),