-
Notifications
You must be signed in to change notification settings - Fork 72
/
driver.go
466 lines (378 loc) · 12 KB
/
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
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
package otelsql
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"go.opentelemetry.io/otel/trace"
)
// Open is a wrapper over sql.Open that instruments the sql.DB to record executed queries
// using OpenTelemetry API.
func Open(driverName, dsn string, opts ...Option) (*sql.DB, error) {
db, err := sql.Open(driverName, dsn)
if err != nil {
return nil, err
}
return patchDB(db, dsn, opts...)
}
func patchDB(db *sql.DB, dsn string, opts ...Option) (*sql.DB, error) {
dbDriver := db.Driver()
// Close the db since we are about to open a new one.
if err := db.Close(); err != nil {
return nil, err
}
d := newDriver(dbDriver, opts)
if _, ok := dbDriver.(driver.DriverContext); ok {
connector, err := d.OpenConnector(dsn)
if err != nil {
return nil, err
}
return sqlOpenDB(connector, d.instrum), nil
}
return sqlOpenDB(&dsnConnector{
driver: d,
dsn: dsn,
}, d.instrum), nil
}
// OpenDB is a wrapper over sql.OpenDB that instruments the sql.DB to record executed queries
// using OpenTelemetry API.
func OpenDB(connector driver.Connector, opts ...Option) *sql.DB {
instrum := newDBInstrum(opts)
c := newConnector(connector.Driver(), connector, instrum)
return sqlOpenDB(c, instrum)
}
func sqlOpenDB(connector driver.Connector, instrum *dbInstrum) *sql.DB {
db := sql.OpenDB(connector)
ReportDBStatsMetrics(db, WithMeterProvider(instrum.meterProvider), WithAttributes(instrum.attrs...))
return db
}
type dsnConnector struct {
driver *otelDriver
dsn string
}
func (c *dsnConnector) Connect(ctx context.Context) (driver.Conn, error) {
var conn driver.Conn
err := c.driver.instrum.withSpan(ctx, "db.Connect", "",
func(ctx context.Context, span trace.Span) error {
var err error
conn, err = c.driver.Open(c.dsn)
return err
})
return conn, err
}
func (c *dsnConnector) Driver() driver.Driver {
return c.driver
}
//------------------------------------------------------------------------------
type otelDriver struct {
driver driver.Driver
driverCtx driver.DriverContext
instrum *dbInstrum
}
var _ driver.DriverContext = (*otelDriver)(nil)
func newDriver(dr driver.Driver, opts []Option) *otelDriver {
driverCtx, _ := dr.(driver.DriverContext)
d := &otelDriver{
driver: dr,
driverCtx: driverCtx,
instrum: newDBInstrum(opts),
}
return d
}
func (d *otelDriver) Open(name string) (driver.Conn, error) {
conn, err := d.driver.Open(name)
if err != nil {
return nil, err
}
return newConn(conn, d.instrum), nil
}
func (d *otelDriver) OpenConnector(dsn string) (driver.Connector, error) {
connector, err := d.driverCtx.OpenConnector(dsn)
if err != nil {
return nil, err
}
return newConnector(d, connector, d.instrum), nil
}
//------------------------------------------------------------------------------
type connector struct {
driver.Connector
driver driver.Driver
instrum *dbInstrum
}
var _ driver.Connector = (*connector)(nil)
func newConnector(d driver.Driver, c driver.Connector, instrum *dbInstrum) *connector {
return &connector{
driver: d,
Connector: c,
instrum: instrum,
}
}
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
var conn driver.Conn
if err := c.instrum.withSpan(ctx, "db.Connect", "",
func(ctx context.Context, span trace.Span) error {
var err error
conn, err = c.Connector.Connect(ctx)
return err
}); err != nil {
return nil, err
}
return newConn(conn, c.instrum), nil
}
func (c *connector) Driver() driver.Driver {
return c.driver
}
//------------------------------------------------------------------------------
type otelConn struct {
driver.Conn
instrum *dbInstrum
ping pingFunc
exec execFunc
execCtx execCtxFunc
query queryFunc
queryCtx queryCtxFunc
prepareCtx prepareCtxFunc
beginTx beginTxFunc
resetSession resetSessionFunc
checkNamedValue checkNamedValueFunc
}
var _ driver.Conn = (*otelConn)(nil)
func newConn(conn driver.Conn, instrum *dbInstrum) *otelConn {
cn := &otelConn{
Conn: conn,
instrum: instrum,
}
cn.ping = cn.createPingFunc(conn)
cn.exec = cn.createExecFunc(conn)
cn.execCtx = cn.createExecCtxFunc(conn)
cn.query = cn.createQueryFunc(conn)
cn.queryCtx = cn.createQueryCtxFunc(conn)
cn.prepareCtx = cn.createPrepareCtxFunc(conn)
cn.beginTx = cn.createBeginTxFunc(conn)
cn.resetSession = cn.createResetSessionFunc(conn)
cn.checkNamedValue = cn.createCheckNamedValueFunc(conn)
return cn
}
var _ driver.Pinger = (*otelConn)(nil)
func (c *otelConn) Ping(ctx context.Context) error {
return c.ping(ctx)
}
type pingFunc func(ctx context.Context) error
func (c *otelConn) createPingFunc(conn driver.Conn) pingFunc {
if pinger, ok := conn.(driver.Pinger); ok {
return func(ctx context.Context) error {
return c.instrum.withSpan(ctx, "db.Ping", "",
func(ctx context.Context, span trace.Span) error {
return pinger.Ping(ctx)
})
}
}
return func(ctx context.Context) error {
return driver.ErrSkip
}
}
//------------------------------------------------------------------------------
var _ driver.Execer = (*otelConn)(nil)
func (c *otelConn) Exec(query string, args []driver.Value) (driver.Result, error) {
return c.exec(query, args)
}
type execFunc func(query string, args []driver.Value) (driver.Result, error)
func (c *otelConn) createExecFunc(conn driver.Conn) execFunc {
if execer, ok := conn.(driver.Execer); ok {
return func(query string, args []driver.Value) (driver.Result, error) {
return execer.Exec(query, args)
}
}
return func(query string, args []driver.Value) (driver.Result, error) {
return nil, driver.ErrSkip
}
}
//------------------------------------------------------------------------------
var _ driver.ExecerContext = (*otelConn)(nil)
func (c *otelConn) ExecContext(
ctx context.Context, query string, args []driver.NamedValue,
) (driver.Result, error) {
return c.execCtx(ctx, query, args)
}
type execCtxFunc func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
func (c *otelConn) createExecCtxFunc(conn driver.Conn) execCtxFunc {
var fn execCtxFunc
if execer, ok := conn.(driver.ExecerContext); ok {
fn = execer.ExecContext
} else {
fn = func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
vArgs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return c.exec(query, vArgs)
}
}
return func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
var res driver.Result
if err := c.instrum.withSpan(ctx, "db.Exec", query,
func(ctx context.Context, span trace.Span) error {
var err error
res, err = fn(ctx, query, args)
if err != nil {
return err
}
if span.IsRecording() {
rows, err := res.RowsAffected()
if err == nil {
span.SetAttributes(dbRowsAffected.Int64(rows))
}
}
return nil
}); err != nil {
return nil, err
}
return res, nil
}
}
//------------------------------------------------------------------------------
var _ driver.Queryer = (*otelConn)(nil)
func (c *otelConn) Query(query string, args []driver.Value) (driver.Rows, error) {
return c.query(query, args)
}
type queryFunc func(query string, args []driver.Value) (driver.Rows, error)
func (c *otelConn) createQueryFunc(conn driver.Conn) queryFunc {
if queryer, ok := c.Conn.(driver.Queryer); ok {
return func(query string, args []driver.Value) (driver.Rows, error) {
return queryer.Query(query, args)
}
}
return func(query string, args []driver.Value) (driver.Rows, error) {
return nil, driver.ErrSkip
}
}
//------------------------------------------------------------------------------
var _ driver.QueryerContext = (*otelConn)(nil)
func (c *otelConn) QueryContext(
ctx context.Context, query string, args []driver.NamedValue,
) (driver.Rows, error) {
return c.queryCtx(ctx, query, args)
}
type queryCtxFunc func(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
func (c *otelConn) createQueryCtxFunc(conn driver.Conn) queryCtxFunc {
var fn queryCtxFunc
if queryer, ok := c.Conn.(driver.QueryerContext); ok {
fn = queryer.QueryContext
} else {
fn = func(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
vArgs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
return c.query(query, vArgs)
}
}
return func(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
var rows driver.Rows
err := c.instrum.withSpan(ctx, "db.Query", query,
func(ctx context.Context, span trace.Span) error {
var err error
rows, err = fn(ctx, query, args)
return err
})
return rows, err
}
}
//------------------------------------------------------------------------------
var _ driver.ConnPrepareContext = (*otelConn)(nil)
func (c *otelConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return c.prepareCtx(ctx, query)
}
type prepareCtxFunc func(ctx context.Context, query string) (driver.Stmt, error)
func (c *otelConn) createPrepareCtxFunc(conn driver.Conn) prepareCtxFunc {
var fn prepareCtxFunc
if preparer, ok := c.Conn.(driver.ConnPrepareContext); ok {
fn = preparer.PrepareContext
} else {
fn = func(ctx context.Context, query string) (driver.Stmt, error) {
return c.Conn.Prepare(query)
}
}
return func(ctx context.Context, query string) (driver.Stmt, error) {
var stmt driver.Stmt
if err := c.instrum.withSpan(ctx, "db.Prepare", query,
func(ctx context.Context, span trace.Span) error {
var err error
stmt, err = fn(ctx, query)
return err
}); err != nil {
return nil, err
}
return newStmt(stmt, query, c.instrum), nil
}
}
var _ driver.ConnBeginTx = (*otelConn)(nil)
func (c *otelConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return c.beginTx(ctx, opts)
}
type beginTxFunc func(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
func (c *otelConn) createBeginTxFunc(conn driver.Conn) beginTxFunc {
var fn beginTxFunc
if txor, ok := conn.(driver.ConnBeginTx); ok {
fn = txor.BeginTx
} else {
fn = func(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return conn.Begin()
}
}
return func(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
var tx driver.Tx
if err := c.instrum.withSpan(ctx, "db.Begin", "",
func(ctx context.Context, span trace.Span) error {
var err error
tx, err = fn(ctx, opts)
return err
}); err != nil {
return nil, err
}
return newTx(ctx, tx, c.instrum), nil
}
}
//------------------------------------------------------------------------------
var _ driver.SessionResetter = (*otelConn)(nil)
func (c *otelConn) ResetSession(ctx context.Context) error {
return c.resetSession(ctx)
}
type resetSessionFunc func(ctx context.Context) error
func (c *otelConn) createResetSessionFunc(conn driver.Conn) resetSessionFunc {
if resetter, ok := c.Conn.(driver.SessionResetter); ok {
return func(ctx context.Context) error {
return resetter.ResetSession(ctx)
}
}
return func(ctx context.Context) error {
return driver.ErrSkip
}
}
//------------------------------------------------------------------------------
var _ driver.NamedValueChecker = (*otelConn)(nil)
func (c *otelConn) CheckNamedValue(value *driver.NamedValue) error {
return c.checkNamedValue(value)
}
type checkNamedValueFunc func(*driver.NamedValue) error
func (c *otelConn) createCheckNamedValueFunc(conn driver.Conn) checkNamedValueFunc {
if checker, ok := c.Conn.(driver.NamedValueChecker); ok {
return func(value *driver.NamedValue) error {
return checker.CheckNamedValue(value)
}
}
return func(value *driver.NamedValue) error {
return driver.ErrSkip
}
}
//------------------------------------------------------------------------------
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
args := make([]driver.Value, len(named))
for n, param := range named {
if len(param.Name) > 0 {
return nil, errors.New("otelsql: driver does not support named parameters")
}
args[n] = param.Value
}
return args, nil
}