forked from luna-duclos/instrumentedsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
executable file
·58 lines (47 loc) · 1.22 KB
/
connector.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
// +build go1.10
package instrumentedsql
import (
"context"
"database/sql/driver"
"time"
)
type wrappedConnector struct {
opts
parent driver.Connector
driverRef *WrappedDriver
}
var (
_ driver.Connector = wrappedConnector{}
)
func (c wrappedConnector) Connect(ctx context.Context) (conn driver.Conn, err error) {
if !c.hasOpExcluded(OpSQLConnectorConnect) {
span := c.GetSpan(ctx).NewChild(OpSQLConnectorConnect)
span.SetLabel("component", "database/sql")
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
c.Log(ctx, OpSQLConnectorConnect, "err", err, "duration", time.Since(start))
}()
}
conn, err = c.parent.Connect(ctx)
if err != nil {
return nil, err
}
return wrappedConn{opts: c.driverRef.opts, parent: conn}, nil
}
func (c wrappedConnector) Driver() driver.Driver {
return c.driverRef
}
// dsnConnector is a fallback connector placed in position of wrappedConnector.parent
// when given Driver does not comply with DriverContext interface.
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
}