forked from j2gg0s/otsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
506 lines (439 loc) · 12.5 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package otsql
import (
"context"
"database/sql/driver"
"io"
"reflect"
"go.opentelemetry.io/otel/label"
)
var (
labelMissingContext = label.String("otsql.warning", "missing upstream context")
labelDeprecated = label.String("otsql.warning", "database driver uses deprecated features")
labelUnknownArgs = label.String("otsql.warning", "unknown args type")
)
// driver.Conn
type otConn struct {
driver.Conn
options TraceOptions
}
func (c otConn) Exec(query string, args []driver.Value) (res driver.Result, err error) {
ctx, span, endTrace := startTrace(context.Background(), c.options, methodExec, query, args)
span.SetAttributes(labelDeprecated, labelMissingContext)
defer func() {
endTrace(ctx, err)
}()
execer, ok := c.Conn.(driver.Execer) // nolint
if !ok {
return nil, driver.ErrSkip
}
if res, err = execer.Exec(query, args); err != nil {
return nil, err
}
return wrapResult(ctx, res, c.options), nil
}
func (c otConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (res driver.Result, err error) {
ctx, _, endTrace := startTrace(ctx, c.options, methodExec, query, args)
defer func() {
endTrace(ctx, err)
}()
execer, ok := c.Conn.(driver.ExecerContext)
if !ok {
return nil, driver.ErrSkip
}
if res, err = execer.ExecContext(ctx, query, args); err != nil {
return nil, err
}
return wrapResult(ctx, res, c.options), nil
}
func (c otConn) Query(query string, args []driver.Value) (rows driver.Rows, err error) {
ctx, span, endTrace := startTrace(context.Background(), c.options, methodQuery, query, args)
span.SetAttributes(labelDeprecated, labelMissingContext)
defer func() {
endTrace(ctx, err)
}()
queryer, ok := c.Conn.(driver.Queryer) // nolint
if !ok {
return nil, driver.ErrSkip
}
if rows, err = queryer.Query(query, args); err != nil {
return nil, err
}
return wrapRows(ctx, rows, c.options), nil
}
func (c otConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
ctx, _, endTrace := startTrace(ctx, c.options, methodQuery, query, args)
defer func() {
endTrace(ctx, err)
}()
queryer, ok := c.Conn.(driver.QueryerContext)
if !ok {
return nil, driver.ErrSkip
}
if rows, err = queryer.QueryContext(ctx, query, args); err != nil {
return nil, err
}
return wrapRows(ctx, rows, c.options), nil
}
func (c otConn) Ping(ctx context.Context) (err error) {
ctx, _, traceFunc := startTrace(ctx, c.options, methodPing, "", nil)
defer func() {
traceFunc(ctx, err)
}()
pinger, ok := c.Conn.(driver.Pinger)
if !ok {
return driver.ErrSkip
}
return pinger.Ping(ctx)
}
func (c otConn) PrepareContext(ctx context.Context, query string) (stmt driver.Stmt, err error) {
ctx, span, endTrace := startTrace(ctx, c.options, methodPrepare, query, nil)
defer func() {
endTrace(ctx, err)
}()
if prepare, ok := c.Conn.(driver.ConnPrepareContext); ok {
if stmt, err = prepare.PrepareContext(ctx, query); err != nil {
return nil, err
}
} else {
span.SetAttributes(labelMissingContext)
if stmt, err = c.Conn.Prepare(query); err != nil {
return nil, err
}
}
return wrapStmt(stmt, query, c.options), nil
}
func (c otConn) Prepare(query string) (stmt driver.Stmt, err error) {
ctx, span, endTrace := startTrace(context.Background(), c.options, methodPrepare, query, nil)
span.SetAttributes(labelMissingContext)
defer func() {
endTrace(ctx, err)
}()
stmt, err = c.Conn.Prepare(query)
if err != nil {
return nil, err
}
return wrapStmt(stmt, query, c.options), nil
}
func (c otConn) Begin() (tx driver.Tx, err error) {
ctx, span, endTrace := startTrace(context.Background(), c.options, methodBegin, "", nil)
defer func() {
endTrace(ctx, err)
}()
span.SetAttributes(labelDeprecated, labelMissingContext)
tx, err = c.Conn.Begin() // nolint
if err != nil {
return nil, err
}
return wrapTx(ctx, tx, c.options), nil
}
func (c otConn) BeginTx(ctx context.Context, opts driver.TxOptions) (tx driver.Tx, err error) {
ctx, span, endTrace := startTrace(ctx, c.options, methodBegin, "", nil)
defer func() {
endTrace(ctx, err)
}()
if beginTx, ok := c.Conn.(driver.ConnBeginTx); ok {
if tx, err = beginTx.BeginTx(ctx, opts); err != nil {
return nil, err
}
} else {
span.SetAttributes(labelDeprecated, labelMissingContext)
if tx, err = c.Conn.Begin(); err != nil { // nolint
return nil, err
}
}
return wrapTx(ctx, tx, c.options), nil
}
func (c otConn) Close() error {
return c.Conn.Close()
}
func wrapConn(conn driver.Conn, options TraceOptions) driver.Conn {
return otConn{
Conn: conn,
options: options,
}
}
// driver.Result
type otResult struct {
driver.Result
ctx context.Context
options TraceOptions
}
func (r otResult) LastInsertId() (id int64, err error) {
if !r.options.LastInsertID {
return r.Result.LastInsertId()
}
ctx, span, endTrace := startTrace(r.ctx, r.options, methodLastInsertID, "", nil)
defer func() {
endTrace(ctx, err)
}()
r.ctx = ctx
id, err = r.Result.LastInsertId()
span.SetAttributes(label.Int64("sql.last_insert_id", id))
return
}
func (r otResult) RowsAffected() (cnt int64, err error) {
if !r.options.RowsAffected {
return r.Result.RowsAffected()
}
ctx, span, endTrace := startTrace(r.ctx, r.options, methodRowsAffected, "", nil)
defer func() {
endTrace(ctx, err)
}()
r.ctx = ctx
cnt, err = r.Result.RowsAffected()
span.SetAttributes(label.Int64("sql.rows_affected", cnt))
return
}
func wrapResult(ctx context.Context, parent driver.Result, options TraceOptions) driver.Result {
return &otResult{
Result: parent,
ctx: ctx,
options: options,
}
}
// withRowsColumnTypeScanType is the same as the driver.RowsColumnTypeScanType
// interface except it omits the driver.Rows embedded interface.
// If the original driver.Rows implementation wrapped by ocsql supports
// RowsColumnTypeScanType we enable the original method implementation in the
// returned driver.Rows from wrapRows by doing a composition with ocRows.
type withRowsColumnTypeScanType interface {
ColumnTypeScanType(index int) reflect.Type
}
// driver.Rows
type otRows struct {
driver.Rows
ctx context.Context
options TraceOptions
}
func (r otRows) Columns() []string {
return r.Rows.Columns()
}
func (r otRows) Close() (err error) {
if !r.options.RowsClose {
return r.Rows.Close()
}
ctx, _, endTrace := startTrace(r.ctx, r.options, methodRowsClose, "", nil)
defer func() {
endTrace(ctx, err)
}()
return r.Rows.Close()
}
func (r otRows) Next(dest []driver.Value) (err error) {
if !r.options.RowsNext {
return r.Rows.Next(dest)
}
ctx, _, endTrace := startTrace(r.ctx, r.options, methodRowsNext, "", nil)
defer func() {
if err == io.EOF {
endTrace(ctx, nil)
} else {
endTrace(ctx, err)
}
}()
err = r.Rows.Next(dest)
return
}
func wrapRows(ctx context.Context, parent driver.Rows, options TraceOptions) driver.Rows {
ts, isColumnTypeScan := parent.(driver.RowsColumnTypeScanType)
r := otRows{
Rows: parent,
ctx: ctx,
options: options,
}
if isColumnTypeScan {
return struct {
otRows
withRowsColumnTypeScanType
}{r, ts}
}
return r
}
type otStmt struct {
driver.Stmt
query string
options TraceOptions
}
func (s otStmt) Exec(args []driver.Value) (res driver.Result, err error) {
ctx, span, endTrace := startTrace(context.Background(), s.options, methodExec, s.query, args)
span.SetAttributes(labelDeprecated, labelMissingContext)
defer func() {
endTrace(ctx, err)
}()
res, err = s.Stmt.Exec(args) // nolint
if err != nil {
return nil, err
}
return wrapResult(ctx, res, s.options), nil
}
func (s otStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
ctx, _, endTrace := startTrace(ctx, s.options, methodExec, s.query, args)
defer func() {
endTrace(ctx, err)
}()
// we already tested driver when wrap stmt
res, err = s.Stmt.(driver.StmtExecContext).ExecContext(ctx, args)
if err != nil {
return nil, err
}
return wrapResult(ctx, res, s.options), nil
}
func (s otStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
ctx, span, endTrace := startTrace(context.Background(), s.options, methodQuery, s.query, args)
span.SetAttributes(labelDeprecated, labelMissingContext)
defer func() {
endTrace(ctx, err)
}()
rows, err = s.Stmt.Query(args) // nolint
if err != nil {
return nil, err
}
return wrapRows(ctx, rows, s.options), nil
}
func (s otStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
ctx, _, endTrace := startTrace(ctx, s.options, methodExec, s.query, args)
defer func() {
endTrace(ctx, err)
}()
// we already tested driver when wrap stmt
rows, err = s.Stmt.(driver.StmtQueryContext).QueryContext(ctx, args)
if err != nil {
return nil, err
}
return wrapRows(ctx, rows, s.options), nil
}
func wrapStmt(stmt driver.Stmt, query string, options TraceOptions) driver.Stmt {
_, isExecCtx := stmt.(driver.StmtExecContext)
_, isQueryCtx := stmt.(driver.StmtQueryContext)
cc, isColumnConverter := stmt.(driver.ColumnConverter) // nolint
nvc, isNamedValueChecker := stmt.(driver.NamedValueChecker)
s := otStmt{
Stmt: stmt,
query: query,
options: options,
}
switch {
case !isExecCtx && !isQueryCtx && !isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
}{s}
case isExecCtx && !isQueryCtx && !isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
}{s, s}
case !isExecCtx && isQueryCtx && !isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtQueryContext
}{s, s}
case !isExecCtx && !isQueryCtx && isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.ColumnConverter
}{s, cc}
case !isExecCtx && !isQueryCtx && !isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.NamedValueChecker
}{s, nvc}
case isExecCtx && isQueryCtx && !isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
}{s, s, s}
case isExecCtx && !isQueryCtx && isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.ColumnConverter
}{s, s, cc}
case isExecCtx && !isQueryCtx && !isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.NamedValueChecker
}{s, s, nvc}
case !isExecCtx && isQueryCtx && isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.ColumnConverter
}{s, s, cc}
case !isExecCtx && isQueryCtx && !isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, nvc}
case !isExecCtx && !isQueryCtx && isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.ColumnConverter
driver.NamedValueChecker
}{s, cc, nvc}
case isExecCtx && isQueryCtx && isColumnConverter && !isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.ColumnConverter
}{s, s, s, cc}
case isExecCtx && isQueryCtx && !isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.NamedValueChecker
}{s, s, s, nvc}
case isExecCtx && !isQueryCtx && isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, cc, nvc}
case !isExecCtx && isQueryCtx && isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtQueryContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, cc, nvc}
case isExecCtx && isQueryCtx && isColumnConverter && isNamedValueChecker:
return struct {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
driver.ColumnConverter
driver.NamedValueChecker
}{s, s, s, cc, nvc}
}
panic("unreachable")
}
type otTx struct {
driver.Tx
ctx context.Context
options TraceOptions
}
func (t otTx) Commit() (err error) {
ctx, _, endTrace := startTrace(t.ctx, t.options, methodCommit, "", nil)
defer func() {
endTrace(ctx, err)
}()
return t.Tx.Commit()
}
func (t otTx) Rollback() (err error) {
ctx, _, endTrace := startTrace(t.ctx, t.options, methodRollback, "", nil)
defer func() {
endTrace(ctx, err)
}()
return t.Tx.Rollback()
}
func wrapTx(ctx context.Context, tx driver.Tx, options TraceOptions) driver.Tx {
return otTx{
Tx: tx,
ctx: ctx,
options: options,
}
}