feat: add QueryTracer interface for SQL statement tracing (#1716)#1748
Draft
ljluestc wants to merge 1 commit intogo-sql-driver:masterfrom
Draft
feat: add QueryTracer interface for SQL statement tracing (#1716)#1748ljluestc wants to merge 1 commit intogo-sql-driver:masterfrom
ljluestc wants to merge 1 commit intogo-sql-driver:masterfrom
Conversation
…ver#1716) Add a QueryTracer interface that allows users to trace SQL query execution for logging, metrics, or distributed tracing. Inspired by pgx's tracelog. - QueryTracer.TraceQueryStart is called before query execution with the query string and arguments, returning a context for span propagation. - QueryTracer.TraceQueryEnd is called after completion with the error and wall-clock duration. Instrumented paths: ExecContext, QueryContext, PrepareContext, mysqlStmt.ExecContext, mysqlStmt.QueryContext. The tracer is configured via the WithTracer functional option. When no tracer is set, the overhead is a single nil check per query. Co-Authored-By: Warp <agent@warp.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The existing
Loggerinterface (cfg.Logger) only logs internal drivererrors (e.g., authentication failures, broken connections). There is no
way to observe actual SQL statements, parameters, errors, or timing
without wrapping
database/sqlexternally.Ref: #1716
Solution
A new
QueryTracerinterface with two methods:StartTraceis called before the query executes, receiving the queryand arguments. Returns a new context (e.g., for OpenTelemetry span propagation).
EndTraceis called after execution completes with the error and wall-clock duration.Usage
Instrumented code paths
(*mysql.Conn).ExecContext—connection.go(*mysql.Conn).QueryContext—connection.go(*mysql.Conn).PrepareContext—connection.go(*mysql.Stmt).ExecContext—connection.go(*mysql.Stmt).QueryContext—connection.goWhen no tracer is configured, the cost is a single
nilcheck per query (zero allocations).Files changed
tracer.go(new) —QueryTracerinterface andtracerQueryhelper methodtracer_test.go(new) — 5 unit testsdsn.go— addedtracerfield toConfig, addedWithTraceroption functionconnection.go— instrumentedExecContext,QueryContext,PrepareContext,Stmt.ExecContext,Stmt.QueryContextstmt.go— addedqueryStrfield tomysqlStmtfor tracing prepared statementsHow to test
1. Run the new unit tests
Expected:
2. Run the full test suite
go test -short ./... go vet ./...3. Manual integration test
With a local MySQL instance:
Each call prints the SQL and timing via the tracer.
Checklist
go build ./...)go test -short ./...)go vetpasses