-
Notifications
You must be signed in to change notification settings - Fork 27
/
sql.go
468 lines (384 loc) · 11.5 KB
/
sql.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package instrumentedsql
import (
"context"
"database/sql/driver"
"fmt"
"github.com/kr/pretty"
"github.com/pkg/errors"
)
type wrappedDriver struct {
Logger
Tracer
parent driver.Driver
}
type wrappedConn struct {
Logger
Tracer
parent driver.Conn
}
type wrappedTx struct {
Logger
Tracer
ctx context.Context
parent driver.Tx
}
type wrappedStmt struct {
Logger
Tracer
ctx context.Context
query string
parent driver.Stmt
}
type wrappedResult struct {
Logger
Tracer
ctx context.Context
parent driver.Result
}
type wrappedRows struct {
Logger
Tracer
ctx context.Context
parent driver.Rows
}
// WrapDriver will wrap the passed SQL driver and return a new sql driver that uses it and also logs and traces calls using the passed logger and tracer
// The returned driver will still have to be registered with the sql package before it can be used.
//
// Important note: Seeing as the context passed into the various instrumentation calls this package calls,
// Any call without a context passed will not be instrumented. Please be sure to use the ___Context() and BeginTx() function calls added in Go 1.8
// instead of the older calls which do not accept a context.
func WrapDriver(driver driver.Driver, opts ...Opt) driver.Driver {
d := wrappedDriver{parent: driver}
for _, opt := range opts {
opt(&d)
}
if d.Logger == nil {
d.Logger = nullLogger{}
}
if d.Tracer == nil {
d.Tracer = nullTracer{}
}
return d
}
func (d wrappedDriver) Open(name string) (driver.Conn, error) {
conn, err := d.parent.Open(name)
if err != nil {
return nil, err
}
return wrappedConn{Tracer: d.Tracer, Logger: d.Logger, parent: conn}, nil
}
func (c wrappedConn) Prepare(query string) (driver.Stmt, error) {
parent, err := c.parent.Prepare(query)
if err != nil {
return nil, err
}
return wrappedStmt{Tracer: c.Tracer, Logger: c.Logger, 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{Tracer: c.Tracer, Logger: c.Logger, parent: tx}, nil
}
func (c wrappedConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
span := c.GetSpan(ctx).NewChild("sql-tx-begin")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
c.Log(ctx, "sql-tx-begin", "err", err)
}()
if connBeginTx, ok := c.parent.(driver.ConnBeginTx); ok {
tx, err = connBeginTx.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
return wrappedTx{Tracer: c.Tracer, Logger: c.Logger, ctx: ctx, parent: tx}, nil
}
tx, err = c.parent.Begin()
if err != nil {
return nil, err
}
return wrappedTx{Tracer: c.Tracer, Logger: c.Logger, ctx: ctx, parent: tx}, nil
}
func (c wrappedConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
span := c.GetSpan(ctx).NewChild("sql-prepare")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
c.Log(ctx, "sql-prepare", "err", err)
}()
if connPrepareCtx, ok := c.parent.(driver.ConnPrepareContext); ok {
stmt, err := connPrepareCtx.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
return wrappedStmt{Tracer: c.Tracer, Logger: c.Logger, ctx: ctx, 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{Tracer: c.Tracer, Logger: c.Logger, parent: res}, nil
}
return nil, driver.ErrSkip
}
func (c wrappedConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
span := c.GetSpan(ctx).NewChild("sql-conn-exec")
span.SetLabel("component", "database/sql")
span.SetLabel("query", query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
c.Log(ctx, "sql-conn-exec", "query", query, "args", pretty.Sprint(args), "err", err)
}()
if execContext, ok := c.parent.(driver.ExecerContext); ok {
res, err := execContext.ExecContext(ctx, query, args)
if err != nil {
return nil, err
}
return wrappedResult{Tracer: c.Tracer, Logger: c.Logger, ctx: ctx, parent: res}, nil
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return c.Exec(query, dargs)
}
func (c wrappedConn) Ping(ctx context.Context) (err error) {
if pinger, ok := c.parent.(driver.Pinger); ok {
span := c.GetSpan(ctx).NewChild("sql-ping")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
c.Log(ctx, "sql-ping", "err", err)
}()
return pinger.Ping(ctx)
}
c.Log(ctx, "sql-dummy-ping")
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{Tracer: c.Tracer, Logger: c.Logger, parent: rows}, nil
}
return nil, driver.ErrSkip
}
func (c wrappedConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
span := c.GetSpan(ctx).NewChild("sql-conn-query")
span.SetLabel("component", "database/sql")
span.SetLabel("query", query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
c.Log(ctx, "sql-conn-query", "query", query, "args", pretty.Sprint(args), "err", err)
}()
if queryerContext, ok := c.parent.(driver.QueryerContext); ok {
rows, err := queryerContext.QueryContext(ctx, query, args)
if err != nil {
return nil, err
}
return wrappedRows{Tracer: c.Tracer, Logger: c.Logger, 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)
}
func (t wrappedTx) Commit() (err error) {
span := t.GetSpan(t.ctx).NewChild("sql-tx-commit")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
t.Log(t.ctx, "sql-tx-commit", "err", err)
}()
return t.parent.Commit()
}
func (t wrappedTx) Rollback() (err error) {
span := t.GetSpan(t.ctx).NewChild("sql-tx-rollback")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
t.Log(t.ctx, "sql-tx-rollback", "err", err)
}()
return t.parent.Rollback()
}
func (s wrappedStmt) Close() (err error) {
span := s.GetSpan(s.ctx).NewChild("sql-stmt-close")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
s.Log(s.ctx, "sql-stmt-close", "err", err)
}()
return s.parent.Close()
}
func (s wrappedStmt) NumInput() int {
return s.parent.NumInput()
}
func (s wrappedStmt) Exec(args []driver.Value) (res driver.Result, err error) {
span := s.GetSpan(s.ctx).NewChild("sql-stmt-exec")
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
s.Log(s.ctx, "sql-stmt-exec", "query", s.query, "args", pretty.Sprint(args), "err", err)
}()
res, err = s.parent.Exec(args)
if err != nil {
return nil, err
}
return wrappedResult{Tracer: s.Tracer, Logger: s.Logger, ctx: s.ctx, parent: res}, nil
}
func (s wrappedStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
span := s.GetSpan(s.ctx).NewChild("sql-stmt-query")
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
s.Log(s.ctx, "sql-stmt-query", "query", s.query, "args", pretty.Sprint(args), "err", err)
}()
rows, err = s.parent.Query(args)
if err != nil {
return nil, err
}
return wrappedRows{Tracer: s.Tracer, Logger: s.Logger, ctx: s.ctx, parent: rows}, nil
}
func (s wrappedStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
span := s.GetSpan(ctx).NewChild("sql-stmt-exec")
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
s.Log(ctx, "sql-stmt-exec", "query", s.query, "args", pretty.Sprint(args), "err", err)
}()
if stmtExecContext, ok := s.parent.(driver.StmtExecContext); ok {
res, err := stmtExecContext.ExecContext(ctx, args)
if err != nil {
return nil, err
}
return wrappedResult{Tracer: s.Tracer, Logger: s.Logger, ctx: ctx, parent: res}, nil
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return s.Exec(dargs)
}
func (s wrappedStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
span := s.GetSpan(ctx).NewChild("sql-stmt-query")
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
span.SetLabel("args", pretty.Sprint(args))
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
s.Log(ctx, "sql-stmt-query", "query", s.query, "args", pretty.Sprint(args), "err", err)
}()
if stmtQueryContext, ok := s.parent.(driver.StmtQueryContext); ok {
rows, err := stmtQueryContext.QueryContext(ctx, args)
if err != nil {
return nil, err
}
return wrappedRows{Tracer: s.Tracer, Logger: s.Logger, 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 s.Query(dargs)
}
func (r wrappedResult) LastInsertId() (id int64, err error) {
span := r.GetSpan(r.ctx).NewChild("sql-res-lastInsertId")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
r.Log(r.ctx, "sql-res-lastInsertId", "err", err)
}()
return r.parent.LastInsertId()
}
func (r wrappedResult) RowsAffected() (num int64, err error) {
span := r.GetSpan(r.ctx).NewChild("sql-res-rowsAffected")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
r.Log(r.ctx, "sql-res-rowsAffected", "err", err)
}()
return r.parent.RowsAffected()
}
func (r wrappedRows) Columns() []string {
return r.parent.Columns()
}
func (r wrappedRows) Close() error {
return r.parent.Close()
}
func (r wrappedRows) Next(dest []driver.Value) (err error) {
span := r.GetSpan(r.ctx).NewChild("sql-rows-next")
span.SetLabel("component", "database/sql")
defer func() {
span.SetLabel("err", fmt.Sprint(err))
span.Finish()
r.Log(r.ctx, "sql-rows-next", "err", err)
}()
return r.parent.Next(dest)
}
// namedValueToValue is a helper function copied from the database/sql package
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
dargs := make([]driver.Value, len(named))
for n, param := range named {
if len(param.Name) > 0 {
return nil, errors.New("sql: driver does not support the use of Named Parameters")
}
dargs[n] = param.Value
}
return dargs, nil
}