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 b42b04c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 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
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

0 comments on commit b42b04c

Please sign in to comment.