Skip to content

Commit da30d5c

Browse files
committed
improved readme and code documentation
1 parent 212e4a7 commit da30d5c

File tree

3 files changed

+167
-21
lines changed

3 files changed

+167
-21
lines changed

README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
11
# ocsql
2-
OpenCensus SQL database driver wrapper
2+
OpenCensus SQL database driver wrapper.
3+
4+
Add an ocsql wrapper to your existing database code to instrument the
5+
interactions with the database.
6+
7+
## initialize
8+
9+
To use ocsql with your application, register an ocsql wrapper of a database
10+
driver as shown below.
11+
12+
Example:
13+
```go
14+
import (
15+
"github.com/basvanbeek/ocsql"
16+
_ "github.com/mattn/go-sqlite3"
17+
)
18+
19+
var (
20+
driverName string
21+
err error
22+
db *sql.DB
23+
)
24+
25+
// Register our ocsql wrapper for the provided SQLite3 driver.
26+
driverName, err = ocsql.Register("sqlite3", ocsql.WithOptions(ocsql.TraceAll))
27+
if err != nil {
28+
log.Fatalf("unable to register our ocsql driver: %v\n", err)
29+
}
30+
31+
// Connect to a SQLite3 database using the ocsql driver wrapper.
32+
db, err = sql.Open(driverName, "resource.db")
33+
```
34+
35+
If not wanting to use the named driver magic used by ocsql.Register, an
36+
alternative way to bootstrap the ocsql wrapper exists.
37+
38+
Example:
39+
```go
40+
import (
41+
"github.com/basvanbeek/ocsql"
42+
sqlite3 "github.com/mattn/go-sqlite3"
43+
)
44+
45+
var (
46+
driver driver.Driver
47+
err error
48+
db *sql.DB
49+
)
50+
51+
// Explicitly wrap the SQLite3 driver with ocsql
52+
driver = ocsql.Wrap(&sqlite3.SQLiteDriver{})
53+
54+
// Register our ocsql wrapper as a database driver
55+
sql.Register("ocsql-sqlite3", driver)
56+
57+
// Connect to a SQLite3 database using the ocsql driver wrapper
58+
db, err = sql.Open("ocsql-sqlite3", "resource.db")
59+
```
60+
61+
## context
62+
63+
To really take advantage of ocsql, all database calls should be made using the
64+
*Context methods. Failing to do so will result in many orphaned ocsql traces
65+
if the `AllowRoot` TraceOption is set to true. By default AllowRoot is disabled
66+
and will result in ocsql not tracing the database calls if context or parent
67+
spans are missing.
68+
69+
| Old | New |
70+
|----------------|-----------------------|
71+
| *DB.Begin | *DB.BeginTx |
72+
| *DB.Exec | *DB.ExecContext |
73+
| *DB.Ping | *DB.PingContext |
74+
| *DB.Prepare | *DB.PrepareContext |
75+
| *DB.Query | *DB.QueryContext |
76+
| *DB.QueryRow | *DB.QueryRowContext |
77+
| | |
78+
| *Stmt.Exec | *Stmt.ExecContext |
79+
| *Stmt.Query | *Stmt.QueryContext |
80+
| *Stmt.QueryRow | *Stmt.QueryRowContext |
81+
| | |
82+
| *Tx.Exec | *Tx.ExecContext |
83+
| *Tx.Prepare | *Tx.PrepareContext |
84+
| *Tx.Query | *Tx.QueryContext |
85+
| *Tx.QueryRow | *Tx.QueryRowContext |
86+
87+
Example:
88+
```go
89+
90+
func (s *svc) GetDevice(ctx context.Context, id int) (*Device, error) {
91+
// assume we have instrumented our service transports and ctx holds a span.
92+
var device Device
93+
if err := s.db.QueryRowContext(
94+
ctx, "SELECT * FROM device WHERE id = ?", id,
95+
).Scan(&device); err != nil {
96+
return nil, err
97+
}
98+
return device
99+
}
100+
```

driver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var (
2222
// Register initializes and registers our ocsql wrapped database driver
2323
// identified by its driverName and using provided TraceOptions. On success it
2424
// returns the generated driverName to use when calling sql.Open.
25+
// It is possible to register multiple wrappers for the same database driver if
26+
// needing different TraceOptions for different connections.
2527
func Register(driverName string, options ...TraceOption) (string, error) {
2628
// retrieve the driver implementation we need to wrap with instrumentation
2729
db, err := sql.Open(driverName, "")
@@ -38,7 +40,7 @@ func Register(driverName string, options ...TraceOption) (string, error) {
3840

3941
// Since we might want to register multiple ocsql drivers to have different
4042
// TraceOptions, but potentially the same underlying database driver, we
41-
// cycle through available driver names.
43+
// cycle through to find available driver names.
4244
driverName = driverName + "-ocsql-"
4345
for i := int64(0); i < 100; i++ {
4446
var (

options.go

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
package ocsql
22

3-
// TraceOption allows for functional options.
3+
// TraceOption allows for managing ocsql configuration using functional options.
44
type TraceOption func(o *TraceOptions)
55

66
// TraceOptions holds configuration of our ocsql tracing middleware.
7+
// By default all options are set to false intentionally when creating a wrapped
8+
// driver and provide the most sensible default with both performance and
9+
// security in mind.
710
type TraceOptions struct {
8-
AllowRoot bool
9-
Transaction bool
10-
Ping bool
11-
RowsNext bool
12-
RowsClose bool
11+
// AllowRoot, if set to true, will allow ocsql to create root spans in
12+
// absence of exisiting spans or even context.
13+
// Default is to not trace ocsql calls if no existing parent span is found
14+
// in context or when using methods not taking context.
15+
AllowRoot bool
16+
17+
// Transaction, if set to true, will create spans for the duration of db
18+
// transactions. All spans created by the transaction's scoped queries will
19+
// become children of the transaction span.
20+
Transaction bool
21+
22+
// Ping, if set to true, will enable the creation of spans on Ping requests.
23+
Ping bool
24+
25+
// RowsNext, if set to true, will enable the creation of spans on RowsNext
26+
// calls. This can result in many spans.
27+
RowsNext bool
28+
29+
// RowsClose, if set to true, will enable the creation of spans on RowsClose
30+
// calls.
31+
RowsClose bool
32+
33+
// RowsAffected, if set to true, will enable the creation of spans on
34+
// RowsAffected calls.
1335
RowsAffected bool
36+
37+
// LastInsertID, if set to true, will enable the creation of spans on
38+
// LastInsertId calls.
1439
LastInsertID bool
15-
Query bool
16-
QueryParams bool
40+
41+
// Query, if set to true, will enable recording of sql queries in spans.
42+
// Only allow this if it is safe to have queries recorded with respect to
43+
// security.
44+
Query bool
45+
46+
// QueryParams, if set to true, will enable recording of parameters used
47+
// with parametrized queries. Only allow this if it is safe to have
48+
// parameters recorded with respect to security.
49+
// This setting is a noop if the Query option is set to false.
50+
QueryParams bool
1751
}
1852

1953
// TraceAll has all tracing options enabled.
@@ -37,65 +71,77 @@ func WithOptions(options TraceOptions) TraceOption {
3771
}
3872
}
3973

40-
// WithAllowRoot when set to true will allow ocsql to create root spans. If
41-
// no context is provided to (the majority) of database/sql commands this will
42-
// result in many single span traces.
74+
// WithAllowRoot if set to true, will allow ocsql to create root spans in
75+
// absence of exisiting spans or even context.
76+
// Default is to not trace ocsql calls if no existing parent span is found
77+
// in context or when using methods not taking context.
4378
func WithAllowRoot(b bool) TraceOption {
4479
return func(o *TraceOptions) {
4580
o.AllowRoot = b
4681
}
4782
}
4883

49-
// WithTransaction enables / disables creation of transaction spans.
84+
// WithTransaction if set to true, will create spans for the duration of db
85+
// transactions. All spans created by the transaction's scoped queries will
86+
// become children of the transaction span.
5087
func WithTransaction(b bool) TraceOption {
5188
return func(o *TraceOptions) {
5289
o.Transaction = b
5390
}
5491
}
5592

56-
// WithPing enables / disables tracing of database Ping calls.
93+
// WithPing if set to true, will enable the creation of spans on Ping requests.
5794
func WithPing(b bool) TraceOption {
5895
return func(o *TraceOptions) {
5996
o.Ping = b
6097
}
6198
}
6299

63-
// WithRowsNext enables / disables tracing of database Rows.Next calls.
100+
// WithRowsNext if set to true, will enable the creation of spans on RowsNext
101+
// calls. This can result in many spans.
64102
func WithRowsNext(b bool) TraceOption {
65103
return func(o *TraceOptions) {
66104
o.RowsNext = b
67105
}
68106
}
69107

70-
// WithRowsClose enables / disables tracing of database Rows.Close calls.
108+
// WithRowsClose if set to true, will enable the creation of spans on RowsClose
109+
// calls.
71110
func WithRowsClose(b bool) TraceOption {
72111
return func(o *TraceOptions) {
73112
o.RowsClose = b
74113
}
75114
}
76115

77-
// WithRowsAffected enables / disables tracing of database Result.RowsAffected calls.
116+
// WithRowsAffected if set to true, will enable the creation of spans on
117+
// RowsAffected calls.
78118
func WithRowsAffected(b bool) TraceOption {
79119
return func(o *TraceOptions) {
80120
o.RowsAffected = b
81121
}
82122
}
83123

84-
// WithLastInsertID enables / disables tracing of database Result.LastInsertId calls.
124+
// WithLastInsertID if set to true, will enable the creation of spans on
125+
// LastInsertId calls.
85126
func WithLastInsertID(b bool) TraceOption {
86127
return func(o *TraceOptions) {
87128
o.LastInsertID = b
88129
}
89130
}
90131

91-
// WithQuery enables / disables annotating of database sql queries.
132+
// WithQuery if set to true, will enable recording of sql queries in spans.
133+
// Only allow this if it is safe to have queries recorded with respect to
134+
// security.
92135
func WithQuery(b bool) TraceOption {
93136
return func(o *TraceOptions) {
94137
o.Query = b
95138
}
96139
}
97140

98-
// WithQueryParams enables / disables tracing of database query parameters.
141+
// WithQueryParams if set to true, will enable recording of parameters used
142+
// with parametrized queries. Only allow this if it is safe to have
143+
// parameters recorded with respect to security.
144+
// This setting is a noop if the Query option is set to false.
99145
func WithQueryParams(b bool) TraceOption {
100146
return func(o *TraceOptions) {
101147
o.QueryParams = b

0 commit comments

Comments
 (0)