Skip to content

Commit

Permalink
Change SpanNameFormatter from a function to an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM committed Dec 31, 2020
1 parent c746714 commit 1662cd7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
12 changes: 8 additions & 4 deletions instrumentation/database/sql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ const (
instrumentationName = "go.opentelemetry.io/contrib/instrumentation/database/sql"
)

// SpanNameFormatter is a function type that used to format span names.
type SpanNameFormatter func(ctx context.Context, method Method, query string) string
// SpanNameFormatter is an interface that used to format span names.
type SpanNameFormatter interface {
Format(ctx context.Context, method Method, query string) string
}

type config struct {
TracerProvider trace.TracerProvider
Expand Down Expand Up @@ -58,7 +60,9 @@ type SpanFlags struct {
Ping bool
}

func defaultSpanNameFormatter(ctx context.Context, method Method, query string) string {
type defaultSpanNameFormatter struct{}

func (f *defaultSpanNameFormatter) Format(ctx context.Context, method Method, query string) string {
return string(method)
}

Expand All @@ -67,7 +71,7 @@ func newConfig(dbSystem string, options ...Option) config {
cfg := config{
TracerProvider: otel.GetTracerProvider(),
DBSystem: dbSystem,
SpanNameFormatter: defaultSpanNameFormatter,
SpanNameFormatter: &defaultSpanNameFormatter{},
}
for _, opt := range options {
opt.Apply(&cfg)
Expand Down
12 changes: 6 additions & 6 deletions instrumentation/database/sql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *otConn) Ping(ctx context.Context) (err error) {

if c.otDriver.cfg.SpanFlags.Ping {
var span trace.Span
ctx, span = c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnPing, ""),
ctx, span = c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnPing, ""),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(c.cfg.Attributes...),
)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (c *otConn) ExecContext(ctx context.Context, query string, args []driver.Na
return nil, driver.ErrSkip
}

ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnExec, query),
ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnExec, query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
append(c.cfg.Attributes,
Expand Down Expand Up @@ -119,7 +119,7 @@ func (c *otConn) QueryContext(ctx context.Context, query string, args []driver.N
return nil, driver.ErrSkip
}

ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnQuery, query),
ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnQuery, query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
append(c.cfg.Attributes,
Expand All @@ -143,7 +143,7 @@ func (c *otConn) PrepareContext(ctx context.Context, query string) (stmt driver.
return nil, driver.ErrSkip
}

ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnPrepare, query),
ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnPrepare, query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
append(c.cfg.Attributes,
Expand All @@ -170,7 +170,7 @@ func (c *otConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
return nil, driver.ErrSkip
}

ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnBeginTx, ""),
ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnBeginTx, ""),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(c.cfg.Attributes...),
)
Expand All @@ -194,7 +194,7 @@ func (c *otConn) ResetSession(ctx context.Context) (err error) {
return driver.ErrSkip
}

ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter(ctx, MethodConnResetSession, ""),
ctx, span := c.cfg.Tracer.Start(ctx, c.cfg.SpanNameFormatter.Format(ctx, MethodConnResetSession, ""),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(c.cfg.Attributes...),
)
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/database/sql/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func WithAttributes(attributes ...label.KeyValue) Option {
})
}

// WithSpanNameFormatter takes a function that will be called on every
// operation and the returned string will become the Span Name.
// WithSpanNameFormatter takes an interface that will be called on every
// operation and the returned string will become the span name.
func WithSpanNameFormatter(spanNameFormatter SpanNameFormatter) Option {
return OptionFunc(func(cfg *config) {
cfg.SpanNameFormatter = spanNameFormatter
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/database/sql/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *otStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res
return nil, driver.ErrSkip
}

ctx, span := s.cfg.Tracer.Start(ctx, s.cfg.SpanNameFormatter(ctx, MethodStmtExec, s.query),
ctx, span := s.cfg.Tracer.Start(ctx, s.cfg.SpanNameFormatter.Format(ctx, MethodStmtExec, s.query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
append(s.cfg.Attributes,
Expand All @@ -77,7 +77,7 @@ func (s *otStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (ro
return nil, driver.ErrSkip
}

ctx, span := s.cfg.Tracer.Start(ctx, s.cfg.SpanNameFormatter(ctx, MethodStmtQuery, s.query),
ctx, span := s.cfg.Tracer.Start(ctx, s.cfg.SpanNameFormatter.Format(ctx, MethodStmtQuery, s.query),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
append(s.cfg.Attributes,
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/database/sql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newTx(ctx context.Context, tx driver.Tx, cfg config) *otTx {
}

func (t *otTx) Commit() (err error) {
_, span := t.cfg.Tracer.Start(t.ctx, t.cfg.SpanNameFormatter(t.ctx, MethodTxCommit, ""),
_, span := t.cfg.Tracer.Start(t.ctx, t.cfg.SpanNameFormatter.Format(t.ctx, MethodTxCommit, ""),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.cfg.Attributes...),
)
Expand All @@ -54,7 +54,7 @@ func (t *otTx) Commit() (err error) {
}

func (t *otTx) Rollback() (err error) {
_, span := t.cfg.Tracer.Start(t.ctx, t.cfg.SpanNameFormatter(t.ctx, MethodTxRollback, ""),
_, span := t.cfg.Tracer.Start(t.ctx, t.cfg.SpanNameFormatter.Format(t.ctx, MethodTxRollback, ""),
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.cfg.Attributes...),
)
Expand Down

0 comments on commit 1662cd7

Please sign in to comment.