Skip to content

Commit

Permalink
Move config to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Sep 3, 2021
1 parent 9bab4ee commit a69a5e5
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 142 deletions.
82 changes: 0 additions & 82 deletions instrumentation/database/sql/splunksql/config.go

This file was deleted.

17 changes: 9 additions & 8 deletions instrumentation/database/sql/splunksql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"errors"

"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/config"
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/moniker"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
Expand All @@ -13,7 +14,7 @@ import (
type otelConn struct {
driver.Conn

config config
config config.Config
}

// Compile-time check otelConn implements database interfaces.
Expand All @@ -29,7 +30,7 @@ var (
_ driver.SessionResetter = (*otelConn)(nil)
)

func newConn(conn driver.Conn, conf config) *otelConn {
func newConn(conn driver.Conn, conf config.Config) *otelConn {
return &otelConn{Conn: conn, config: conf}
}

Expand All @@ -39,7 +40,7 @@ func (c *otelConn) Ping(ctx context.Context) error {
if !ok {
return driver.ErrSkip
}
return c.config.withClientSpan(ctx, moniker.Ping, pinger.Ping)
return c.config.WithSpan(ctx, moniker.Ping, pinger.Ping)
}

// Exec calls the wrapped Connection Exec method if implemented.
Expand Down Expand Up @@ -77,7 +78,7 @@ func (c *otelConn) ExecContext(ctx context.Context, query string, args []driver.
}
}

err := c.config.withClientSpan(ctx, moniker.Exec, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
err := c.config.WithSpan(ctx, moniker.Exec, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
return res, err
}

Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *otelConn) QueryContext(ctx context.Context, query string, args []driver
}
}

err := c.config.withClientSpan(ctx, moniker.Query, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
err := c.config.WithSpan(ctx, moniker.Query, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func (c *otelConn) PrepareContext(ctx context.Context, query string) (driver.Stm
}
}

err := c.config.withClientSpan(ctx, moniker.Prepare, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
err := c.config.WithSpan(ctx, moniker.Prepare, f, trace.WithAttributes(semconv.DBStatementKey.String(query)))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +173,7 @@ func (c *otelConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.T
}
}

err := c.config.withClientSpan(ctx, moniker.Begin, f)
err := c.config.WithSpan(ctx, moniker.Begin, f)
if err != nil {
return nil, err
}
Expand All @@ -186,7 +187,7 @@ func (c *otelConn) ResetSession(ctx context.Context) error {
return driver.ErrSkip
}

return c.config.withClientSpan(ctx, moniker.Reset, resetter.ResetSession)
return c.config.WithSpan(ctx, moniker.Reset, resetter.ResetSession)
}

// copied from stdlib database/sql package: src/database/sql/ctxutil.go
Expand Down
6 changes: 4 additions & 2 deletions instrumentation/database/sql/splunksql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package splunksql

import (
"database/sql/driver"

"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/config"
)

// otelDriver wraps a SQL Driver and traces all operations it performs.
type otelDriver struct {
driver driver.Driver
config config
config config.Config
}

// Compile-time check *otelDriver implements database interfaces.
Expand All @@ -16,7 +18,7 @@ var (
_ driver.DriverContext = (*otelDriver)(nil)
)

func newDriver(d driver.Driver, c config) driver.Driver {
func newDriver(d driver.Driver, c config.Config) driver.Driver {
if _, ok := d.(driver.DriverContext); ok {
return &otelDriver{driver: d, config: c}
}
Expand Down
78 changes: 78 additions & 0 deletions instrumentation/database/sql/splunksql/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config

import (
"context"
"database/sql/driver"

splunkotel "github.com/signalfx/splunk-otel-go"
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/moniker"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

// InstrumentationName is the instrumentation library identifier for a Tracer.
const InstrumentationName = "github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"

// Config contains configuration options.
type Config struct {
TracerProvider trace.TracerProvider
}

// NewConfig returns a new Config with default values.
func NewConfig() Config {
return Config{TracerProvider: otel.GetTracerProvider()}
}

// Tracer returns an OTel Tracer from the appropriate TracerProvider.
//
// If the passed context contains a span, the TracerProvider that created the
// Tracer that created that span will be used. Otherwise, the TracerProvider
// from c is used.
func (c Config) Tracer(ctx context.Context) trace.Tracer {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
return span.TracerProvider().Tracer(
InstrumentationName,
trace.WithInstrumentationVersion(splunkotel.Version()),
)
}
return c.TracerProvider.Tracer(
InstrumentationName,
trace.WithInstrumentationVersion(splunkotel.Version()),
)
}

// WithSpan wraps the function f with a span.
func (c Config) WithSpan(ctx context.Context, name moniker.Span, f func(context.Context) error, opts ...trace.SpanStartOption) error {
// From the specification: span kind MUST always be CLIENT.
opts = append(opts, trace.WithSpanKind(trace.SpanKindClient))

var (
err error
span trace.Span
)
ctx, span = c.Tracer(ctx).Start(ctx, name.String(), opts...)
defer func() {
handleErr(span, err)
span.End()
}()

err = f(ctx)
return err
}

func handleErr(span trace.Span, err error) {
if span == nil {
return
}

switch err {
case nil:
// Everything Okay.
case driver.ErrSkip:
// Expected if method not implemented, do not record these.
default:
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package splunksql
package config

import (
"context"
Expand Down Expand Up @@ -27,24 +27,24 @@ func (fn *fnTracer) Start(ctx context.Context, name string, opts ...trace.SpanSt
}

func TestConfigDefaultTracerProvider(t *testing.T) {
c := newConfig()
c := NewConfig()
assert.Equal(t, otel.GetTracerProvider(), c.TracerProvider)
}

func TestWithTracerProvider(t *testing.T) {
// Default is to use the global TracerProvider. This will override that.
tp := new(fnTracerProvider)
c := newConfig(WithTracerProvider(tp))
c := Config{TracerProvider: tp}
assert.Same(t, tp, c.TracerProvider)
}

func TestConfigTracerFromGlobal(t *testing.T) {
c := newConfig()
c := NewConfig()
expected := otel.Tracer(
instrumentationName,
InstrumentationName,
trace.WithInstrumentationVersion(splunkotel.Version()),
)
got := c.tracer(context.Background())
got := c.Tracer(context.Background())
assert.Equal(t, expected, got)
}

Expand All @@ -54,12 +54,12 @@ func TestConfigTracerFromConfig(t *testing.T) {
return &fnTracer{}
},
}
c := newConfig(WithTracerProvider(tp))
c := Config{TracerProvider: tp}
expected := tp.Tracer(
instrumentationName,
InstrumentationName,
trace.WithInstrumentationVersion(splunkotel.Version()),
)
got := c.tracer(context.Background())
got := c.Tracer(context.Background())
assert.Equal(t, expected, got)
}

Expand All @@ -72,10 +72,10 @@ func TestConfigTracerFromContext(t *testing.T) {
ctx := trace.ContextWithSpanContext(context.Background(), sc)
// Use the global TracerProvider in the config and override with the
// passed context to the tracer method.
c := newConfig()
got := c.tracer(ctx)
c := NewConfig()
got := c.Tracer(ctx)
expected := trace.NewNoopTracerProvider().Tracer(
instrumentationName,
InstrumentationName,
trace.WithInstrumentationVersion(splunkotel.Version()),
)
assert.Equal(t, expected, got)
Expand Down
30 changes: 30 additions & 0 deletions instrumentation/database/sql/splunksql/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package splunksql

import (
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql/internal/config"
"go.opentelemetry.io/otel/trace"
)

func newConfig(options ...Option) config.Config {
c := config.NewConfig()
for _, o := range options {
o.apply(&c)
}
return c
}

type Option interface {
apply(*config.Config)
}

type tracerProviderOption struct {
tp trace.TracerProvider
}

func (o tracerProviderOption) apply(c *config.Config) {
c.TracerProvider = o.tp
}

func WithTracerProvider(tp trace.TracerProvider) Option {
return tracerProviderOption{tp: tp}
}
Loading

0 comments on commit a69a5e5

Please sign in to comment.