Skip to content

Commit

Permalink
otelgocql: Refactor option type (#859)
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
pellared and MrAlias authored Jul 20, 2021
1 parent 0448364 commit 5352bda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
63 changes: 26 additions & 37 deletions instrumentation/github.com/gocql/gocql/otelgocql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

// TracedSessionConfig provides configuration for sessions
// created with NewSessionWithTracing.
type TracedSessionConfig struct {
type config struct {
tracerProvider trace.TracerProvider
meterProvider metric.MeterProvider
instrumentQuery bool
Expand All @@ -36,96 +34,87 @@ type TracedSessionConfig struct {
connectObserver gocql.ConnectObserver
}

// TracedSessionOption applies a configuration option to
// the given TracedSessionConfig.
type TracedSessionOption interface {
Apply(*TracedSessionConfig)
// Option applies a configuration option.
type Option interface {
apply(*config)
}

// TracedSessionOptionFunc is a function type that applies
// a particular configuration to the traced session in question.
type TracedSessionOptionFunc func(*TracedSessionConfig)
type optionFunc func(*config)

// Apply will apply the TracedSessionOptionFunc to c, the given
// TracedSessionConfig.
func (o TracedSessionOptionFunc) Apply(c *TracedSessionConfig) {
func (o optionFunc) apply(c *config) {
o(c)
}

// ------------------------------------------ TracedSessionOptions

// WithQueryObserver sets an additional QueryObserver to the session configuration. Use this if
// there is an existing QueryObserver that you would like called. It will be called after the
// OpenTelemetry implementation, if it is not nil. Defaults to nil.
func WithQueryObserver(observer gocql.QueryObserver) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithQueryObserver(observer gocql.QueryObserver) Option {
return optionFunc(func(cfg *config) {
cfg.queryObserver = observer
})
}

// WithBatchObserver sets an additional BatchObserver to the session configuration. Use this if
// there is an existing BatchObserver that you would like called. It will be called after the
// OpenTelemetry implementation, if it is not nil. Defaults to nil.
func WithBatchObserver(observer gocql.BatchObserver) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithBatchObserver(observer gocql.BatchObserver) Option {
return optionFunc(func(cfg *config) {
cfg.batchObserver = observer
})
}

// WithConnectObserver sets an additional ConnectObserver to the session configuration. Use this if
// there is an existing ConnectObserver that you would like called. It will be called after the
// OpenTelemetry implementation, if it is not nil. Defaults to nil.
func WithConnectObserver(observer gocql.ConnectObserver) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithConnectObserver(observer gocql.ConnectObserver) Option {
return optionFunc(func(cfg *config) {
cfg.connectObserver = observer
})
}

// WithTracerProvider will set the trace provider used to get a tracer
// for creating spans. Defaults to TracerProvider()
func WithTracerProvider(provider trace.TracerProvider) TracedSessionOption {
return TracedSessionOptionFunc(func(c *TracedSessionConfig) {
func WithTracerProvider(provider trace.TracerProvider) Option {
return optionFunc(func(c *config) {
c.tracerProvider = provider
})
}

// WithMeterProvider will set the meter provider used to get a meter
// for creating instruments.
// Defaults to global.GetMeterProvider().
func WithMeterProvider(provider metric.MeterProvider) TracedSessionOption {
return TracedSessionOptionFunc(func(c *TracedSessionConfig) {
func WithMeterProvider(provider metric.MeterProvider) Option {
return optionFunc(func(c *config) {
c.meterProvider = provider
})
}

// WithQueryInstrumentation will enable and disable instrumentation of
// queries. Defaults to enabled.
func WithQueryInstrumentation(enabled bool) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithQueryInstrumentation(enabled bool) Option {
return optionFunc(func(cfg *config) {
cfg.instrumentQuery = enabled
})
}

// WithBatchInstrumentation will enable and disable insturmentation of
// batch queries. Defaults to enabled.
func WithBatchInstrumentation(enabled bool) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithBatchInstrumentation(enabled bool) Option {
return optionFunc(func(cfg *config) {
cfg.instrumentBatch = enabled
})
}

// WithConnectInstrumentation will enable and disable instrumentation of
// connection attempts. Defaults to enabled.
func WithConnectInstrumentation(enabled bool) TracedSessionOption {
return TracedSessionOptionFunc(func(cfg *TracedSessionConfig) {
func WithConnectInstrumentation(enabled bool) Option {
return optionFunc(func(cfg *config) {
cfg.instrumentConnect = enabled
})
}

// ------------------------------------------ Private Functions

func newTracedSessionConfig(options ...TracedSessionOption) *TracedSessionConfig {
config := &TracedSessionConfig{
func newConfig(options ...Option) *config {
cfg := &config{
tracerProvider: otel.GetTracerProvider(),
meterProvider: global.GetMeterProvider(),
instrumentQuery: true,
Expand All @@ -134,8 +123,8 @@ func newTracedSessionConfig(options ...TracedSessionOption) *TracedSessionConfig
}

for _, apply := range options {
apply.Apply(config)
apply.apply(cfg)
}

return config
return cfg
}
20 changes: 10 additions & 10 deletions instrumentation/github.com/gocql/gocql/otelgocql/gocql.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ import (
// NewSessionWithTracing creates a new session using the given cluster
// configuration enabling tracing for queries, batch queries, and connection attempts.
// You may use additional observers and disable specific tracing using the provided `TracedSessionOption`s.
func NewSessionWithTracing(ctx context.Context, cluster *gocql.ClusterConfig, options ...TracedSessionOption) (*gocql.Session, error) {
config := newTracedSessionConfig(options...)
instruments := newInstruments(config.meterProvider)
tracer := config.tracerProvider.Tracer(
func NewSessionWithTracing(ctx context.Context, cluster *gocql.ClusterConfig, options ...Option) (*gocql.Session, error) {
cfg := newConfig(options...)
instruments := newInstruments(cfg.meterProvider)
tracer := cfg.tracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(otelcontrib.SemVersion()),
)
cluster.QueryObserver = &OTelQueryObserver{
enabled: config.instrumentQuery,
observer: config.queryObserver,
enabled: cfg.instrumentQuery,
observer: cfg.queryObserver,
tracer: tracer,
inst: instruments,
}
cluster.BatchObserver = &OTelBatchObserver{
enabled: config.instrumentBatch,
observer: config.batchObserver,
enabled: cfg.instrumentBatch,
observer: cfg.batchObserver,
tracer: tracer,
inst: instruments,
}
cluster.ConnectObserver = &OTelConnectObserver{
ctx: ctx,
enabled: config.instrumentConnect,
observer: config.connectObserver,
enabled: cfg.instrumentConnect,
observer: cfg.connectObserver,
tracer: tracer,
inst: instruments,
}
Expand Down

0 comments on commit 5352bda

Please sign in to comment.