Skip to content

Commit e7675b6

Browse files
committed
Ensure thread safety for DefaultAttributes
1 parent 59b77da commit e7675b6

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

driver.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ func (c ocConn) Ping(ctx context.Context) (err error) {
9696
if c.options.Ping && (c.options.AllowRoot || trace.FromContext(ctx) != nil) {
9797
var span *trace.Span
9898
ctx, span = trace.StartSpan(ctx, "sql:ping")
99-
span.AddAttributes(c.options.DefaultAttributes...)
99+
if len(c.options.DefaultAttributes) > 0 {
100+
span.AddAttributes(c.options.DefaultAttributes...)
101+
}
100102
defer func() {
101103
if err != nil {
102104
span.SetStatus(trace.Status{
@@ -123,8 +125,10 @@ func (c ocConn) Exec(query string, args []driver.Value) (res driver.Result, err
123125
}
124126

125127
ctx, span := trace.StartSpan(context.Background(), "sql:exec")
126-
attrs := append(
127-
c.options.DefaultAttributes,
128+
attrs := make([]trace.Attribute, 0, len(c.options.DefaultAttributes)+2)
129+
attrs = append(attrs, c.options.DefaultAttributes...)
130+
attrs = append(
131+
attrs,
128132
attrDeprecated,
129133
trace.StringAttribute(
130134
"ocsql.deprecated", "driver does not support ExecerContext",
@@ -166,7 +170,7 @@ func (c ocConn) ExecContext(ctx context.Context, query string, args []driver.Nam
166170
} else {
167171
_, span = trace.StartSpan(ctx, "sql:exec")
168172
}
169-
attrs := c.options.DefaultAttributes
173+
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
170174
if c.options.Query {
171175
attrs = append(attrs, trace.StringAttribute("sql.query", query))
172176
if c.options.QueryParams {
@@ -197,8 +201,10 @@ func (c ocConn) Query(query string, args []driver.Value) (rows driver.Rows, err
197201
}
198202

199203
ctx, span := trace.StartSpan(context.Background(), "sql:query")
200-
attrs := append(
201-
c.options.DefaultAttributes,
204+
attrs := make([]trace.Attribute, 0, len(c.options.DefaultAttributes)+2)
205+
attrs = append(attrs, c.options.DefaultAttributes...)
206+
attrs = append(
207+
attrs,
202208
attrDeprecated,
203209
trace.StringAttribute(
204210
"ocsql.deprecated", "driver does not support QueryerContext",
@@ -241,7 +247,7 @@ func (c ocConn) QueryContext(ctx context.Context, query string, args []driver.Na
241247
} else {
242248
_, span = trace.StartSpan(ctx, "sql:query")
243249
}
244-
attrs := c.options.DefaultAttributes
250+
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
245251
if c.options.Query {
246252
attrs = append(attrs, trace.StringAttribute("sql.query", query))
247253
if c.options.QueryParams {
@@ -269,7 +275,9 @@ func (c ocConn) QueryContext(ctx context.Context, query string, args []driver.Na
269275
func (c ocConn) Prepare(query string) (stmt driver.Stmt, err error) {
270276
if c.options.AllowRoot {
271277
_, span := trace.StartSpan(context.Background(), "sql:prepare")
272-
attrs := append(c.options.DefaultAttributes, attrMissingContext)
278+
attrs := make([]trace.Attribute, 0, len(c.options.DefaultAttributes)+1)
279+
attrs = append(attrs, c.options.DefaultAttributes...)
280+
attrs = append(attrs, attrMissingContext)
273281
if c.options.Query {
274282
attrs = append(attrs, trace.StringAttribute("sql.query", query))
275283
}
@@ -300,7 +308,7 @@ func (c *ocConn) Begin() (driver.Tx, error) {
300308

301309
func (c *ocConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
302310
var span *trace.Span
303-
attrs := c.options.DefaultAttributes
311+
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
304312
if c.options.AllowRoot || trace.FromContext(ctx) != nil {
305313
ctx, span = trace.StartSpan(ctx, "sql:prepare")
306314
if c.options.Query {
@@ -338,7 +346,7 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx,
338346
}
339347

340348
var span *trace.Span
341-
attrs := c.options.DefaultAttributes
349+
attrs := append([]trace.Attribute(nil), c.options.DefaultAttributes...)
342350
defer span.AddAttributes(attrs...)
343351
if ctx == nil || ctx == context.TODO() {
344352
ctx = context.Background()
@@ -383,7 +391,9 @@ type ocResult struct {
383391
func (r ocResult) LastInsertId() (id int64, err error) {
384392
if r.options.LastInsertID {
385393
_, span := trace.StartSpan(r.ctx, "sql:last_insert_id")
386-
span.AddAttributes(r.options.DefaultAttributes...)
394+
if len(r.options.DefaultAttributes) > 0 {
395+
span.AddAttributes(r.options.DefaultAttributes...)
396+
}
387397
defer func() {
388398
setSpanStatus(span, err)
389399
span.End()
@@ -397,7 +407,9 @@ func (r ocResult) LastInsertId() (id int64, err error) {
397407
func (r ocResult) RowsAffected() (cnt int64, err error) {
398408
if r.options.RowsAffected {
399409
_, span := trace.StartSpan(r.ctx, "sql:rows_affected")
400-
span.AddAttributes(r.options.DefaultAttributes...)
410+
if len(r.options.DefaultAttributes) > 0 {
411+
span.AddAttributes(r.options.DefaultAttributes...)
412+
}
401413
defer func() {
402414
setSpanStatus(span, err)
403415
span.End()
@@ -421,8 +433,10 @@ func (s ocStmt) Exec(args []driver.Value) (res driver.Result, err error) {
421433
}
422434

423435
ctx, span := trace.StartSpan(context.Background(), "sql:exec")
424-
attrs := append(
425-
s.options.DefaultAttributes,
436+
attrs := make([]trace.Attribute, 0, len(s.options.DefaultAttributes)+2)
437+
attrs = append(attrs, s.options.DefaultAttributes...)
438+
attrs = append(
439+
attrs,
426440
attrDeprecated,
427441
trace.StringAttribute(
428442
"ocsql.deprecated", "driver does not support StmtExecContext",
@@ -464,8 +478,10 @@ func (s ocStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
464478
}
465479

466480
ctx, span := trace.StartSpan(context.Background(), "sql:query")
467-
attrs := append(
468-
s.options.DefaultAttributes,
481+
attrs := make([]trace.Attribute, 0, len(s.options.DefaultAttributes)+2)
482+
attrs = append(attrs, s.options.DefaultAttributes...)
483+
attrs = append(
484+
attrs,
469485
attrDeprecated,
470486
trace.StringAttribute(
471487
"ocsql.deprecated", "driver does not support StmtQueryContext",
@@ -505,7 +521,7 @@ func (s ocStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res
505521
} else {
506522
_, span = trace.StartSpan(ctx, "sql:exec")
507523
}
508-
attrs := s.options.DefaultAttributes
524+
attrs := append([]trace.Attribute(nil), s.options.DefaultAttributes...)
509525
if s.options.Query {
510526
attrs = append(attrs, trace.StringAttribute("sql.query", s.query))
511527
if s.options.QueryParams {
@@ -542,7 +558,7 @@ func (s ocStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (row
542558
} else {
543559
_, span = trace.StartSpan(ctx, "sql:query")
544560
}
545-
attrs := s.options.DefaultAttributes
561+
attrs := append([]trace.Attribute(nil), s.options.DefaultAttributes...)
546562
if s.options.Query {
547563
attrs = append(attrs, trace.StringAttribute("sql.query", s.query))
548564
if s.options.QueryParams {
@@ -580,7 +596,9 @@ func (r ocRows) Columns() []string {
580596
func (r ocRows) Close() (err error) {
581597
if r.options.RowsClose {
582598
_, span := trace.StartSpan(r.ctx, "sql:rows_close")
583-
span.AddAttributes(r.options.DefaultAttributes...)
599+
if len(r.options.DefaultAttributes) > 0 {
600+
span.AddAttributes(r.options.DefaultAttributes...)
601+
}
584602
defer func() {
585603
setSpanStatus(span, err)
586604
span.End()
@@ -594,7 +612,9 @@ func (r ocRows) Close() (err error) {
594612
func (r ocRows) Next(dest []driver.Value) (err error) {
595613
if r.options.RowsNext {
596614
_, span := trace.StartSpan(r.ctx, "sql:rows_next")
597-
span.AddAttributes(r.options.DefaultAttributes...)
615+
if len(r.options.DefaultAttributes) > 0 {
616+
span.AddAttributes(r.options.DefaultAttributes...)
617+
}
598618
defer func() {
599619
if err == io.EOF {
600620
// not an error; expected to happen during iteration
@@ -619,7 +639,9 @@ type ocTx struct {
619639

620640
func (t ocTx) Commit() (err error) {
621641
_, span := trace.StartSpan(t.ctx, "sql:commit")
622-
span.AddAttributes(t.options.DefaultAttributes...)
642+
if len(t.options.DefaultAttributes) > 0 {
643+
span.AddAttributes(t.options.DefaultAttributes...)
644+
}
623645
defer func() {
624646
setSpanStatus(span, err)
625647
span.End()
@@ -631,7 +653,9 @@ func (t ocTx) Commit() (err error) {
631653

632654
func (t ocTx) Rollback() (err error) {
633655
_, span := trace.StartSpan(t.ctx, "sql:rollback")
634-
span.AddAttributes(t.options.DefaultAttributes...)
656+
if len(t.options.DefaultAttributes) > 0 {
657+
span.AddAttributes(t.options.DefaultAttributes...)
658+
}
635659
defer func() {
636660
setSpanStatus(span, err)
637661
span.End()

0 commit comments

Comments
 (0)