• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-sql-driver / mysql / 23700483573 / 10
83%
master: 83%

Build:
Build:
LAST BUILD BRANCH: refs/tags/v1.10.0
DEFAULT BRANCH: master
Ran 01 Apr 2026 08:14AM UTC
Files 19
Run time 7s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

29 Mar 2026 02:55AM UTC coverage: 81.692% (-0.07%) from 81.759%
23700483573.10

Pull #1753

github

ljluestc
# 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, ... (continued)
Pull Request #1753: feat: add QueryTracer interface for SQL statement tracing (#1716)

3284 of 4020 relevant lines covered (81.69%)

73664.25 hits per line

Source Files on job Windows-Go-1.24-DB-mariadb-11.4 - 23700483573.10
  • Tree
  • List 19
  • Changed 5
  • Source Changed 0
  • Coverage Changed 5
Coverage ∆ File Lines Relevant Covered Missed Hits/Line
  • Back to Build 23700483573
  • 2ae012e4 on github
  • Prev Job for on private-query-tracer (#22184055321.20)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc