-
Notifications
You must be signed in to change notification settings - Fork 39
/
instrumentation_sql.go
593 lines (475 loc) · 15.8 KB
/
instrumentation_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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package instana
import (
"context"
"database/sql"
"database/sql/driver"
"net/url"
"regexp"
"slices"
"strings"
"sync"
_ "unsafe"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
var (
sqlDriverRegistrationMu sync.Mutex
)
// InstrumentSQLDriver instruments provided database driver for use with `sql.Open()`.
// This method will ignore any attempt to register the driver with the same name again.
//
// The instrumented version is registered with `_with_instana` suffix, e.g.
// if `postgres` provided as a name, the instrumented version is registered as
// `postgres_with_instana`.
func InstrumentSQLDriver(sensor TracerLogger, name string, driver driver.Driver) {
sqlDriverRegistrationMu.Lock()
defer sqlDriverRegistrationMu.Unlock()
instrumentedName := name + "_with_instana"
// Check if the instrumented version of a driver has already been registered
// with database/sql and ignore the second attempt to avoid panicking
for _, drv := range sql.Drivers() {
if drv == instrumentedName {
return
}
}
sql.Register(instrumentedName, &wrappedSQLDriver{
Driver: driver,
driverName: name,
sensor: sensor,
})
}
// SQLOpen is a convenience wrapper for `sql.Open()` to use the instrumented version
// of a driver previosly registered using `instana.InstrumentSQLDriver()`
func SQLOpen(driverName, dataSourceName string) (*sql.DB, error) {
if !strings.HasSuffix(driverName, "_with_instana") {
driverName += "_with_instana"
}
return sql.Open(driverName, dataSourceName)
}
//go:linkname drivers database/sql.drivers
var drivers map[string]driver.Driver
// SQLInstrumentAndOpen returns instrumented `*sql.DB`.
// It takes already registered `driver.Driver` by name, instruments it and additionally registers
// it with different name. After that it returns instrumented `*sql.DB` or error if any.
//
// This function can be used as a convenient shortcut for InstrumentSQLDriver and SQLOpen functions.
// The main difference is that this approach will use the already registered driver and using InstrumentSQLDriver
// requires to explicitly provide an instance of the driver to instrument.
func SQLInstrumentAndOpen(sensor TracerLogger, driverName, dataSourceName string) (*sql.DB, error) {
if d, ok := drivers[driverName]; ok {
InstrumentSQLDriver(sensor, driverName, d)
}
return SQLOpen(driverName, dataSourceName)
}
type wrappedSQLDriver struct {
driver.Driver
driverName string
sensor TracerLogger
}
func (drv *wrappedSQLDriver) Open(name string) (driver.Conn, error) {
conn, err := drv.Driver.Open(name)
if err != nil {
return conn, err
}
if connAlreadyWrapped(conn) {
return conn, nil
}
w := wrapConn(getDBConnDetails(name, drv.driverName), conn, drv.sensor)
return w, nil
}
// getDBConnDetails returns db connection details parsing connection URI and checking driver name
func getDBConnDetails(connStr, driverName string) DbConnDetails {
if isDB2driver(driverName) {
if details, ok := parseDB2ConnDetailsKV(connStr); ok {
return details
}
return DbConnDetails{RawString: connStr}
}
return ParseDBConnDetails(connStr)
}
func postgresSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
tags := ot.Tags{
"pg.stmt": query,
"pg.user": conn.User,
"pg.host": conn.Host,
}
if conn.Schema != "" {
tags["pg.db"] = conn.Schema
} else {
tags["pg.db"] = conn.RawString
}
if conn.Port != "" {
tags["pg.port"] = conn.Port
}
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan(string(PostgreSQLSpanType), opts...)
}
func mySQLSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
tags := ot.Tags{
"mysql.stmt": query,
"mysql.user": conn.User,
"mysql.host": conn.Host,
}
if conn.Schema != "" {
tags["mysql.db"] = conn.Schema
} else {
tags["mysql.db"] = conn.RawString
}
if conn.Port != "" {
tags["mysql.port"] = conn.Port
}
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan(string(MySQLSpanType), opts...)
}
var redisCmds = regexp.MustCompile(`(?i)SET|GET|DEL|INCR|DECR|APPEND|GETRANGE|SETRANGE|STRLEN|HSET|HGET|HMSET|HMGET|HDEL|HGETALL|HKEYS|HVALS|HLEN|HINCRBY|LPUSH|RPUSH|LPOP|RPOP|LLEN|LRANGE|LREM|LINDEX|LSET|SADD|SREM|SMEMBERS|SISMEMBER|SCARD|SINTER|SUNION|SDIFF|SRANDMEMBER|SPOP|ZADD|ZREM|ZRANGE|ZREVRANGE|ZRANK|ZREVRANK|ZRANGEBYSCORE|ZCARD|ZSCORE|PFADD|PFCOUNT|PFMERGE|SUBSCRIBE|UNSUBSCRIBE|PUBLISH|MULTI|EXEC|DISCARD|WATCH|UNWATCH|KEYS|EXISTS|EXPIRE|TTL|PERSIST|RENAME|RENAMENX|TYPE|SCAN|PING|INFO|CLIENT LIST|CONFIG GET|CONFIG SET|FLUSHDB|FLUSHALL|DBSIZE|SAVE|BGSAVE|BGREWRITEAOF|SHUTDOWN`)
func redisSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
qarr := strings.Fields(query)
var q string
for _, w := range qarr {
if redisCmds.MatchString(w) {
q += w + " "
}
}
tags := ot.Tags{
"redis.command": strings.TrimSpace(q),
}
if conn.Error != nil {
tags["redis.error"] = conn.Error.Error()
}
connection := conn.Host + ":" + conn.Port
if conn.Host == "" || conn.Port == "" {
i := strings.LastIndex(conn.RawString, "@")
connection = conn.RawString[i+1:]
}
tags["redis.connection"] = connection
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan(string(RedisSpanType), opts...)
}
func couchbaseSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
tags := ot.Tags{
"couchbase.hostname": conn.RawString,
"couchbase.sql": query,
}
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan(string(CouchbaseSpanType), opts...)
}
func cosmosSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
tags := ot.Tags{
"cosmos.cmd": query,
}
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan(string(CosmosSpanType), opts...)
}
func genericSQLSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) ot.Span {
tags := ot.Tags{
string(ext.DBType): "sql",
string(ext.DBStatement): query,
string(ext.PeerAddress): conn.RawString,
}
if conn.Schema != "" {
tags[string(ext.DBInstance)] = conn.Schema
} else {
tags[string(ext.DBInstance)] = conn.RawString
}
if conn.Host != "" {
tags[string(ext.PeerHostname)] = conn.Host
}
if conn.Port != "" {
tags[string(ext.PeerPort)] = conn.Port
}
opts := []ot.StartSpanOption{ext.SpanKindRPCClient, tags}
if parentSpan, ok := SpanFromContext(ctx); ok {
opts = append(opts, ot.ChildOf(parentSpan.Context()))
}
return sensor.StartSpan("sdk.database", opts...)
}
// dbNameByQuery attempts to guess what is the database based on the query.
func dbNameByQuery(q string) string {
qf := strings.Fields(q)
if len(qf) > 0 && redisCmds.MatchString(qf[0]) {
return "redis"
}
return ""
}
// StartSQLSpan creates a span based on DbConnDetails and a query, and attempts to detect which kind of database it belongs.
// If a database is detected and it is already part of the registered spans, the span details will be specific to that
// database.
// Otherwise, the span will have generic database fields.
func StartSQLSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) (sp ot.Span, dbKey string) {
return startSQLSpan(ctx, conn, query, sensor)
}
func startSQLSpan(ctx context.Context, conn DbConnDetails, query string, sensor TracerLogger) (sp ot.Span, dbKey string) {
if conn.DatabaseName == "" {
conn.DatabaseName = dbNameByQuery(query)
}
switch conn.DatabaseName {
case "postgres":
return postgresSpan(ctx, conn, query, sensor), "pg"
case "redis":
return redisSpan(ctx, conn, query, sensor), "redis"
case "mysql":
return mySQLSpan(ctx, conn, query, sensor), "mysql"
case "couchbase":
return couchbaseSpan(ctx, conn, query, sensor), "couchbase"
case "cosmos":
return cosmosSpan(ctx, conn, query, sensor), "cosmos"
}
return genericSQLSpan(ctx, conn, query, sensor), "db"
}
type DbConnDetails struct {
RawString string
Host, Port string
Schema string
User string
DatabaseName string
Error error
}
func ParseDBConnDetails(connStr string) DbConnDetails {
strategies := [...]func(string) (DbConnDetails, bool){
parseMySQLGoSQLDriver,
parsePostgresConnDetailsKV,
parseMySQLConnDetailsKV,
parseRedisConnString,
parseDBConnDetailsURI,
}
for _, parseFn := range strategies {
if details, ok := parseFn(connStr); ok {
return details
}
}
return DbConnDetails{RawString: connStr}
}
// isDB2driver checks whether the driver belongs to IBM Db2 database.
// The driver name is checked against known Db2 drivers.
func isDB2driver(name string) bool {
// Add the driver name in the knownDB2drivers array
// if a new Db2 driver needs to be supported.
knownDB2drivers := []string{
"go_ibm_db",
}
return slices.Contains(knownDB2drivers, name)
}
// parseDBConnDetailsURI attempts to parse a connection string as an URI, assuming that it has
// following format: [scheme://][user[:[password]]@]host[:port][/schema][?attribute1=value1&attribute2=value2...]
func parseDBConnDetailsURI(connStr string) (DbConnDetails, bool) {
u, err := url.Parse(connStr)
if err != nil {
return DbConnDetails{}, false
}
if u.Scheme == "" {
return DbConnDetails{}, false
}
path := ""
if len(u.Path) > 1 {
path = u.Path[1:]
}
details := DbConnDetails{
RawString: connStr,
Host: u.Hostname(),
Port: u.Port(),
Schema: path,
}
if u.User != nil {
details.User = u.User.Username()
// create a copy without user password
u := cloneURL(u)
u.User = url.User(details.User)
details.RawString = u.String()
}
if u.Scheme == "postgres" {
details.DatabaseName = u.Scheme
}
return details, true
}
var postgresKVPasswordRegex = regexp.MustCompile(`(^|\s)password=[^\s]+(\s|$)`)
// parsePostgresConnDetailsKV parses a space-separated PostgreSQL-style connection string
func parsePostgresConnDetailsKV(connStr string) (DbConnDetails, bool) {
var details DbConnDetails
for _, field := range strings.Split(connStr, " ") {
fieldNorm := strings.ToLower(field)
var (
prefix string
fieldPtr *string
)
switch {
case strings.HasPrefix(fieldNorm, "host="):
if details.Host != "" {
// hostaddr= takes precedence
continue
}
prefix, fieldPtr = "host=", &details.Host
case strings.HasPrefix(fieldNorm, "hostaddr="):
prefix, fieldPtr = "hostaddr=", &details.Host
case strings.HasPrefix(fieldNorm, "port="):
prefix, fieldPtr = "port=", &details.Port
case strings.HasPrefix(fieldNorm, "user="):
prefix, fieldPtr = "user=", &details.User
case strings.HasPrefix(fieldNorm, "dbname="):
prefix, fieldPtr = "dbname=", &details.Schema
default:
continue
}
*fieldPtr = field[len(prefix):]
}
if details.Schema == "" {
return DbConnDetails{}, false
}
details.RawString = postgresKVPasswordRegex.ReplaceAllString(connStr, " ")
details.DatabaseName = "postgres"
return details, true
}
var mysqlKVPasswordRegex = regexp.MustCompile(`(?i)(^|;)Pwd=[^;]+(;|$)`)
// parseMySQLConnDetailsKV parses a semicolon-separated MySQL-style connection string
func parseMySQLConnDetailsKV(connStr string) (DbConnDetails, bool) {
details := DbConnDetails{RawString: connStr, DatabaseName: "mysql"}
for _, field := range strings.Split(connStr, ";") {
fieldNorm := strings.ToLower(field)
var (
prefix string
fieldPtr *string
)
switch {
case strings.HasPrefix(fieldNorm, "server="):
prefix, fieldPtr = "server=", &details.Host
case strings.HasPrefix(fieldNorm, "port="):
prefix, fieldPtr = "port=", &details.Port
case strings.HasPrefix(fieldNorm, "uid="):
prefix, fieldPtr = "uid=", &details.User
case strings.HasPrefix(fieldNorm, "database="):
prefix, fieldPtr = "database=", &details.Schema
default:
continue
}
*fieldPtr = field[len(prefix):]
}
if details.Schema == "" {
return DbConnDetails{}, false
}
details.RawString = mysqlKVPasswordRegex.ReplaceAllString(connStr, ";")
return details, true
}
// parseDB2ConnDetailsKV parses a semicolon-separated MySQL-style connection string for DB2
func parseDB2ConnDetailsKV(connStr string) (DbConnDetails, bool) {
details := DbConnDetails{RawString: connStr, DatabaseName: "db2"}
for _, field := range strings.Split(connStr, ";") {
fieldNorm := strings.ToLower(field)
var (
prefix string
fieldPtr *string
)
switch {
case strings.HasPrefix(fieldNorm, "server="):
address := field[len("server="):]
addrFields := strings.Split(address, ":")
details.Host = addrFields[0]
if len(addrFields) > 1 {
details.Port = addrFields[1]
}
continue
case strings.HasPrefix(fieldNorm, "hostname="):
prefix, fieldPtr = "hostname=", &details.Host
case strings.HasPrefix(fieldNorm, "port="):
prefix, fieldPtr = "port=", &details.Port
case strings.HasPrefix(fieldNorm, "uid="):
prefix, fieldPtr = "uid=", &details.User
case strings.HasPrefix(fieldNorm, "database="):
prefix, fieldPtr = "database=", &details.Schema
default:
continue
}
*fieldPtr = field[len(prefix):]
}
details.RawString = mysqlKVPasswordRegex.ReplaceAllString(connStr, ";")
return details, true
}
var mySQLGoDriverRe = regexp.MustCompile(`^(.*):(.*)@((.*)\((.*):([0-9]+)\))?\/(.*)$`)
// parseMySQLGoSQLDriver parses the connection string from https://github.com/go-sql-driver/mysql
// Format: user:password@protocol(host:port)/databasename
// When protocol(host:port) is omitted, assume "tcp(localhost:3306)"
func parseMySQLGoSQLDriver(connStr string) (DbConnDetails, bool) {
// Expected matches
// 0 - Entire match. eg: go:gopw@tcp(localhost:3306)/godb
// 1 - User
// 2 - password
// 3 - protocol+host+port. Eg: tcp(localhost:3306)
// 4 - protocol (if "" use tcp)
// 5 - host (if "" use localhost)
// 6 - port (if "" use 3306)
// 7 - database name
matches := mySQLGoDriverRe.FindAllStringSubmatch(connStr, -1)
if len(matches) == 0 {
return DbConnDetails{}, false
}
values := matches[0]
host := values[5]
port := values[6]
if host == "" {
host = "localhost"
}
if port == "" {
port = "3306"
}
d := DbConnDetails{
RawString: connStr,
User: values[1],
Host: host,
Port: port,
Schema: values[7],
DatabaseName: "mysql",
}
return d, true
}
var redisOptionalUser = regexp.MustCompile(`^(.*:\/\/)?(.+)?:.+@(.+):(\d+)`)
// parseRedisConnString attempts to parse: user:password@host:port
// Based on conn string from github.com/bonede/go-redis-driver
func parseRedisConnString(connStr string) (DbConnDetails, bool) {
// Expected matches
// 0 - mysql://user:password@localhost:9898 or db://user:password@localhost:9898 and so on
// 1 - mysql:// or db:// and so on
// 2 - user
// 3 - localhost
// 4 - 1234
matches := redisOptionalUser.FindAllStringSubmatch(connStr, -1)
var d = DbConnDetails{}
if len(matches) == 0 {
return d, false
}
// We want to ignore the first match. for instance db:// or mysql:// will be ignored if matched
if matches[0][1] == "" {
return DbConnDetails{
Host: matches[0][3],
Port: matches[0][4],
DatabaseName: "redis",
RawString: connStr,
},
true
}
return d, false
}
type dsnConnector struct {
dsn string
driver driver.Driver
}
func (t dsnConnector) Connect(_ context.Context) (driver.Conn, error) {
return t.driver.Open(t.dsn)
}
func (t dsnConnector) Driver() driver.Driver {
return t.driver
}