-
Notifications
You must be signed in to change notification settings - Fork 15
/
sql_driver.go
407 lines (341 loc) · 9.13 KB
/
sql_driver.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
package pinpoint
import (
"bytes"
"context"
"database/sql/driver"
"errors"
"fmt"
"time"
)
type DBInfo struct {
DBType int
QueryType int
DBName string
DBHost string
ParseDSN func(info *DBInfo, dsn string)
}
func parseDSN(info *DBInfo, dsn string) {
if f := info.ParseDSN; f != nil {
f(info, dsn)
}
}
// NewDatabaseTracer returns a Tracer for database operation.
func NewDatabaseTracer(ctx context.Context, funcName string, info *DBInfo) Tracer {
tracer := FromContext(ctx)
tracer.NewSpanEvent(funcName)
se := tracer.SpanEvent()
se.SetServiceType(int32(info.QueryType))
se.SetEndPoint(info.DBHost)
se.SetDestination(info.DBName)
return tracer
}
func wrapDriver(drv *sqlDriver) driver.Driver {
if _, ok := drv.Driver.(driver.DriverContext); ok {
return struct {
driver.Driver
driver.DriverContext
}{drv, drv}
} else {
return struct {
driver.Driver
}{drv}
}
}
// WrapSQLDriver wraps a driver.Driver and instruments SQL query calls.
func WrapSQLDriver(drv driver.Driver, info DBInfo) driver.Driver {
return wrapDriver(&sqlDriver{Driver: drv, dbInfo: info})
}
type sqlDriver struct {
driver.Driver
dbInfo DBInfo
}
func (d *sqlDriver) Open(name string) (driver.Conn, error) {
conn, err := d.Driver.Open(name)
if err != nil {
return nil, err
}
sc := newSqlConn(conn, d.dbInfo)
parseDSN(&sc.dbInfo, name)
return sc, nil
}
func (d *sqlDriver) OpenConnector(name string) (driver.Connector, error) {
conn, err := d.Driver.(driver.DriverContext).OpenConnector(name)
if err != nil {
return nil, err
}
sc := &sqlConnector{
Connector: conn,
dbInfo: d.dbInfo,
driver: d,
}
parseDSN(&sc.dbInfo, name)
return sc, nil
}
type sqlConnector struct {
driver.Connector
dbInfo DBInfo
driver *sqlDriver
}
func (c *sqlConnector) Connect(ctx context.Context) (driver.Conn, error) {
if conn, err := c.Connector.Connect(ctx); err != nil {
return nil, err
} else {
return newSqlConn(conn, c.dbInfo), nil
}
}
func (c *sqlConnector) Driver() driver.Driver {
return c.driver
}
type sqlConn struct {
driver.Conn
dbInfo DBInfo
config *Config
}
func newSqlConn(conn driver.Conn, dbInfo DBInfo) *sqlConn {
return &sqlConn{
Conn: conn,
dbInfo: dbInfo,
config: GetConfig(),
}
}
func prepare(stmt driver.Stmt, err error, conn *sqlConn, sql string) (driver.Stmt, error) {
if nil != err {
return nil, err
}
return &sqlStmt{
Stmt: stmt,
conn: conn,
sql: sql,
}, nil
}
func (c *sqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
if cpc, ok := c.Conn.(driver.ConnPrepareContext); ok {
stmt, err := cpc.PrepareContext(ctx, query)
return prepare(stmt, err, c, query)
}
stmt, err := c.Conn.Prepare(query)
return prepare(stmt, err, c, query)
}
func (c *sqlConn) newSqlSpanEventWithNamedValue(ctx context.Context, operation string, start time.Time, err error, sql string, args []driver.NamedValue) {
tracer := NewDatabaseTracer(ctx, operation, &c.dbInfo)
defer tracer.EndSpanEvent()
if tracer.IsSampled() {
setSqlSpanEvent(tracer, start, err, sql, c.namedValueToString(args))
}
}
func (c *sqlConn) newSqlSpanEventWithValue(ctx context.Context, operation string, start time.Time, err error, sql string, args []driver.Value) {
tracer := NewDatabaseTracer(ctx, operation, &c.dbInfo)
defer tracer.EndSpanEvent()
if tracer.IsSampled() {
setSqlSpanEvent(tracer, start, err, sql, c.valueToString(args))
}
}
func (c *sqlConn) newSqlSpanEventNoSql(ctx context.Context, operation string, start time.Time, err error) {
tracer := NewDatabaseTracer(ctx, operation, &c.dbInfo)
defer tracer.EndSpanEvent()
if tracer.IsSampled() {
setSqlSpanEvent(tracer, start, err, "", "")
}
}
func setSqlSpanEvent(tracer Tracer, start time.Time, err error, sql string, args string) {
tracer.SpanEvent().SetSQL(sql, args)
tracer.SpanEvent().SetError(err, "SQL error")
tracer.SpanEvent().FixDuration(start, time.Now())
}
func (c *sqlConn) namedValueToString(named []driver.NamedValue) string {
if !c.config.sqlTraceBindValue || named == nil {
return ""
}
var b bytes.Buffer
numComma := len(named) - 1
for i, param := range named {
if !writeBindValue(&b, i, param.Value, numComma, c.config.sqlMaxBindValueSize) {
break
}
}
return b.String()
}
func (c *sqlConn) valueToString(values []driver.Value) string {
if !c.config.sqlTraceBindValue || values == nil {
return ""
}
var b bytes.Buffer
numComma := len(values) - 1
for i, v := range values {
if !writeBindValue(&b, i, v, numComma, c.config.sqlMaxBindValueSize) {
break
}
}
return b.String()
}
func writeBindValue(b *bytes.Buffer, index int, value interface{}, numComma int, maxSize int) bool {
b.WriteString(fmt.Sprint(value))
if index < numComma {
b.WriteString(", ")
}
if b.Len() > maxSize {
b.WriteString("...(")
b.WriteString(fmt.Sprint(maxSize))
b.WriteString(")")
return false
}
return true
}
func (c *sqlConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
start := time.Now()
if ec, ok := c.Conn.(driver.ExecerContext); ok {
result, err := ec.ExecContext(ctx, query, args)
if err != driver.ErrSkip {
c.newSqlSpanEventWithNamedValue(ctx, "ConnExecContext", start, err, query, args)
}
return result, err
}
// sourced: database/sql/cxtutil.go
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
if e, ok := c.Conn.(driver.Execer); ok {
result, err := e.Exec(query, dargs)
if err != driver.ErrSkip {
c.newSqlSpanEventWithValue(ctx, "ConnExec", start, err, query, dargs)
}
return result, err
}
return nil, driver.ErrSkip
}
func (c *sqlConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
start := time.Now()
if qc, ok := c.Conn.(driver.QueryerContext); ok {
rows, err := qc.QueryContext(ctx, query, args)
if err != driver.ErrSkip {
c.newSqlSpanEventWithNamedValue(ctx, "ConnQueryContext", start, err, query, args)
}
return rows, err
}
// sourced: database/sql/cxtutil.go
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
if q, ok := c.Conn.(driver.Queryer); ok {
rows, err := q.Query(query, dargs)
if err != driver.ErrSkip {
c.newSqlSpanEventWithValue(ctx, "ConnQuery", start, err, query, dargs)
}
return rows, err
}
return nil, driver.ErrSkip
}
func (c *sqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
var tx driver.Tx
var err error
start := time.Now()
if cbt, ok := c.Conn.(driver.ConnBeginTx); ok {
tx, err = cbt.BeginTx(ctx, opts)
if c.config.sqlTraceCommit || c.config.sqlTraceRollback {
c.newSqlSpanEventNoSql(ctx, "BeginTx", start, err)
if err == nil {
tx = &sqlTx{tx, c, ctx}
}
}
return tx, err
}
tx, err = c.Conn.Begin()
if c.config.sqlTraceCommit || c.config.sqlTraceRollback {
c.newSqlSpanEventNoSql(ctx, "Begin", start, err)
if err == nil {
tx = &sqlTx{tx, c, ctx}
}
}
return tx, err
}
type sqlTx struct {
driver.Tx
conn *sqlConn
ctx context.Context
}
func (t *sqlTx) Commit() (err error) {
start := time.Now()
err = t.Tx.Commit()
if t.conn.config.sqlTraceCommit {
t.conn.newSqlSpanEventNoSql(t.ctx, "Commit", start, err)
}
return err
}
func (t *sqlTx) Rollback() (err error) {
start := time.Now()
err = t.Tx.Rollback()
if t.conn.config.sqlTraceRollback {
t.conn.newSqlSpanEventNoSql(t.ctx, "Rollback", start, err)
}
return err
}
type sqlStmt struct {
driver.Stmt
conn *sqlConn
sql string
}
func (s *sqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
start := time.Now()
if sec, ok := s.Stmt.(driver.StmtExecContext); ok {
result, err := sec.ExecContext(ctx, args)
s.conn.newSqlSpanEventWithNamedValue(ctx, "StmtExecContext", start, err, s.sql, args)
return result, err
}
// sourced: database/sql/cxtutil.go
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
result, err := s.Stmt.Exec(dargs)
s.conn.newSqlSpanEventWithValue(ctx, "StmtExec", start, err, s.sql, dargs)
return result, err
}
func (s *sqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
start := time.Now()
if sqc, ok := s.Stmt.(driver.StmtQueryContext); ok {
rows, err := sqc.QueryContext(ctx, args)
s.conn.newSqlSpanEventWithNamedValue(ctx, "StmtQueryContext", start, err, s.sql, args)
return rows, err
}
// sourced: database/sql/cxtutil.go
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
rows, err := s.Stmt.Query(dargs)
s.conn.newSqlSpanEventWithValue(ctx, "StmtQuery", start, err, s.sql, dargs)
return rows, err
}
// sourced: database/sql/cxtutil.go
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
}