Skip to content

Commit 24b349c

Browse files
committed
test: adds tests for Tx and propagation.
1 parent 79cca89 commit 24b349c

2 files changed

Lines changed: 282 additions & 65 deletions

File tree

driver.go

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ var (
2727
_ driver.Driver = &zDriver{}
2828
_ conn = &zConn{}
2929
_ driver.Result = &zResult{}
30-
_ driver.Rows = &zRows{}
3130
)
3231

3332
var (
@@ -79,11 +78,11 @@ func wrapDriver(d driver.Driver, t *zipkin.Tracer, o TraceOptions) driver.Driver
7978
}
8079

8180
func wrapConn(c driver.Conn, t *zipkin.Tracer, options TraceOptions) driver.Conn {
82-
return &zConn{driver: c, tracer: t, options: options}
81+
return &zConn{conn: c, tracer: t, options: options}
8382
}
8483

8584
func wrapStmt(stmt driver.Stmt, query string, tracer *zipkin.Tracer, options TraceOptions) driver.Stmt {
86-
s := zStmt{driver: stmt, query: query, options: options, tracer: tracer}
85+
s := zStmt{stmt: stmt, query: query, options: options, tracer: tracer}
8786
_, hasExeCtx := stmt.(driver.StmtExecContext)
8887
_, hasQryCtx := stmt.(driver.StmtQueryContext)
8988
c, hasColCnv := stmt.(driver.ColumnConverter)
@@ -147,28 +146,28 @@ func (d zDriver) Open(name string) (driver.Conn, error) {
147146

148147
// zConn implements driver.Conn
149148
type zConn struct {
150-
driver driver.Conn
149+
conn driver.Conn
151150
tracer *zipkin.Tracer
152151
options TraceOptions
153152
}
154153

155154
func (c zConn) Ping(ctx context.Context) (err error) {
156-
if pinger, ok := c.driver.(driver.Pinger); ok {
155+
if pinger, ok := c.conn.(driver.Pinger); ok {
157156
err = pinger.Ping(ctx)
158157
}
159158
return
160159
}
161160

162161
func (c zConn) Exec(query string, args []driver.Value) (res driver.Result, err error) {
163-
if exec, ok := c.driver.(driver.Execer); ok {
162+
if exec, ok := c.conn.(driver.Execer); ok {
164163
return exec.Exec(query, args)
165164
}
166165

167166
return nil, driver.ErrSkip
168167
}
169168

170169
func (c zConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (res driver.Result, err error) {
171-
if execCtx, ok := c.driver.(driver.ExecerContext); ok {
170+
if execCtx, ok := c.conn.(driver.ExecerContext); ok {
172171
if zipkin.SpanFromContext(ctx) == nil && !c.options.AllowRootSpan {
173172
return execCtx.ExecContext(ctx, query, args)
174173
}
@@ -187,22 +186,22 @@ func (c zConn) ExecContext(ctx context.Context, query string, args []driver.Name
187186
return nil, err
188187
}
189188

190-
return zResult{driver: res, tracer: c.tracer, ctx: ctx, options: c.options}, nil
189+
return zResult{result: res, tracer: c.tracer, ctx: ctx, options: c.options}, nil
191190
}
192191

193192
return nil, driver.ErrSkip
194193
}
195194

196195
func (c zConn) Query(query string, args []driver.Value) (rows driver.Rows, err error) {
197-
if queryer, ok := c.driver.(driver.Queryer); ok {
196+
if queryer, ok := c.conn.(driver.Queryer); ok {
198197
return queryer.Query(query, args)
199198
}
200199

201200
return nil, driver.ErrSkip
202201
}
203202

204203
func (c zConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) {
205-
if queryerCtx, ok := c.driver.(driver.QueryerContext); ok {
204+
if queryerCtx, ok := c.conn.(driver.QueryerContext); ok {
206205
if zipkin.SpanFromContext(ctx) == nil && !c.options.AllowRootSpan {
207206
return queryerCtx.QueryContext(ctx, query, args)
208207
}
@@ -221,14 +220,14 @@ func (c zConn) QueryContext(ctx context.Context, query string, args []driver.Nam
221220
return nil, err
222221
}
223222

224-
return zRows{driver: rows, ctx: ctx, options: c.options}, nil
223+
return rows, nil
225224
}
226225

227226
return nil, driver.ErrSkip
228227
}
229228

230229
func (c zConn) Prepare(query string) (stmt driver.Stmt, err error) {
231-
stmt, err = c.driver.Prepare(query)
230+
stmt, err = c.conn.Prepare(query)
232231
if err != nil {
233232
return nil, err
234233
}
@@ -238,72 +237,72 @@ func (c zConn) Prepare(query string) (stmt driver.Stmt, err error) {
238237
}
239238

240239
func (c *zConn) Close() error {
241-
return c.driver.Close()
240+
return c.conn.Close()
242241
}
243242

244243
func (c *zConn) Begin() (driver.Tx, error) {
245244
return c.Begin()
246245
}
247246

248247
func (c *zConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
249-
if prepCtx, ok := c.driver.(driver.ConnPrepareContext); ok {
248+
if prepCtx, ok := c.conn.(driver.ConnPrepareContext); ok {
250249
return prepCtx.PrepareContext(ctx, query)
251250
}
252251

253-
return c.driver.Prepare(query)
252+
return c.conn.Prepare(query)
254253
}
255254

256255
func (c *zConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
257256
if zipkin.SpanFromContext(ctx) == nil && !c.options.AllowRootSpan {
258-
if connBeginTx, ok := c.driver.(driver.ConnBeginTx); ok {
257+
if connBeginTx, ok := c.conn.(driver.ConnBeginTx); ok {
259258
return connBeginTx.BeginTx(ctx, opts)
260259
}
261260

262-
return c.driver.Begin()
261+
return c.conn.Begin()
263262
}
264263

265264
span, _ := c.tracer.StartSpanFromContext(ctx, "sql/begin_transaction", zipkin.Kind(zipkinmodel.Client))
266265
defer span.Finish()
267266

268267
setSpanDefaultTags(span, c.options.DefaultTags)
269268

270-
if connBeginTx, ok := c.driver.(driver.ConnBeginTx); ok {
269+
if connBeginTx, ok := c.conn.(driver.ConnBeginTx); ok {
271270
tx, err := connBeginTx.BeginTx(ctx, opts)
272271
setSpanError(span, err)
273272
if err != nil {
274273
return nil, err
275274
}
276-
return zTx{driver: tx, ctx: ctx}, nil
275+
return zTx{tx: tx, ctx: ctx, tracer: c.tracer, options: c.options}, nil
277276
}
278277

279-
tx, err := c.driver.Begin()
278+
tx, err := c.conn.Begin()
280279
setSpanError(span, err)
281280
if err != nil {
282281
return nil, err
283282
}
284283

285-
return zTx{driver: tx, ctx: ctx, tracer: c.tracer}, nil
284+
return zTx{tx: tx, ctx: ctx, tracer: c.tracer, options: c.options}, nil
286285
}
287286

288287
// zResult implements driver.Result
289288
type zResult struct {
290-
driver driver.Result
289+
result driver.Result
291290
ctx context.Context
292291
tracer *zipkin.Tracer
293292
options TraceOptions
294293
}
295294

296295
func (r zResult) LastInsertId() (int64, error) {
297296
if !r.options.LastInsertIDSpan {
298-
return r.driver.LastInsertId()
297+
return r.result.LastInsertId()
299298
}
300299

301300
span, _ := r.tracer.StartSpanFromContext(r.ctx, "sql/last_insert_id", zipkin.Kind(zipkinmodel.Client))
302301
defer span.Finish()
303302

304303
setSpanDefaultTags(span, r.options.DefaultTags)
305304

306-
id, err := r.driver.LastInsertId()
305+
id, err := r.result.LastInsertId()
307306
setSpanError(span, err)
308307

309308
return id, err
@@ -321,37 +320,80 @@ func (r zResult) RowsAffected() (cnt int64, err error) {
321320
}()
322321
}
323322

324-
cnt, err = r.driver.RowsAffected()
323+
cnt, err = r.result.RowsAffected()
325324
return
326325
}
327326

328327
// zStmt implements driver.Stmt
329328
type zStmt struct {
330-
driver driver.Stmt
329+
stmt driver.Stmt
331330
query string
332331
tracer *zipkin.Tracer
333332
options TraceOptions
334333
}
335334

336-
func (s zStmt) Exec(args []driver.Value) (driver.Result, error) {
337-
return s.driver.Exec(args)
335+
func (s zStmt) Exec(args []driver.Value) (res driver.Result, err error) {
336+
if !s.options.AllowRootSpan {
337+
return s.stmt.Exec(args)
338+
}
339+
340+
span, ctx := s.tracer.StartSpanFromContext(context.Background(), "sql:exec", zipkin.Kind(zipkinmodel.Client))
341+
setSpanDefaultTags(span, s.options.DefaultTags)
342+
343+
if s.options.TagQuery {
344+
span.Tag("sql.query", s.query)
345+
}
346+
347+
defer func() {
348+
setSpanError(span, err)
349+
span.Finish()
350+
}()
351+
352+
res, err = s.stmt.Exec(args)
353+
if err != nil {
354+
return nil, err
355+
}
356+
357+
res, err = zResult{result: res, ctx: ctx, tracer: s.tracer, options: s.options}, nil
358+
return
338359
}
339360

340361
func (s zStmt) Close() error {
341-
return s.driver.Close()
362+
return s.stmt.Close()
342363
}
343364

344365
func (s zStmt) NumInput() int {
345-
return s.driver.NumInput()
366+
return s.stmt.NumInput()
346367
}
347368

348-
func (s zStmt) Query(args []driver.Value) (driver.Rows, error) {
349-
return s.driver.Query(args)
369+
func (s zStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
370+
if !s.options.AllowRootSpan {
371+
return s.stmt.Query(args)
372+
}
373+
374+
span, _ := s.tracer.StartSpanFromContext(context.Background(), "sql:query", zipkin.Kind(zipkinmodel.Client))
375+
setSpanDefaultTags(span, s.options.DefaultTags)
376+
377+
if s.options.TagQuery {
378+
span.Tag("sql.query", s.query)
379+
}
380+
381+
defer func() {
382+
setSpanError(span, err)
383+
span.Finish()
384+
}()
385+
386+
rows, err = s.stmt.Query(args)
387+
if err != nil {
388+
return nil, err
389+
}
390+
391+
return
350392
}
351393

352394
func (s zStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
353395
if zipkin.SpanFromContext(ctx) == nil && !s.options.AllowRootSpan {
354-
return s.driver.(driver.StmtExecContext).ExecContext(ctx, args)
396+
return s.stmt.(driver.StmtExecContext).ExecContext(ctx, args)
355397
}
356398

357399
span, ctx := s.tracer.StartSpanFromContext(ctx, "sql/exec", zipkin.Kind(zipkinmodel.Client))
@@ -366,7 +408,7 @@ func (s zStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res d
366408

367409
setSpanDefaultTags(span, s.options.DefaultTags)
368410

369-
execContext := s.driver.(driver.StmtExecContext)
411+
execContext := s.stmt.(driver.StmtExecContext)
370412
res, err = execContext.ExecContext(ctx, args)
371413
if err != nil {
372414
return nil, err
@@ -378,13 +420,13 @@ func (s zStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res d
378420
}
379421
}
380422

381-
res, err = zResult{driver: res, tracer: s.tracer, ctx: ctx, options: s.options}, nil
423+
res, err = zResult{result: res, tracer: s.tracer, ctx: ctx, options: s.options}, nil
382424
return
383425
}
384426

385427
func (s zStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
386428
if zipkin.SpanFromContext(ctx) == nil && !s.options.AllowRootSpan {
387-
return s.driver.(driver.StmtQueryContext).QueryContext(ctx, args)
429+
return s.stmt.(driver.StmtQueryContext).QueryContext(ctx, args)
388430
}
389431

390432
span, ctx := s.tracer.StartSpanFromContext(ctx, "sql/query", zipkin.Kind(zipkinmodel.Client))
@@ -405,38 +447,18 @@ func (s zStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows
405447
}()
406448

407449
// we already tested driver to implement StmtQueryContext
408-
queryContext := s.driver.(driver.StmtQueryContext)
450+
queryContext := s.stmt.(driver.StmtQueryContext)
409451
rows, err = queryContext.QueryContext(ctx, args)
410452
if err != nil {
411453
return nil, err
412454
}
413455

414-
rows, err = zRows{driver: rows, ctx: ctx, options: s.options}, nil
415456
return
416457
}
417458

418-
// zRows implements driver.Rows.
419-
type zRows struct {
420-
driver driver.Rows
421-
ctx context.Context
422-
options TraceOptions
423-
}
424-
425-
func (r zRows) Columns() []string {
426-
return r.driver.Columns()
427-
}
428-
429-
func (r zRows) Close() error {
430-
return r.driver.Close()
431-
}
432-
433-
func (r zRows) Next(dest []driver.Value) error {
434-
return r.driver.Next(dest)
435-
}
436-
437459
// zTx implemens driver.Tx
438460
type zTx struct {
439-
driver driver.Tx
461+
tx driver.Tx
440462
ctx context.Context
441463
tracer *zipkin.Tracer
442464
options TraceOptions
@@ -451,7 +473,7 @@ func (t zTx) Commit() (err error) {
451473
span.Finish()
452474
}()
453475
}
454-
err = t.driver.Commit()
476+
err = t.tx.Commit()
455477
return
456478
}
457479

@@ -464,7 +486,7 @@ func (t zTx) Rollback() (err error) {
464486
span.Finish()
465487
}()
466488
}
467-
err = t.driver.Rollback()
489+
err = t.tx.Rollback()
468490
return
469491
}
470492

0 commit comments

Comments
 (0)