Skip to content

Commit

Permalink
Use span id instead of allowing options
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-normand committed Jun 9, 2022
1 parent c70b163 commit ea84e8c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
25 changes: 13 additions & 12 deletions contrib/database/sql/conn.go
Expand Up @@ -43,14 +43,14 @@ func (tc *tracedConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx dr
start := time.Now()
if connBeginTx, ok := tc.Conn.(driver.ConnBeginTx); ok {
tx, err = connBeginTx.BeginTx(ctx, opts)
tc.tryTrace(ctx, queryTypeBegin, "", start, err)
tc.tryTrace(ctx, queryTypeBegin, "", start, err, 0)
if err != nil {
return nil, err
}
return &tracedTx{tx, tc.traceParams, ctx}, nil
}
tx, err = tc.Conn.Begin()
tc.tryTrace(ctx, queryTypeBegin, "", start, err)
tc.tryTrace(ctx, queryTypeBegin, "", start, err, 0)
if err != nil {
return nil, err
}
Expand All @@ -67,14 +67,14 @@ func (tc *tracedConn) PrepareContext(ctx context.Context, query string) (stmt dr
cquery, spanID := injectComments(ctx, query, mode)
if connPrepareCtx, ok := tc.Conn.(driver.ConnPrepareContext); ok {
stmt, err := connPrepareCtx.PrepareContext(ctx, cquery)
tc.tryTrace(ctx, queryTypePrepare, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypePrepare, query, start, err, spanID)
if err != nil {
return nil, err
}
return &tracedStmt{Stmt: stmt, traceParams: tc.traceParams, ctx: ctx, query: query}, nil
}
stmt, err = tc.Prepare(cquery)
tc.tryTrace(ctx, queryTypePrepare, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypePrepare, query, start, err, spanID)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +87,7 @@ func (tc *tracedConn) ExecContext(ctx context.Context, query string, args []driv
if execContext, ok := tc.Conn.(driver.ExecerContext); ok {
cquery, spanID := injectComments(ctx, query, tc.cfg.commentInjectionMode)
r, err := execContext.ExecContext(ctx, cquery, args)
tc.tryTrace(ctx, queryTypeExec, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypeExec, query, start, err, spanID)
return r, err
}
if execer, ok := tc.Conn.(driver.Execer); ok {
Expand All @@ -102,7 +102,7 @@ func (tc *tracedConn) ExecContext(ctx context.Context, query string, args []driv
}
cquery, spanID := injectComments(ctx, query, tc.cfg.commentInjectionMode)
r, err = execer.Exec(cquery, dargs)
tc.tryTrace(ctx, queryTypeExec, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypeExec, query, start, err, spanID)
return r, err
}
return nil, driver.ErrSkip
Expand All @@ -114,7 +114,7 @@ func (tc *tracedConn) Ping(ctx context.Context) (err error) {
if pinger, ok := tc.Conn.(driver.Pinger); ok {
err = pinger.Ping(ctx)
}
tc.tryTrace(ctx, queryTypePing, "", start, err)
tc.tryTrace(ctx, queryTypePing, "", start, err, 0)
return err
}

Expand All @@ -123,7 +123,7 @@ func (tc *tracedConn) QueryContext(ctx context.Context, query string, args []dri
if queryerContext, ok := tc.Conn.(driver.QueryerContext); ok {
cquery, spanID := injectComments(ctx, query, tc.cfg.commentInjectionMode)
rows, err := queryerContext.QueryContext(ctx, cquery, args)
tc.tryTrace(ctx, queryTypeQuery, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypeQuery, query, start, err, spanID)
return rows, err
}
if queryer, ok := tc.Conn.(driver.Queryer); ok {
Expand All @@ -138,7 +138,7 @@ func (tc *tracedConn) QueryContext(ctx context.Context, query string, args []dri
}
cquery, spanID := injectComments(ctx, query, tc.cfg.commentInjectionMode)
rows, err = queryer.Query(cquery, dargs)
tc.tryTrace(ctx, queryTypeQuery, query, start, err, tracer.WithSpanID(spanID))
tc.tryTrace(ctx, queryTypeQuery, query, start, err, spanID)
return rows, err
}
return nil, driver.ErrSkip
Expand Down Expand Up @@ -207,7 +207,7 @@ func resolveInjectionMode(mode tracer.SQLCommentInjectionMode, discardTracingTag
}

// tryTrace will create a span using the given arguments, but will act as a no-op when err is driver.ErrSkip.
func (tp *traceParams) tryTrace(ctx context.Context, qtype queryType, query string, startTime time.Time, err error, spanOpts ...ddtrace.StartSpanOption) {
func (tp *traceParams) tryTrace(ctx context.Context, qtype queryType, query string, startTime time.Time, err error, spanID uint64) {
if err == driver.ErrSkip {
// Not a user error: driver is telling sql package that an
// optional interface method is not implemented. There is
Expand All @@ -219,11 +219,12 @@ func (tp *traceParams) tryTrace(ctx context.Context, qtype queryType, query stri
return
}
name := fmt.Sprintf("%s.query", tp.driverName)
opts := append(spanOpts,
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(tp.cfg.serviceName),
tracer.SpanType(ext.SpanTypeSQL),
tracer.StartTime(startTime),
)
tracer.WithSpanID(spanID),
}
if !math.IsNaN(tp.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, tp.cfg.analyticsRate))
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/database/sql/sql.go
Expand Up @@ -141,7 +141,7 @@ func (t *tracedConnector) Connect(ctx context.Context) (driver.Conn, error) {
}
start := time.Now()
conn, err := t.connector.Connect(ctx)
tp.tryTrace(ctx, queryTypeConnect, "", start, err)
tp.tryTrace(ctx, queryTypeConnect, "", start, err, 0)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions contrib/database/sql/stmt.go
Expand Up @@ -26,7 +26,7 @@ type tracedStmt struct {
func (s *tracedStmt) Close() (err error) {
start := time.Now()
err = s.Stmt.Close()
s.tryTrace(s.ctx, queryTypeClose, "", start, err)
s.tryTrace(s.ctx, queryTypeClose, "", start, err, 0)
return err
}

Expand All @@ -35,7 +35,7 @@ func (s *tracedStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
start := time.Now()
if stmtExecContext, ok := s.Stmt.(driver.StmtExecContext); ok {
res, err := stmtExecContext.ExecContext(ctx, args)
s.tryTrace(ctx, queryTypeExec, s.query, start, err)
s.tryTrace(ctx, queryTypeExec, s.query, start, err, 0)
return res, err
}
dargs, err := namedValueToValue(args)
Expand All @@ -48,7 +48,7 @@ func (s *tracedStmt) ExecContext(ctx context.Context, args []driver.NamedValue)
default:
}
res, err = s.Exec(dargs)
s.tryTrace(ctx, queryTypeExec, s.query, start, err)
s.tryTrace(ctx, queryTypeExec, s.query, start, err, 0)
return res, err
}

Expand All @@ -57,7 +57,7 @@ func (s *tracedStmt) QueryContext(ctx context.Context, args []driver.NamedValue)
start := time.Now()
if stmtQueryContext, ok := s.Stmt.(driver.StmtQueryContext); ok {
rows, err := stmtQueryContext.QueryContext(ctx, args)
s.tryTrace(ctx, queryTypeQuery, s.query, start, err)
s.tryTrace(ctx, queryTypeQuery, s.query, start, err, 0)
return rows, err
}
dargs, err := namedValueToValue(args)
Expand All @@ -70,7 +70,7 @@ func (s *tracedStmt) QueryContext(ctx context.Context, args []driver.NamedValue)
default:
}
rows, err = s.Query(dargs)
s.tryTrace(ctx, queryTypeQuery, s.query, start, err)
s.tryTrace(ctx, queryTypeQuery, s.query, start, err, 0)
return rows, err
}

Expand Down
4 changes: 2 additions & 2 deletions contrib/database/sql/tx.go
Expand Up @@ -24,14 +24,14 @@ type tracedTx struct {
func (t *tracedTx) Commit() (err error) {
start := time.Now()
err = t.Tx.Commit()
t.tryTrace(t.ctx, queryTypeCommit, "", start, err)
t.tryTrace(t.ctx, queryTypeCommit, "", start, err, 0)
return err
}

// Rollback sends a span if the connection is aborted
func (t *tracedTx) Rollback() (err error) {
start := time.Now()
err = t.Tx.Rollback()
t.tryTrace(t.ctx, queryTypeRollback, "", start, err)
t.tryTrace(t.ctx, queryTypeRollback, "", start, err, 0)
return err
}

0 comments on commit ea84e8c

Please sign in to comment.