-
Notifications
You must be signed in to change notification settings - Fork 39
/
instrumentation_sql_go1.10.go
76 lines (58 loc) · 1.57 KB
/
instrumentation_sql_go1.10.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package instana
import (
"context"
"database/sql/driver"
)
type wrappedSQLConnector struct {
driver.Connector
connDetails DbConnDetails
sensor TracerLogger
}
// WrapSQLConnector wraps an existing sql.Connector and instruments the DB calls made using it
func WrapSQLConnector(sensor TracerLogger, name string, connector driver.Connector) *wrappedSQLConnector {
if c, ok := connector.(*wrappedSQLConnector); ok {
return c
}
return &wrappedSQLConnector{
Connector: connector,
connDetails: ParseDBConnDetails(name),
sensor: sensor,
}
}
func (c *wrappedSQLConnector) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.Connector.Connect(ctx)
if err != nil {
return conn, err
}
if connAlreadyWrapped(conn) {
return conn, nil
}
w := wrapConn(c.connDetails, conn, c.sensor)
return w, nil
}
func (c *wrappedSQLConnector) Driver() driver.Driver {
if drv, ok := c.Connector.Driver().(*wrappedSQLDriver); ok {
return drv
}
return &wrappedSQLDriver{
Driver: c.Connector.Driver(),
sensor: c.sensor,
}
}
func (drv *wrappedSQLDriver) OpenConnector(name string) (driver.Connector, error) {
var connector driver.Connector = dsnConnector{dsn: name, driver: drv}
if d, ok := drv.Driver.(driver.DriverContext); ok {
var err error
connector, err = d.OpenConnector(name)
if err != nil {
return connector, err
}
}
if connector, ok := connector.(*wrappedSQLConnector); ok {
return connector, nil
}
wc := WrapSQLConnector(drv.sensor, name, connector)
return wc, nil
}