Skip to content

Commit 41ee6eb

Browse files
authored
Merge pull request #37 from a8m/master
all: add DisableErrSkip option to span status
2 parents 88d7880 + 22b7580 commit 41ee6eb

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

driver.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (c ocConn) Exec(query string, args []driver.Value) (res driver.Result, err
177177
span.AddAttributes(attrs...)
178178

179179
defer func() {
180-
setSpanStatus(span, err)
180+
setSpanStatus(span, c.options, err)
181181
span.End()
182182
}()
183183

@@ -216,7 +216,7 @@ func (c ocConn) ExecContext(ctx context.Context, query string, args []driver.Nam
216216
span.AddAttributes(attrs...)
217217

218218
defer func() {
219-
setSpanStatus(span, err)
219+
setSpanStatus(span, c.options, err)
220220
span.End()
221221
}()
222222

@@ -257,7 +257,7 @@ func (c ocConn) Query(query string, args []driver.Value) (rows driver.Rows, err
257257
span.AddAttributes(attrs...)
258258

259259
defer func() {
260-
setSpanStatus(span, err)
260+
setSpanStatus(span, c.options, err)
261261
span.End()
262262
}()
263263

@@ -297,7 +297,7 @@ func (c ocConn) QueryContext(ctx context.Context, query string, args []driver.Na
297297
span.AddAttributes(attrs...)
298298

299299
defer func() {
300-
setSpanStatus(span, err)
300+
setSpanStatus(span, c.options, err)
301301
span.End()
302302
}()
303303

@@ -326,7 +326,7 @@ func (c ocConn) Prepare(query string) (stmt driver.Stmt, err error) {
326326
span.AddAttributes(attrs...)
327327

328328
defer func() {
329-
setSpanStatus(span, err)
329+
setSpanStatus(span, c.options, err)
330330
span.End()
331331
}()
332332
}
@@ -359,7 +359,7 @@ func (c *ocConn) PrepareContext(ctx context.Context, query string) (stmt driver.
359359
attrs = append(attrs, trace.StringAttribute("sql.query", query))
360360
}
361361
defer func() {
362-
setSpanStatus(span, err)
362+
setSpanStatus(span, c.options, err)
363363
span.End()
364364
}()
365365
}
@@ -410,7 +410,7 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
410410

411411
if connBeginTx, ok := c.parent.(driver.ConnBeginTx); ok {
412412
tx, err = connBeginTx.BeginTx(ctx, opts)
413-
setSpanStatus(span, err)
413+
setSpanStatus(span, c.options, err)
414414
if err != nil {
415415
return nil, err
416416
}
@@ -425,7 +425,7 @@ func (c *ocConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.
425425
),
426426
)
427427
tx, err = c.parent.Begin()
428-
setSpanStatus(span, err)
428+
setSpanStatus(span, c.options, err)
429429
if err != nil {
430430
return nil, err
431431
}
@@ -446,7 +446,7 @@ func (r ocResult) LastInsertId() (id int64, err error) {
446446
span.AddAttributes(r.options.DefaultAttributes...)
447447
}
448448
defer func() {
449-
setSpanStatus(span, err)
449+
setSpanStatus(span, r.options, err)
450450
span.End()
451451
}()
452452
}
@@ -462,7 +462,7 @@ func (r ocResult) RowsAffected() (cnt int64, err error) {
462462
span.AddAttributes(r.options.DefaultAttributes...)
463463
}
464464
defer func() {
465-
setSpanStatus(span, err)
465+
setSpanStatus(span, r.options, err)
466466
span.End()
467467
}()
468468
}
@@ -504,7 +504,7 @@ func (s ocStmt) Exec(args []driver.Value) (res driver.Result, err error) {
504504
span.AddAttributes(attrs...)
505505

506506
defer func() {
507-
setSpanStatus(span, err)
507+
setSpanStatus(span, s.options, err)
508508
span.End()
509509
}()
510510

@@ -551,7 +551,7 @@ func (s ocStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
551551
span.AddAttributes(attrs...)
552552

553553
defer func() {
554-
setSpanStatus(span, err)
554+
setSpanStatus(span, s.options, err)
555555
span.End()
556556
}()
557557

@@ -588,7 +588,7 @@ func (s ocStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res
588588
span.AddAttributes(attrs...)
589589

590590
defer func() {
591-
setSpanStatus(span, err)
591+
setSpanStatus(span, s.options, err)
592592
span.End()
593593
}()
594594

@@ -627,7 +627,7 @@ func (s ocStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (row
627627
span.AddAttributes(attrs...)
628628

629629
defer func() {
630-
setSpanStatus(span, err)
630+
setSpanStatus(span, s.options, err)
631631
span.End()
632632
}()
633633

@@ -735,7 +735,7 @@ func (r ocRows) Close() (err error) {
735735
span.AddAttributes(r.options.DefaultAttributes...)
736736
}
737737
defer func() {
738-
setSpanStatus(span, err)
738+
setSpanStatus(span, r.options, err)
739739
span.End()
740740
}()
741741
}
@@ -753,9 +753,9 @@ func (r ocRows) Next(dest []driver.Value) (err error) {
753753
defer func() {
754754
if err == io.EOF {
755755
// not an error; expected to happen during iteration
756-
setSpanStatus(span, nil)
756+
setSpanStatus(span, r.options, nil)
757757
} else {
758-
setSpanStatus(span, err)
758+
setSpanStatus(span, r.options, err)
759759
}
760760
span.End()
761761
}()
@@ -807,7 +807,7 @@ func (t ocTx) Commit() (err error) {
807807
span.AddAttributes(t.options.DefaultAttributes...)
808808
}
809809
defer func() {
810-
setSpanStatus(span, err)
810+
setSpanStatus(span, t.options, err)
811811
span.End()
812812
}()
813813

@@ -823,7 +823,7 @@ func (t ocTx) Rollback() (err error) {
823823
span.AddAttributes(t.options.DefaultAttributes...)
824824
}
825825
defer func() {
826-
setSpanStatus(span, err)
826+
setSpanStatus(span, t.options, err)
827827
span.End()
828828
}()
829829

@@ -878,13 +878,20 @@ func argToAttr(key string, val interface{}) trace.Attribute {
878878
}
879879
}
880880

881-
func setSpanStatus(span *trace.Span, err error) {
881+
func setSpanStatus(span *trace.Span, opts TraceOptions, err error) {
882882
var status trace.Status
883883
switch err {
884884
case nil:
885885
status.Code = trace.StatusCodeOK
886886
span.SetStatus(status)
887887
return
888+
case driver.ErrSkip:
889+
status.Code = trace.StatusCodeUnimplemented
890+
if opts.DisableErrSkip {
891+
// Suppress driver.ErrSkip since at runtime some drivers might not have
892+
// certain features, and an error would pollute many spans.
893+
status.Code = trace.StatusCodeOK
894+
}
888895
case context.Canceled:
889896
status.Code = trace.StatusCodeCancelled
890897
case context.DeadlineExceeded:

options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type TraceOptions struct {
5050

5151
// DefaultAttributes will be set to each span as default.
5252
DefaultAttributes []trace.Attribute
53+
54+
// DisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
55+
DisableErrSkip bool
5356
}
5457

5558
// WithAllTraceOptions enables all available trace options.
@@ -156,3 +159,10 @@ func WithDefaultAttributes(attrs ...trace.Attribute) TraceOption {
156159
o.DefaultAttributes = attrs
157160
}
158161
}
162+
163+
// WithDisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
164+
func WithDisableErrSkip(b bool) TraceOption {
165+
return func(o *TraceOptions) {
166+
o.DisableErrSkip = b
167+
}
168+
}

0 commit comments

Comments
 (0)