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: 15 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import (
// ErrUnsupported is an error returned when the underlying driver doesn't provide a given function.
var ErrUnsupported = errors.New("operation unsupported by the underlying driver")

// TagQuery is a span tag for SQL queries.
const TagQuery = "query"
// spanTagQuery is a span tag for SQL queries.
var spanTagQuery = "query"

// SetTagQuery sets a span tag for SQL queries.
// Use in init funcs only.
func SetTagQuery(tag string) {
spanTagQuery = tag
}

// SpanNameFunc defines a function which returns a name for the span which is being created on traceable operations.
// Passing span naming function is optional, however it gives the user a way to use a custom naming strategy. To allow
Expand All @@ -23,9 +29,10 @@ type SpanNameFunc func(context.Context) string

// tracer defines a set of instances for collecting spans.
type tracer struct {
t opentracing.Tracer
nameFunc SpanNameFunc
saveQuery bool
t opentracing.Tracer
nameFunc SpanNameFunc
observerFunc func(context.Context, opentracing.Span)
saveQuery bool
}

// newSpan creates a new opentracing.Span instance from the given context.
Expand All @@ -37,6 +44,9 @@ func (t *tracer) newSpan(ctx context.Context) opentracing.Span {
opts = append(opts, opentracing.ChildOf(parent.Context()))
}
span := t.t.StartSpan(name, opts...)
if t.observerFunc != nil {
t.observerFunc(ctx, span)
}
return span
}

Expand Down
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
s := c.tracer.newSpan(ctx)
if c.tracer.saveQuery {
s.SetTag(TagQuery, query)
s.SetTag(spanTagQuery, query)
}
defer s.Finish()
if execerContext, ok := c.conn.(driver.ExecerContext); ok {
Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
s := c.tracer.newSpan(ctx)
if c.tracer.saveQuery {
s.SetTag(TagQuery, query)
s.SetTag(spanTagQuery, query)
}
defer s.Finish()
if queryerContext, ok := c.conn.(driver.QueryerContext); ok {
Expand Down
14 changes: 0 additions & 14 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ func NewTracingDriver(d driver.Driver, t opentracing.Tracer, options ...func(*tr
return td
}

// SpanNameFunction is an option for using a custom span naming function.
func SpanNameFunction(f SpanNameFunc) func(*tracingDriver) {
return func(d *tracingDriver) {
d.tracer.nameFunc = f
}
}

// SaveQuery is an option for saving SQL queries.
func SaveQuery(f SpanNameFunc) func(*tracingDriver) {
return func(d *tracingDriver) {
d.tracer.saveQuery = true
}
}

// Open implements driver.Driver Open.
func (d *tracingDriver) Open(name string) (driver.Conn, error) {
c, err := d.driver.Open(name)
Expand Down
28 changes: 28 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sql

import (
"context"

"github.com/opentracing/opentracing-go"
)

// SpanNameFunction is an option for using a custom span naming function.
func SpanNameFunction(f SpanNameFunc) func(*tracingDriver) {
return func(d *tracingDriver) {
d.tracer.nameFunc = f
}
}

// SaveQuery is an option for saving SQL queries.
func SaveQuery(f SpanNameFunc) func(*tracingDriver) {
return func(d *tracingDriver) {
d.tracer.saveQuery = true
}
}

// WithSpanObserver allows you to modify the span's tags for every span created.
func WithSpanObserver(obsFunc func(context.Context, opentracing.Span)) func(*tracingDriver) {
return func(d *tracingDriver) {
d.tracer.observerFunc = obsFunc
}
}
4 changes: 2 additions & 2 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
func (s *stmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
span := s.tracer.newSpan(ctx)
if s.tracer.saveQuery {
span.SetTag(TagQuery, query)
span.SetTag(spanTagQuery, query)
}
defer span.Finish()
if execerContext, ok := s.stmt.(driver.ExecerContext); ok {
Expand All @@ -52,7 +52,7 @@ func (s *stmt) ExecContext(ctx context.Context, query string, args []driver.Name
func (s *stmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
span := s.tracer.newSpan(ctx)
if s.tracer.saveQuery {
span.SetTag(TagQuery, query)
span.SetTag(spanTagQuery, query)
}
defer span.Finish()
if queryerContext, ok := s.stmt.(driver.QueryerContext); ok {
Expand Down