-
Notifications
You must be signed in to change notification settings - Fork 1
/
spans.go
44 lines (34 loc) · 919 Bytes
/
spans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sqltracing
import (
"context"
"errors"
)
// DBStatementTagKey is the name of the tracing that contains db query
// statements.
const DBStatementTagKey = "db.statement"
func (d *Interceptor) startSpan(ctx context.Context, opName SQLOp, query string, whitelistedErr ...error) (func(err error), context.Context) {
if d.opIsExcluded(opName) {
return func(_ error) {}, ctx
}
span, ctx := d.tracer.StartSpan(ctx, opName.String())
if query != "" {
span.SetTag(DBStatementTagKey, query)
}
return spanFinishFunc(span, whitelistedErr...), ctx
}
func spanFinishFunc(span Span, whitelistedErr ...error) func(err error) {
return func(err error) {
if err != nil && !errisOneOf(err, whitelistedErr) {
span.SetError(err)
}
span.Finish()
}
}
func errisOneOf(err error, targets []error) bool {
for _, target := range targets {
if errors.Is(err, target) {
return true
}
}
return false
}