Skip to content

Commit 8e7bfe9

Browse files
committed
Combine defers in BeginTx so that span attrs are set correctly
1 parent d9ec83d commit 8e7bfe9

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

driver.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,20 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
393393

394394
var span *trace.Span
395395
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
396-
defer func() {
397-
if len(attrs) > 0 {
398-
span.AddAttributes(attrs...)
399-
}
400-
}()
396+
401397
if ctx == nil || ctx == context.TODO() {
402398
ctx = context.Background()
403399
_, span = trace.StartSpan(ctx, "sql:begin_transaction", trace.WithSpanKind(trace.SpanKindClient))
404400
attrs = append(attrs, attrMissingContext)
405401
} else {
406402
_, span = trace.StartSpan(ctx, "sql:begin_transaction", trace.WithSpanKind(trace.SpanKindClient))
407403
}
408-
defer span.End()
404+
defer func() {
405+
if len(attrs) > 0 {
406+
span.AddAttributes(attrs...)
407+
}
408+
span.End()
409+
}()
409410

410411
if connBeginTx, ok := c.parent.(driver.ConnBeginTx); ok {
411412
tx, err = connBeginTx.BeginTx(ctx, opts)

0 commit comments

Comments
 (0)