Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix io.Closer support for connectors #199

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Upgrade OTel to version `v1.20.0/v0.43.0`. (#196)
- Fixes an issue where `db.Close` did not call `Close` on the underlying connector. (#199)

## [0.26.0] - 2023-10-11

Expand Down
11 changes: 11 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package otelsql
import (
"context"
"database/sql/driver"
"io"

"go.opentelemetry.io/otel/trace"
)

var _ driver.Connector = (*otConnector)(nil)
var _ io.Closer = (*otConnector)(nil)

type otConnector struct {
driver.Connector
Expand Down Expand Up @@ -62,6 +64,15 @@ func (c *otConnector) Driver() driver.Driver {
return c.otDriver
}

func (c *otConnector) Close() error {
// database/sql uses a type assertion to check if connectors implement io.Closer.
// The type assertion does not pass through to otConnector.Connector, so we explicitly implement it here.
if closer, ok := c.Connector.(io.Closer); ok {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add an implement assertion in line 26 to show otConnector implements io.Closer?
https://github.com/XSAM/otelsql/pull/199/files#diff-c1caa8de39e277339469495d65efd1a363c54065c3cc02e7422b097774e56c86R25
It should be

var _ io.Closer = (*otConnector)(nil)

return closer.Close()
}
return nil
}

// dsnConnector is copied from sql.dsnConnector.
type dsnConnector struct {
dsn string
Expand Down
Loading