Skip to content

Commit a227edf

Browse files
authored
Merge pull request #31 from opencensus-integrations/wrap-connector
Add WrapConnector method to explicitly wrap an existing driver.Connector
2 parents 28a9935 + ceb4de9 commit a227edf

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

README.md

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ var (
5858
db *sql.DB
5959
)
6060

61-
// Explicitly wrap the SQLite3 driver with ocsql
61+
// Explicitly wrap the SQLite3 driver with ocsql.
6262
driver = ocsql.Wrap(&sqlite3.SQLiteDriver{})
6363

64-
// Register our ocsql wrapper as a database driver
64+
// Register our ocsql wrapper as a database driver.
6565
sql.Register("ocsql-sqlite3", driver)
6666

67-
// Connect to a SQLite3 database using the ocsql driver wrapper
67+
// Connect to a SQLite3 database using the ocsql driver wrapper.
6868
db, err = sql.Open("ocsql-sqlite3", "resource.db")
6969
```
7070

@@ -76,21 +76,51 @@ Example:
7676
import "contrib.go.opencensus.io/integrations/ocsql"
7777

7878
func GetConn(...) driver.Conn {
79-
// create custom driver.Conn
79+
// Create custom driver.Conn.
8080
conn := initializeConn(...)
8181

82-
// wrap with ocsql
82+
// Wrap with ocsql.
8383
return ocsql.WrapConn(conn, ocsql.WithAllTraceOptions())
8484
}
8585
```
8686

87+
Finally database drivers that support the new (Go 1.10+) driver.Connector
88+
interface can be wrapped directly by ocsql without the need for ocsql to
89+
register a driver.Driver.
90+
91+
Example:
92+
```go
93+
import(
94+
"contrib.go.opencensus.io/integrations/ocsql"
95+
"github.com/lib/pq"
96+
)
97+
98+
var (
99+
connector driver.Connector
100+
err error
101+
db *sql.DB
102+
)
103+
104+
// Get a database driver.Connector for a fixed configuration.
105+
connector, err = pq.NewConnector("postgres://user:passt@host:5432/db")
106+
if err != nil {
107+
log.Fatalf("unable to create our postgres connector: %v\n", err)
108+
}
109+
110+
// Wrap the driver.Connector with ocsql.
111+
connector = ocsql.WrapConnector(connector, ocsql.WithAllTraceOptions())
112+
113+
// Use the wrapped driver.Connector.
114+
db = sql.OpenDB(connector)
115+
```
116+
87117
## metrics
88118

89119
Next to tracing, ocsql also supports OpenCensus stats. To record call stats,
90120
register the available views or create your own using the provided Measures.
91121

92122
```go
93-
// register default views
123+
// Register default views.
94124
ocsql.RegisterAllViews()
95125

96126
```
@@ -100,13 +130,13 @@ pool details. Use the `RecordStats` function and provide a `*sql.DB` to record
100130
details on, as well as the required record interval.
101131

102132
```go
103-
// register default views
133+
// Register default views.
104134
ocsql.RegisterAllViews()
105135

106-
// Connect to a SQLite3 database using the ocsql driver wrapper
136+
// Connect to a SQLite3 database using the ocsql driver wrapper.
107137
db, err = sql.Open("ocsql-sqlite3", "resource.db")
108138

109-
// Record DB stats every 5 seconds until we exit
139+
// Record DB stats every 5 seconds until we exit.
110140
defer ocsql.RecordStats(db, 5 * time.Second)()
111141
```
112142

@@ -186,7 +216,7 @@ Example:
186216
```go
187217

188218
func (s *svc) GetDevice(ctx context.Context, id int) (*Device, error) {
189-
// assume we have instrumented our service transports and ctx holds a span.
219+
// Assume we have instrumented our service transports and ctx holds a span.
190220
var device Device
191221
if err := s.db.QueryRowContext(
192222
ctx, "SELECT * FROM device WHERE id = ?", id,

driver_go1.10.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ var (
1616
_ driver.Connector = &ocDriver{}
1717
)
1818

19+
// WrapConnector allows wrapping a database driver.Connector which eliminates
20+
// the need to register ocsql as an available driver.Driver.
21+
func WrapConnector(dc driver.Connector, options ...TraceOption) driver.Connector {
22+
opts := TraceOptions{}
23+
for _, o := range options {
24+
o(&opts)
25+
}
26+
27+
return &ocDriver{
28+
parent: dc.Driver(),
29+
connector: dc,
30+
options: opts,
31+
}
32+
}
33+
1934
// ocDriver implements driver.Driver
2035
type ocDriver struct {
2136
parent driver.Driver

0 commit comments

Comments
 (0)