github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/instrumentation_sql_go1.10.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 // +build go1.10 5 6 package instana 7 8 import ( 9 "context" 10 "database/sql/driver" 11 ) 12 13 type wrappedSQLConnector struct { 14 driver.Connector 15 16 connDetails dbConnDetails 17 sensor *Sensor 18 } 19 20 // WrapSQLConnector wraps an existing sql.Connector and instruments the DB calls made using it 21 func WrapSQLConnector(sensor *Sensor, name string, connector driver.Connector) *wrappedSQLConnector { 22 if c, ok := connector.(*wrappedSQLConnector); ok { 23 return c 24 } 25 26 return &wrappedSQLConnector{ 27 Connector: connector, 28 connDetails: parseDBConnDetails(name), 29 sensor: sensor, 30 } 31 } 32 33 func (c *wrappedSQLConnector) Connect(ctx context.Context) (driver.Conn, error) { 34 conn, err := c.Connector.Connect(ctx) 35 if err != nil { 36 return conn, err 37 } 38 39 if conn, ok := conn.(*wrappedSQLConn); ok { 40 return conn, nil 41 } 42 43 return &wrappedSQLConn{ 44 Conn: conn, 45 details: c.connDetails, 46 sensor: c.sensor, 47 }, nil 48 } 49 50 func (c *wrappedSQLConnector) Driver() driver.Driver { 51 if drv, ok := c.Connector.Driver().(*wrappedSQLDriver); ok { 52 return drv 53 } 54 55 return &wrappedSQLDriver{ 56 Driver: c.Connector.Driver(), 57 sensor: c.sensor, 58 } 59 } 60 61 func (drv *wrappedSQLDriver) OpenConnector(name string) (driver.Connector, error) { 62 var connector driver.Connector = dsnConnector{dsn: name, driver: drv} 63 64 if d, ok := drv.Driver.(driver.DriverContext); ok { 65 var err error 66 67 connector, err = d.OpenConnector(name) 68 if err != nil { 69 return connector, err 70 } 71 } 72 73 if connector, ok := connector.(*wrappedSQLConnector); ok { 74 return connector, nil 75 } 76 77 return WrapSQLConnector(drv.sensor, name, connector), nil 78 }