-
Notifications
You must be signed in to change notification settings - Fork 44
/
conn.go
executable file
·232 lines (192 loc) · 5.51 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package instrumentedsql
import (
"context"
"database/sql/driver"
"time"
)
type wrappedConn struct {
opts
parent driver.Conn
}
// Compile time validation that our types implement the expected interfaces
var (
_ driver.Conn = wrappedConn{}
_ driver.ConnBeginTx = wrappedConn{}
_ driver.ConnPrepareContext = wrappedConn{}
_ driver.Execer = wrappedConn{}
_ driver.ExecerContext = wrappedConn{}
_ driver.Pinger = wrappedConn{}
_ driver.Queryer = wrappedConn{}
_ driver.QueryerContext = wrappedConn{}
)
func (c wrappedConn) Prepare(query string) (driver.Stmt, error) {
parent, err := c.parent.Prepare(query)
if err != nil {
return nil, err
}
return wrappedStmt{opts: c.opts, query: query, parent: parent}, nil
}
func (c wrappedConn) Close() error {
return c.parent.Close()
}
func (c wrappedConn) Begin() (driver.Tx, error) {
tx, err := c.parent.Begin()
if err != nil {
return nil, err
}
return wrappedTx{opts: c.opts, parent: tx}, nil
}
func (c wrappedConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
if !c.hasOpExcluded(OpSQLTxBegin) {
span := c.GetSpan(ctx).NewChild(OpSQLTxBegin)
span.SetLabel("component", "database/sql")
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
c.Log(ctx, OpSQLTxBegin, "err", err, "duration", time.Since(start))
}()
}
if connBeginTx, ok := c.parent.(driver.ConnBeginTx); ok {
tx, err = connBeginTx.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return wrappedTx{opts: c.opts, ctx: ctx, parent: tx}, nil
}
tx, err = c.parent.Begin()
if err != nil {
return nil, err
}
return wrappedTx{opts: c.opts, ctx: ctx, parent: tx}, nil
}
func (c wrappedConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
if !c.hasOpExcluded(OpSQLPrepare) {
span := c.GetSpan(ctx).NewChild(OpSQLPrepare)
span.SetLabel("component", "database/sql")
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(ctx, c.opts, OpSQLPrepare, query, err, nil, start)
}()
}
if connPrepareCtx, ok := c.parent.(driver.ConnPrepareContext); ok {
stmt, err := connPrepareCtx.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return wrappedStmt{opts: c.opts, ctx: ctx, query: query, parent: stmt}, nil
}
return c.Prepare(query)
}
func (c wrappedConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if execer, ok := c.parent.(driver.Execer); ok {
res, err := execer.Exec(query, args)
if err != nil {
return nil, err
}
return wrappedResult{opts: c.opts, parent: res}, nil
}
return nil, driver.ErrSkip
}
func (c wrappedConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
if !c.hasOpExcluded(OpSQLConnExec) {
span := c.GetSpan(ctx).NewChild(OpSQLConnExec)
span.SetLabel("component", "database/sql")
span.SetLabel("query", query)
if !c.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(ctx, c.opts, OpSQLConnExec, query, err, args, start)
}()
}
if execContext, ok := c.parent.(driver.ExecerContext); ok {
res, err := execContext.ExecContext(ctx, query, args)
if err != nil {
return nil, err
}
return wrappedResult{opts: c.opts, ctx: ctx, parent: res}, nil
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return c.Exec(query, dargs)
}
}
func (c wrappedConn) Ping(ctx context.Context) (err error) {
if pinger, ok := c.parent.(driver.Pinger); ok {
if !c.hasOpExcluded(OpSQLPing) {
span := c.GetSpan(ctx).NewChild(OpSQLPing)
span.SetLabel("component", "database/sql")
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
c.Log(ctx, OpSQLPing, "err", err, "duration", time.Since(start))
}()
}
return pinger.Ping(ctx)
}
c.Log(ctx, OpSQLDummyPing, "duration", time.Duration(0))
return nil
}
func (c wrappedConn) Query(query string, args []driver.Value) (driver.Rows, error) {
if queryer, ok := c.parent.(driver.Queryer); ok {
rows, err := queryer.Query(query, args)
if err != nil {
return nil, err
}
return wrappedRows{opts: c.opts, parent: rows}, nil
}
return nil, driver.ErrSkip
}
func (c wrappedConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
// Quick skip path: If the wrapped connection implements neither QueryerContext nor Queryer, we have absolutely nothing to do
_, hasQueryerContext := c.parent.(driver.QueryerContext)
_, hasQueryer := c.parent.(driver.Queryer)
if !hasQueryerContext && !hasQueryer {
return nil, driver.ErrSkip
}
if !c.hasOpExcluded(OpSQLConnQuery) {
span := c.GetSpan(ctx).NewChild(OpSQLConnQuery)
span.SetLabel("component", "database/sql")
span.SetLabel("query", query)
if !c.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(ctx, c.opts, OpSQLConnQuery, query, err, args, start)
}()
}
if queryerContext, ok := c.parent.(driver.QueryerContext); ok {
rows, err := queryerContext.QueryContext(ctx, query, args)
if err != nil {
return nil, err
}
return wrappedRows{opts: c.opts, ctx: ctx, parent: rows}, nil
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return c.Query(query, dargs)
}