Commit 2ae012e
committed
# feat: add QueryTracer interface for SQL statement tracing (#1716)
## Summary
Add a `QueryTracer` interface that allows users to trace SQL query execution for logging, metrics, or distributed tracing. This feature is inspired by the pgx driver's tracelog implementation.
## Motivation
Applications often need visibility into database query execution for:
- **Debugging**: Log slow queries and identify bottlenecks
- **Metrics**: Track query execution times, error rates, and throughput
- **Distributed Tracing**: Integrate with observability tools (OpenTelemetry, Jaeger, etc.) using context propagation
- **Compliance**: Audit logging for data access
## Implementation
### New Interface (tracer.go)
Defines the `QueryTracer` interface with two methods:
```go
type QueryTracer interface {
TraceQueryStart(ctx context.Context, query string, args []driver.NamedValue) context.Context
TraceQueryEnd(ctx context.Context, err error, duration time.Duration)
}
```
- `TraceQueryStart` is called before query execution with the query string and arguments, returning a context for span propagation
- `TraceQueryEnd` is called after query completion with the error and wall-clock duration
The `mysqlConn.traceQuery()` helper wraps query execution with automatic tracing. When no tracer is configured, overhead is a single nil check per query.
### Configuration (dsn.go)
- Added `tracer QueryTracer` field to `Config` struct
- Added `WithTracer(tracer QueryTracer)` functional option
### Instrumented Paths (connection.go, statement.go)
- `ExecContext`
- `QueryContext`
- `PrepareContext`
- `mysqlStmt.ExecContext`
- `mysqlStmt.QueryContext`
## Usage Example
```go
package main
import (
"context"
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
)
type DebugTracer struct{}
func (t *DebugTracer) TraceQueryStart(ctx context.Context, query string, args []driver.NamedValue) context.Context {
fmt.Printf("[QUERY START] %s | args: %v\n", query, args)
return ctx
}
func (t *DebugTracer) TraceQueryEnd(ctx context.Context, err error, duration time.Duration) {
fmt.Printf("[QUERY END] duration: %v | error: %v\n", duration, err)
}
func main() {
config := mysql.NewConfig()
config.User = "root"
config.Net = "tcp"
config.Addr = "127.0.0.1:3306"
config.DBName = "test"
config.Tracer = &DebugTracer{}
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
panic(err)
}
defer db.Close()
var result string
err = db.QueryRowContext(context.Background(), "SELECT 'Hello, MySQL!'").Scan(&result)
if err != nil {
panic(err)
}
fmt.Println("Result:", result)
}
```
### OpenTelemetry Integration Example
```go
type OTELTracer struct {
tracer trace.Tracer
}
func (t *OTELTracer) TraceQueryStart(ctx context.Context, query string, args []driver.NamedValue) context.Context {
ctx, span := t.tracer.Start(ctx, "mysql.query",
trace.WithAttributes(
attribute.String("db.statement", query),
attribute.Int("db.args_count", len(args)),
),
)
return ctx
}
func (t *OTELTracer) TraceQueryEnd(ctx context.Context, err error, duration time.Duration) {
span := trace.SpanFromContext(ctx)
defer span.End()
span.SetAttributes(
attribute.Int64("db.duration_ms", duration.Milliseconds()),
)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}
```
## How to Test
### Run Unit Tests
```bash
# Run all tracer tests
go test -v -run Trace
# Run specific test
go test -v -run TestTraceQuery_WithTracer
```
### Test with a Real MySQL Instance
```bash
# Start MySQL (using Docker)
docker run --name mysql-test \
-e MYSQL_ROOT_PASSWORD=secret \
-p 3306:3306 \
-d mysql:8.0
# Run integration tests
go test -v -tags=integration
```
### Manual Testing with DebugTracer
Create a test file `debug_test.go`:
```go
package main
import (
"context"
"database/sql"
"fmt"
"time"
_ "github.com/go-sql-driver/mysql"
)
type DebugTracer struct{}
func (t *DebugTracer) TraceQueryStart(ctx context.Context, query string, args []driver.NamedValue) context.Context {
fmt.Printf("\n=== Query Start ===\n")
fmt.Printf("Query: %s\n", query)
fmt.Printf("Args: %v\n", args)
return ctx
}
func (t *DebugTracer) TraceQueryEnd(ctx context.Context, err error, duration time.Duration) {
fmt.Printf("\n=== Query End ===\n")
fmt.Printf("Duration: %v\n", duration)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Printf("================\n")
}
func main() {
config := mysql.NewConfig()
config.User = "root"
config.Passwd = "secret"
config.Net = "tcp"
config.Addr = "127.0.0.1:3306"
config.Tracer = &DebugTracer{}
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
panic(err)
}
defer db.Close()
if err := db.Ping(); err != nil {
panic(err)
}
_, err = db.ExecContext(context.Background(), "CREATE DATABASE IF NOT EXISTS test")
if err != nil {
panic(err)
}
_, err = db.ExecContext(context.Background(), "CREATE TABLE IF NOT EXISTS test.users (id INT, name VARCHAR(100))")
if err != nil {
panic(err)
}
_, err = db.ExecContext(context.Background(), "INSERT INTO test.users VALUES (?, ?)", 1, "Alice")
if err != nil {
panic(err)
}
var name string
err = db.QueryRowContext(context.Background(), "SELECT name FROM test.users WHERE id = ?", 1).Scan(&name)
if err != nil {
panic(err)
}
fmt.Printf("Fetched name: %s\n", name)
}
```
Run it:
```bash
go run debug_test.go
```
Expected output:
```
=== Query Start ===
Query: SELECT name FROM test.users WHERE id = ?
Args: [1]
=== Query End ===
Duration: 12.345ms
================
Fetched name: Alice
```
## Performance
When no tracer is configured, overhead is a single nil check per query. The feature is designed to have minimal impact on performance when disabled.
Benchmark results:
```bash
go test -bench=. -benchmem
```
## Changes
### New Files
- `tracer.go` (39 lines)
- `tracer_test.go` (165 lines)
### Modified Files
- `connection.go` - Added tracing hooks around query execution
- `statement.go` - Added query string storage for prepared statements
- `dsn.go` - Extended Config to support tracer configuration
**Total: 5 files changed, 235 insertions, 2 deletions**
## Test Coverage
Comprehensive test coverage in `tracer_test.go`:
- `TestTraceQuery_WithTracer` - Validates tracer calls and context propagation
- `TestTraceQuery_ContextFlows` - Ensures context is correctly passed through
- `TestTraceQuery_WithError` - Tests error handling in trace callbacks
- `TestTraceQuery_NilTracer` - Verifies no-op behavior when tracer is nil
- `TestWithTracerOption` - Tests functional option configuration
## Breaking Changes
None. This is a pure addition to the public API.
## Checklist
- [x] Code compiles correctly
- [x] Created tests which fail without the change
- [x] All tests passing
- [ ] Extended the README / documentation, if necessary
- [ ] Added myself / the copyright holder to the AUTHORS file1 parent 76c00e3 commit 2ae012e
5 files changed
Lines changed: 235 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
555 | 555 | | |
556 | 556 | | |
557 | 557 | | |
| 558 | + | |
558 | 559 | | |
559 | 560 | | |
| 561 | + | |
560 | 562 | | |
561 | 563 | | |
562 | 564 | | |
| 565 | + | |
563 | 566 | | |
564 | 567 | | |
565 | 568 | | |
| |||
575 | 578 | | |
576 | 579 | | |
577 | 580 | | |
578 | | - | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
579 | 585 | | |
580 | 586 | | |
581 | 587 | | |
| |||
595 | 601 | | |
596 | 602 | | |
597 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
598 | 609 | | |
599 | 610 | | |
600 | 611 | | |
| |||
608 | 619 | | |
609 | 620 | | |
610 | 621 | | |
| 622 | + | |
611 | 623 | | |
612 | 624 | | |
| 625 | + | |
613 | 626 | | |
614 | 627 | | |
615 | 628 | | |
| 629 | + | |
616 | 630 | | |
617 | 631 | | |
618 | 632 | | |
| |||
628 | 642 | | |
629 | 643 | | |
630 | 644 | | |
631 | | - | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
632 | 649 | | |
633 | 650 | | |
634 | 651 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| |||
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
138 | 149 | | |
139 | 150 | | |
140 | 151 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
0 commit comments