Skip to content

Commit

Permalink
Combine defers in BeginTx so that span attrs are set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lwc committed Mar 6, 2019
1 parent d9ec83d commit 8e7bfe9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,20 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.

var span *trace.Span
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
defer func() {
if len(attrs) > 0 {
span.AddAttributes(attrs...)
}
}()

if ctx == nil || ctx == context.TODO() {
ctx = context.Background()
_, span = trace.StartSpan(ctx, "sql:begin_transaction", trace.WithSpanKind(trace.SpanKindClient))
attrs = append(attrs, attrMissingContext)
} else {
_, span = trace.StartSpan(ctx, "sql:begin_transaction", trace.WithSpanKind(trace.SpanKindClient))
}
defer span.End()
defer func() {
if len(attrs) > 0 {
span.AddAttributes(attrs...)
}
span.End()
}()

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

0 comments on commit 8e7bfe9

Please sign in to comment.