Skip to content

Commit fe5f218

Browse files
committed
chore: renamed trace options to be more intuitive.
1 parent c6d4fcf commit fe5f218

3 files changed

Lines changed: 105 additions & 34 deletions

File tree

README.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,79 @@
11
# Zipkin instrumentation SQL
22

3-
A sql wrapper including Zipkin instrumentation
3+
[![Build Status](https://travis-ci.com/jcchavezs/zipkin-instrumentation-sql.svg?branch=master)](https://travis-ci.com/jcchavezs/zipkin-instrumentation-sql)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/jcchavezs/zipkin-instrumentation-sql)](https://goreportcard.com/report/github.com/jcchavezs/zipkin-instrumentation-sql)
5+
[![GoDoc](https://godoc.org/github.com/jcchavezs/zipkin-instrumentation-sql?status.svg)](https://godoc.org/github.com/jcchavezs/zipkin-instrumentation-sql)
6+
[![Sourcegraph](https://sourcegraph.com/github.com/jcchavezs/zipkin-instrumentation-sql/-/badge.svg)](https://sourcegraph.com/github.com/jcchavezs/zipkin-instrumentation-sql?badge)
7+
8+
A SQL wrapper including Zipkin instrumentation
49

510
## Usage
611

712
```go
813
import (
914
_ "github.com/go-sql-driver/mysql"
1015
zipkinsql "github.com/jcchavezs/zipkin-instrumentation-sql"
16+
zipkin "github.com/openzipkin/zipkin-go"
1117
)
1218

1319
var (
1420
driverName string
1521
err error
1622
db *sql.DB
23+
tracer *zipkin.Tracer
1724
)
1825

1926
// Register our zipkinsql wrapper for the provided MySQL driver.
20-
driverName, err = zipkinsql.Register("mysql", zipkinsql.WithAllTraceOptions())
27+
driverName, err = zipkinsql.Register("mysql", tracer, zipkinsql.WithAllTraceOptions())
2128
if err != nil {
2229
log.Fatalf("unable to register zipkin driver: %v\n", err)
2330
}
2431

25-
// Connect to a MySQL database using the ocsql driver wrapper.
32+
// Connect to a MySQL database using the zipkinsql driver wrapper.
2633
db, err = sql.Open(driverName, "myDSN")
27-
```
34+
```
35+
36+
You can also wrap your own driver with zipkin instrumentation as follows:
37+
38+
```go
39+
40+
import (
41+
mysql "github.com/go-sql-driver/mysql"
42+
"github.com/opencensus-integrations/zipkinsql"
43+
)
44+
45+
var (
46+
driver driver.Driver
47+
err error
48+
db *sql.DB
49+
tracer *zipkin.Tracer
50+
)
51+
52+
// Explicitly wrap the MySQL driver with zipkinsql
53+
driver = zipkinsql.Wrap(&mysql.MySQLDriver{}, tracer)
54+
55+
// Register our zipkinsql wrapper as a database driver
56+
sql.Register("zipkinsql-mysql", driver)
57+
58+
// Connect to a MySQL database using the zipkinsql driver wrapper
59+
db, err = sql.Open("zipkinsql-mysql", "myDSN")
60+
```
61+
62+
## Usage of *Context methods
63+
64+
Instrumentation is possible if the context is being passed downstream in methods.
65+
This is not only for instrumentation purposes but also a [good practice](https://medium.com/@cep21/how-to-correctly-use-context-context-in-go-1-7-8f2c0fafdf39) in go programming. `database/sql` package exposes already a set of methods that receive the context as first paramenter:
66+
67+
- `*DB.Begin` -> `*DB.BeginTx`
68+
- `*DB.Exec` -> `*DB.ExecContext`
69+
- `*DB.Ping` -> `*DB.PingContext`
70+
- `*DB.Prepare` -> `*DB.PrepareContext`
71+
- `*DB.Query` -> `*DB.QueryContext`
72+
- `*DB.QueryRow` -> `*DB.QueryRowContext`
73+
- `*Stmt.Exec` -> `*Stmt.ExecContext`
74+
- `*Stmt.Query` -> `*Stmt.QueryContext`
75+
- `*Stmt.QueryRow` -> `*Stmt.QueryRowContext`
76+
- `*Tx.Exec` -> `*Tx.ExecContext`
77+
- `*Tx.Prepare` -> `*Tx.PrepareContext`
78+
- `*Tx.Query` -> `*Tx.QueryContext`
79+
- `*Tx.QueryRow` -> `*Tx.QueryRowContext`

driver.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (c zConn) ExecContext(ctx context.Context, query string, args []driver.Name
175175
span, _ := c.tracer.StartSpanFromContext(ctx, "sql/exec")
176176
defer span.Finish()
177177

178-
if c.options.Query {
178+
if c.options.TagQuery {
179179
span.Tag("sql.query", query)
180180
}
181181

@@ -209,7 +209,7 @@ func (c zConn) QueryContext(ctx context.Context, query string, args []driver.Nam
209209
span, _ := c.tracer.StartSpanFromContext(ctx, "sql/exec")
210210
defer span.Finish()
211211

212-
if c.options.Query {
212+
if c.options.TagQuery {
213213
span.Tag("sql.query", query)
214214
}
215215

@@ -293,7 +293,7 @@ type zResult struct {
293293
}
294294

295295
func (r zResult) LastInsertId() (int64, error) {
296-
if !r.options.LastInsertID {
296+
if !r.options.LastInsertIDSpan {
297297
return r.driver.LastInsertId()
298298
}
299299

@@ -310,7 +310,7 @@ func (r zResult) LastInsertId() (int64, error) {
310310

311311
func (r zResult) RowsAffected() (cnt int64, err error) {
312312
zipkin.SpanFromContext(r.ctx)
313-
if r.options.RowsAffected && zipkin.SpanFromContext(r.ctx) != nil {
313+
if r.options.RowsAffectedSpan && zipkin.SpanFromContext(r.ctx) != nil {
314314
span, _ := r.tracer.StartSpanFromContext(r.ctx, "sql/rows_affected")
315315
setSpanDefaultTags(span, r.options.DefaultTags)
316316
defer func() {
@@ -359,7 +359,7 @@ func (s zStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res d
359359
span.Finish()
360360
}()
361361

362-
if s.options.Query {
362+
if s.options.TagQuery {
363363
span.Tag("sql.query", s.query)
364364
}
365365

@@ -370,6 +370,13 @@ func (s zStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res d
370370
if err != nil {
371371
return nil, err
372372
}
373+
374+
if s.options.TagAffectedRows {
375+
if affectedRows, err := res.RowsAffected(); err != nil {
376+
span.Tag("sql.affected_rows", fmt.Sprintf("%d", affectedRows))
377+
}
378+
}
379+
373380
res, err = zResult{driver: res, ctx: ctx, options: s.options}, nil
374381
return
375382
}
@@ -385,7 +392,7 @@ func (s zStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows
385392
span.Finish()
386393
}()
387394

388-
if s.options.Query {
395+
if s.options.TagQuery {
389396
span.Tag("sql.query", s.query)
390397
}
391398

@@ -402,6 +409,7 @@ func (s zStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows
402409
if err != nil {
403410
return nil, err
404411
}
412+
405413
rows, err = zRows{driver: rows, ctx: ctx, options: s.options}, nil
406414
return
407415
}

options.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ package zipkinsql
44
type TraceOption func(o *TraceOptions)
55

66
// TraceOptions holds configuration of our zipkinsql 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.
7+
// By default all boolean options are set to false intentionally when creating
8+
// a wrapped driver and provide the most sensible default with both performance
9+
// and security in mind.
1010
type TraceOptions struct {
11-
// RowsAffected, if set to true, will enable the creation of spans on
12-
// RowsAffected calls.
13-
RowsAffected bool
11+
// LastInsertIDSpan, if set to true, will enable the creation of spans on
12+
// LastInsertId calls.
13+
LastInsertIDSpan bool
14+
15+
// RowsAffectedSpan, if set to true, will enable the creation of spans on
16+
// RowsAffectedSpan calls.
17+
RowsAffectedSpan bool
1418

15-
// Query, if set to true, will enable recording of sql queries in spans.
19+
// TagQuery, if set to true, will enable recording of sql queries in spans.
1620
// Only allow this if it is safe to have queries recorded with respect to
1721
// security.
18-
Query bool
22+
TagQuery bool
1923

20-
// LastInsertID, if set to true, will enable the creation of spans on
21-
// LastInsertId calls.
22-
LastInsertID bool
24+
TagAffectedRows bool
2325

2426
// DefaultTags will be set to each span as default.
2527
DefaultTags map[string]string
@@ -34,41 +36,50 @@ func WithAllTraceOptions() TraceOption {
3436

3537
// AllTraceOptions has all tracing options enabled.
3638
var AllTraceOptions = TraceOptions{
37-
RowsAffected: true,
38-
Query: true,
39-
LastInsertID: true,
39+
RowsAffectedSpan: true,
40+
LastInsertIDSpan: true,
41+
TagQuery: true,
42+
TagAffectedRows: true,
4043
}
4144

42-
// WithOptions sets our ocsql tracing middleware options through a single
45+
// WithOptions sets the zipkinsql tracing middleware options through a single
4346
// TraceOptions object.
4447
func WithOptions(options TraceOptions) TraceOption {
4548
return func(o *TraceOptions) {
4649
*o = options
4750
}
4851
}
4952

50-
// WithRowsAffected if set to true, will enable the creation of spans on
53+
// WithRowsAffectedSpan if set to true, will enable the creation of spans on
5154
// RowsAffected calls.
52-
func WithRowsAffected(b bool) TraceOption {
55+
func WithRowsAffectedSpan(b bool) TraceOption {
5356
return func(o *TraceOptions) {
54-
o.RowsAffected = b
57+
o.RowsAffectedSpan = b
5558
}
5659
}
5760

58-
// WithLastInsertID if set to true, will enable the creation of spans on
61+
// WithLastInsertIDSpan if set to true, will enable the creation of spans on
5962
// LastInsertId calls.
60-
func WithLastInsertID(b bool) TraceOption {
63+
func WithLastInsertIDSpan(b bool) TraceOption {
6164
return func(o *TraceOptions) {
62-
o.LastInsertID = b
65+
o.LastInsertIDSpan = b
6366
}
6467
}
6568

66-
// WithQuery if set to true, will enable recording of sql queries in spans.
69+
// WithTagQuery if set to true, will enable recording of SQL queries in spans.
6770
// Only allow this if it is safe to have queries recorded with respect to
6871
// security.
69-
func WithQuery(b bool) TraceOption {
72+
func WithTagQuery(b bool) TraceOption {
73+
return func(o *TraceOptions) {
74+
o.TagQuery = b
75+
}
76+
}
77+
78+
// WithTagAffectedRows if set to true, will enable recording of the affected rows
79+
// number in spans.
80+
func WithTagAffectedRows(b bool) TraceOption {
7081
return func(o *TraceOptions) {
71-
o.Query = b
82+
o.TagAffectedRows = b
7283
}
7384
}
7485

0 commit comments

Comments
 (0)