From d9ec83de8b14d43630fae4f6680d3303958a1aef Mon Sep 17 00:00:00 2001 From: Luke Cawood Date: Wed, 6 Mar 2019 11:51:02 +1100 Subject: [PATCH 1/2] Propagate TraceOptions to the transaction decorator --- driver.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/driver.go b/driver.go index 4018f0c..8fb235c 100644 --- a/driver.go +++ b/driver.go @@ -413,7 +413,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 } attrs = append( @@ -428,7 +428,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 From 8e7bfe9cc9cb78e300caae65630e49f43dc2b65c Mon Sep 17 00:00:00 2001 From: Luke Cawood Date: Wed, 6 Mar 2019 11:55:16 +1100 Subject: [PATCH 2/2] Combine defers in BeginTx so that span attrs are set correctly --- driver.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/driver.go b/driver.go index 8fb235c..ce89c38 100644 --- a/driver.go +++ b/driver.go @@ -393,11 +393,7 @@ 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)) @@ -405,7 +401,12 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver. } 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)