forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
119 lines (108 loc) · 3.12 KB
/
conn.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package sql // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
import (
"context"
"database/sql/driver"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
var _ driver.Conn = (*tracedConn)(nil)
type tracedConn struct {
driver.Conn
*traceParams
}
func (tc *tracedConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
span := tc.newChildSpanFromContext(ctx, "Begin", "")
defer func() {
span.Finish(tracer.WithError(err))
}()
if connBeginTx, ok := tc.Conn.(driver.ConnBeginTx); ok {
tx, err = connBeginTx.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return &tracedTx{tx, tc.traceParams, ctx}, nil
}
tx, err = tc.Conn.Begin()
if err != nil {
return nil, err
}
return &tracedTx{tx, tc.traceParams, ctx}, nil
}
func (tc *tracedConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
span := tc.newChildSpanFromContext(ctx, "Prepare", query)
defer func() {
span.Finish(tracer.WithError(err))
}()
if connPrepareCtx, ok := tc.Conn.(driver.ConnPrepareContext); ok {
stmt, err := connPrepareCtx.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return &tracedStmt{stmt, tc.traceParams, ctx, query}, nil
}
stmt, err = tc.Prepare(query)
if err != nil {
return nil, err
}
return &tracedStmt{stmt, tc.traceParams, ctx, query}, nil
}
func (tc *tracedConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if execer, ok := tc.Conn.(driver.Execer); ok {
return execer.Exec(query, args)
}
return nil, driver.ErrSkip
}
func (tc *tracedConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
span := tc.newChildSpanFromContext(ctx, "Exec", query)
defer func() {
span.Finish(tracer.WithError(err))
}()
if execContext, ok := tc.Conn.(driver.ExecerContext); ok {
return execContext.ExecContext(ctx, query, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return tc.Exec(query, dargs)
}
// tracedConn has a Ping method in order to implement the pinger interface
func (tc *tracedConn) Ping(ctx context.Context) (err error) {
span := tc.newChildSpanFromContext(ctx, "Ping", "")
defer func() {
span.Finish(tracer.WithError(err))
}()
if pinger, ok := tc.Conn.(driver.Pinger); ok {
return pinger.Ping(ctx)
}
return nil
}
func (tc *tracedConn) Query(query string, args []driver.Value) (driver.Rows, error) {
if queryer, ok := tc.Conn.(driver.Queryer); ok {
return queryer.Query(query, args)
}
return nil, driver.ErrSkip
}
func (tc *tracedConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
span := tc.newChildSpanFromContext(ctx, "Query", query)
defer func() {
span.Finish(tracer.WithError(err))
}()
if queryerContext, ok := tc.Conn.(driver.QueryerContext); ok {
return queryerContext.QueryContext(ctx, query, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return tc.Query(query, dargs)
}