From e79ab77430babe396515bc4e384a393df6dc1bef Mon Sep 17 00:00:00 2001 From: Sam Xie Date: Sat, 14 Sep 2024 17:06:42 -0700 Subject: [PATCH] Increase the compatibility of `Open` method (#359) resolve #358 --- CHANGELOG.md | 7 +++++++ sql.go | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3778411..e063523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Fixed + +- The `Open` method uses the `dataSourceName` when calling `sql.Open`. (#359) + + This change improves compatibility with certain drivers that perform a verification of the `dataSourceName` before establishing a connection. + ## [0.33.0] - 2024-08-27 ### Added @@ -214,6 +220,7 @@ This update contains a breaking change of the removal of `SpanOptions.AllowRoot` ### Added - SpanOptions to suppress creation of spans. (#87, #102) + - `OmitConnResetSession` - `OmitConnPrepare` - `OmitConnQuery` diff --git a/sql.go b/sql.go index 465f288..805fcd5 100644 --- a/sql.go +++ b/sql.go @@ -76,8 +76,15 @@ func WrapDriver(dri driver.Driver, options ...Option) driver.Driver { // Open is a wrapper over sql.Open with OTel instrumentation. func Open(driverName, dataSourceName string, options ...Option) (*sql.DB, error) { - // Retrieve the driver implementation we need to wrap with instrumentation - db, err := sql.Open(driverName, "") + // Retrieve the driver implementation we need to wrap with instrumentation. + // The dataSourceName is used to bypass the driver's Open method, as some + // drivers validate the data source name first before actually opening + // connections. + // Any connection opened here (usually no connection will be opened) is not + // used, and it will be closed immediately to prevent leaking connections. + // Usually, no connection will be opened here if the driver implements + // the driver.DriverContext interface. + db, err := sql.Open(driverName, dataSourceName) if err != nil { return nil, err }