Skip to content

Commit 6684c3f

Browse files
authored
Adds function to wrap a driver.Conn directly (#25)
- Adds function to wrap a driver.Conn directly - Readme example of wrapping a driver.Conn - Updated readme with vanity import url
1 parent f67f654 commit 6684c3f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# ocsql
22

3-
[![Go Report Card](https://goreportcard.com/badge/github.com/opencensus-integrations/ocsql)](https://goreportcard.com/report/github.com/opencensus-integrations/ocsql)
4-
[![GoDoc](https://godoc.org/github.com/opencensus-integrations/ocsql?status.svg)](https://godoc.org/github.com/opencensus-integrations/ocsql)
3+
[![Go Report Card](https://goreportcard.com/badge/contrib.go.opencensus.io/integrations/ocsql)](https://goreportcard.com/report/contrib.go.opencensus.io/integrations/ocsql)
4+
[![GoDoc](https://godoc.org/contrib.go.opencensus.io/integrations/ocsql?status.svg)](https://godoc.org/contrib.go.opencensus.io/integrations/ocsql)
55
[![Sourcegraph](https://sourcegraph.com/github.com/opencensus-integrations/ocsql/-/badge.svg)](https://sourcegraph.com/github.com/opencensus-integrations/ocsql?badge)
66

77
OpenCensus SQL database driver wrapper.
88

99
Add an ocsql wrapper to your existing database code to instrument the
1010
interactions with the database.
1111

12+
## installation
13+
14+
go get -u contrib.go.opencensus.io/integrations/ocsql
15+
1216
## initialize
1317

1418
To use ocsql with your application, register an ocsql wrapper of a database
@@ -18,7 +22,7 @@ Example:
1822
```go
1923
import (
2024
_ "github.com/mattn/go-sqlite3"
21-
"github.com/opencensus-integrations/ocsql"
25+
"contrib.go.opencensus.io/integrations/ocsql"
2226
)
2327

2428
var (
@@ -45,7 +49,7 @@ Example:
4549
```go
4650
import (
4751
sqlite3 "github.com/mattn/go-sqlite3"
48-
"github.com/opencensus-integrations/ocsql"
52+
"contrib.go.opencensus.io/integrations/ocsql"
4953
)
5054

5155
var (
@@ -64,6 +68,22 @@ sql.Register("ocsql-sqlite3", driver)
6468
db, err = sql.Open("ocsql-sqlite3", "resource.db")
6569
```
6670

71+
Projects providing their own abstractions on top of database/sql/driver can also
72+
wrap an existing driver.Conn interface directly with ocsql.
73+
74+
Example:
75+
```go
76+
import "contrib.go.opencensus.io/integrations/ocsql"
77+
78+
func GetConn(...) driver.Conn {
79+
// create custom driver.Conn
80+
conn := initializeConn(...)
81+
82+
// wrap with ocsql
83+
return ocsql.WrapConn(conn, ocsql.WithAllTraceOptions())
84+
}
85+
```
86+
6787
## jmoiron/sqlx
6888

6989
If using the `sqlx` library with named queries you will need to use the

driver.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func Wrap(d driver.Driver, options ...TraceOption) driver.Driver {
8989
return wrapDriver(d, o)
9090
}
9191

92+
// Open implements driver.Driver
9293
func (d ocDriver) Open(name string) (driver.Conn, error) {
9394
c, err := d.parent.Open(name)
9495
if err != nil {
@@ -97,6 +98,15 @@ func (d ocDriver) Open(name string) (driver.Conn, error) {
9798
return wrapConn(c, d.options), nil
9899
}
99100

101+
// WrapConn allows an existing driver.Conn to be wrapped by ocsql.
102+
func WrapConn(c driver.Conn, options ...TraceOption) driver.Conn {
103+
o := TraceOptions{}
104+
for _, option := range options {
105+
option(&o)
106+
}
107+
return wrapConn(c, o)
108+
}
109+
100110
// ocConn implements driver.Conn
101111
type ocConn struct {
102112
parent driver.Conn

0 commit comments

Comments
 (0)