Skip to content

Commit

Permalink
contrib/sql/driver: use the connector provided by the sql driver if p…
Browse files Browse the repository at this point in the history
…ossible (#1502)

* contrib/sql/driver: use the connector provided by the sql driver if possible

Currently, when the sql tracer opens a new database with the Open(...) function, it uses a default dsnConnector.
However, if the driver implements the `DriverContext` interface, the one provided by the `OpenConnector(...)` method on the driver should be used instead.
And the documentation on the sql library says that the `DriverContext` should be implemented :

> The driver interface has evolved over time. Drivers should implement Connector and DriverContext interfaces.

(from: https://pkg.go.dev/database/sql/driver@go1.19.2)

This commit uses the connector provided by the sql driver if it implements the `DriverContext` and follows the same logic as the std lib sql library: https://github.com/golang/go/blob/515e3de2999b23da28e6d15ac485bfdd299ec83a/src/database/sql/sql.go#L821-L829

* Empty Commit to re-trigger CI

Co-authored-by: Andrew Glaude <andrew.glaude@datadoghq.com>
Co-authored-by: Alex Normand <alex.normand@datadoghq.com>
  • Loading branch information
3 people committed Oct 7, 2022
1 parent 3e559a2 commit dbab956
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contrib/database/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,14 @@ func Open(driverName, dataSourceName string, opts ...Option) (*sql.DB, error) {
return nil, errNotRegistered
}
d, _ := registeredDrivers.driver(driverName)
if driverCtx, ok := d.(driver.DriverContext); ok {
connector, err := driverCtx.OpenConnector(dataSourceName)
if err != nil {
return nil, err
}
// since we're not using the dsnConnector, we need to register the dsn manually in the config
opts = append(opts, WithDSN(dataSourceName))
return OpenDB(connector, opts...), nil
}
return OpenDB(&dsnConnector{dsn: dataSourceName, driver: d}, opts...), nil
}

0 comments on commit dbab956

Please sign in to comment.