From 1662cd753e466eb657d85e22f525b8582df0131f Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Thu, 31 Dec 2020 09:13:34 +0800 Subject: [PATCH] Change SpanNameFormatter from a function to an interface --- instrumentation/database/sql/config.go | 12 ++++++++---- instrumentation/database/sql/conn.go | 12 ++++++------ instrumentation/database/sql/option.go | 4 ++-- instrumentation/database/sql/stmt.go | 4 ++-- instrumentation/database/sql/tx.go | 4 ++-- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/instrumentation/database/sql/config.go b/instrumentation/database/sql/config.go index 651ae97110e..793fd459911 100644 --- a/instrumentation/database/sql/config.go +++ b/instrumentation/database/sql/config.go @@ -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 @@ -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) } @@ -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) diff --git a/instrumentation/database/sql/conn.go b/instrumentation/database/sql/conn.go index 235edcb7c88..55bfc066027 100644 --- a/instrumentation/database/sql/conn.go +++ b/instrumentation/database/sql/conn.go @@ -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...), ) @@ -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, @@ -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, @@ -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, @@ -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...), ) @@ -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...), ) diff --git a/instrumentation/database/sql/option.go b/instrumentation/database/sql/option.go index 2147e49257c..8a46b0f4390 100644 --- a/instrumentation/database/sql/option.go +++ b/instrumentation/database/sql/option.go @@ -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 diff --git a/instrumentation/database/sql/stmt.go b/instrumentation/database/sql/stmt.go index 027620b70fe..6544c2f5dbc 100644 --- a/instrumentation/database/sql/stmt.go +++ b/instrumentation/database/sql/stmt.go @@ -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, @@ -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, diff --git a/instrumentation/database/sql/tx.go b/instrumentation/database/sql/tx.go index 6ff6b2f66d5..26be3b9f591 100644 --- a/instrumentation/database/sql/tx.go +++ b/instrumentation/database/sql/tx.go @@ -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...), ) @@ -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...), )