Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add span name formatters to gocql instrumentation #3047

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -117,13 +122,49 @@ 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(),
meterProvider: global.MeterProvider(),
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 {
Expand Down
29 changes: 16 additions & 13 deletions instrumentation/github.com/gocql/gocql/otelgocql/gocql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,7 +74,7 @@ func (o *OTelQueryObserver) ObserveQuery(ctx context.Context, observedQuery gocq

ctx, span := o.tracer.Start(
ctx,
observedQuery.Statement,
o.spanNameFormatter(observedQuery),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be a bit more defensive about these not being nil. If a user does With*SpanNameFormatter(nil) this will panic.

I would suggest adding a nil check either after apply's or before used in creating the Observers.

trace.WithTimestamp(observedQuery.Start),
trace.WithAttributes(attributes...),
trace.WithSpanKind(trace.SpanKindClient),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down