diff --git a/contrib/database/sql/conn.go b/contrib/database/sql/conn.go index 7dae0c20c7..d09469d5ab 100644 --- a/contrib/database/sql/conn.go +++ b/contrib/database/sql/conn.go @@ -66,7 +66,7 @@ func (tc *tracedConn) PrepareContext(ctx context.Context, query string) (stmt dr sqlCommentCarrier := tracer.SQLCommentCarrier{DiscardDynamicTags: true} span := tc.tryStartTrace(ctx, queryTypePrepare, query, start, &sqlCommentCarrier, err) if span != nil { - go func() { + defer func() { span.Finish(tracer.WithError(err)) }() } @@ -80,7 +80,7 @@ func (tc *tracedConn) PrepareContext(ctx context.Context, query string) (stmt dr sqlCommentCarrier := tracer.SQLCommentCarrier{DiscardDynamicTags: true} span := tc.tryStartTrace(ctx, queryTypePrepare, query, start, &sqlCommentCarrier, err) if span != nil { - go func() { + defer func() { span.Finish(tracer.WithError(err)) }() } @@ -148,7 +148,7 @@ func (tc *tracedConn) QueryContext(ctx context.Context, query string, args []dri sqlCommentCarrier := tracer.SQLCommentCarrier{} span := tc.tryStartTrace(ctx, queryTypeQuery, query, start, &sqlCommentCarrier, err) if span != nil { - go func() { + defer func() { span.Finish(tracer.WithError(err)) }() } @@ -169,7 +169,7 @@ func (tc *tracedConn) QueryContext(ctx context.Context, query string, args []dri sqlCommentCarrier := tracer.SQLCommentCarrier{} span := tc.tryStartTrace(ctx, queryTypeQuery, query, start, &sqlCommentCarrier, err) if span != nil { - go func() { + defer func() { span.Finish(tracer.WithError(err)) }() } diff --git a/contrib/database/sql/stmt.go b/contrib/database/sql/stmt.go index 9684022b8a..4bfa9b35f1 100644 --- a/contrib/database/sql/stmt.go +++ b/contrib/database/sql/stmt.go @@ -9,6 +9,7 @@ import ( "context" "database/sql/driver" "errors" + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" "time" ) @@ -25,9 +26,13 @@ type tracedStmt struct { // Close sends a span before closing a statement func (s *tracedStmt) Close() (err error) { start := time.Now() + span := s.tryStartTrace(s.ctx, queryTypeClose, "", start, nil, err) + if span != nil { + defer func() { + span.Finish(tracer.WithError(err)) + }() + } err = s.Stmt.Close() - s.tryTrace(s.ctx, queryTypeClose, "", start, err) - return err } diff --git a/ddtrace/mocktracer/mockspan.go b/ddtrace/mocktracer/mockspan.go index 7d95264403..97ac7db4e3 100644 --- a/ddtrace/mocktracer/mockspan.go +++ b/ddtrace/mocktracer/mockspan.go @@ -216,7 +216,6 @@ func (s *mockspan) Finish(opts ...ddtrace.FinishOption) { s.finished = true s.finishTime = t s.tracer.addFinishedSpan(s) - //fmt.Printf("Finished span with type %v\n", s.Tag("sql.query_type")) } // String implements fmt.Stringer.