Skip to content

Commit

Permalink
trace/sql: Wrap and return standard library sql.DB (beatlabs#178)
Browse files Browse the repository at this point in the history
Signed-off-by: Stratos Neiros <[email protected]>
  • Loading branch information
nstratos authored Mar 13, 2020
1 parent a89e360 commit 69990e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions trace/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func OpenDB(c driver.Connector) *DB {
return &DB{db: db}
}

// FromDB wraps an opened db. This allows to support libraries that provide
// *sql.DB like sqlmock.
func FromDB(db *sql.DB) *DB {
return &DB{db: db}
}

// DB returns the underlying db. This is useful for SQL code that does not
// require tracing.
func (db *DB) DB() *sql.DB {
return db.db
}

// BeginTx starts a transaction.
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
sp, _ := db.startSpan(ctx, "db.BeginTx", "")
Expand Down
8 changes: 8 additions & 0 deletions trace/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sql

import (
"context"
"database/sql"
"testing"

"github.com/beatlabs/patron/trace"
Expand Down Expand Up @@ -63,3 +64,10 @@ func TestSQLStartFinishSpan(t *testing.T) {
"key": "value",
}, rawSpan.Tags())
}

func TestFromDB(t *testing.T) {
want := &sql.DB{}
db := FromDB(want)
got := db.DB()
assert.Equal(t, want, got)
}

0 comments on commit 69990e1

Please sign in to comment.