Skip to content

Commit

Permalink
driver: ensure attributes are added on defer before span.End
Browse files Browse the repository at this point in the history
* Fixes adding span attributes before invoking span.End
* Passed in options for each transaction related span
  • Loading branch information
Emmanuel T Odeke authored Mar 6, 2019
2 parents a227edf + 8e7bfe9 commit 88d7880
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,27 +393,28 @@ 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)
setSpanStatus(span, err)
if err != nil {
return nil, err
}
return ocTx{parent: tx, ctx: ctx}, nil
return ocTx{parent: tx, ctx: ctx, options: c.options}, nil
}

attrs = append(
Expand All @@ -428,7 +429,7 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
if err != nil {
return nil, err
}
return ocTx{parent: tx, ctx: ctx}, nil
return ocTx{parent: tx, ctx: ctx, options: c.options}, nil
}

// ocResult implements driver.Result
Expand Down

0 comments on commit 88d7880

Please sign in to comment.