feat: propagate trace context into SQL statements as comments#234
Merged
Conversation
Traces show which application code ran a query; the database's own
diagnostics (slow query logs, statement views) show statements with no
link back to the request that issued them. The sqlcommenter convention
bridges the two: the current trace context rides along as a statement
comment, so a slow-log entry correlates directly to the trace, and the
trace to the exact database-side execution.
- SqlCommenter SPI (storm-core): contributes per-execution comment
content, appended after all statement processing and caching,
immediately before the statement is prepared. Configured per template
via the builder in all three language surfaces. A commenter can
never alter the statement: content containing the comment terminator
or semicolons is rejected, and the emitted comment is padded so
MySQL/MariaDB executable-comment and optimizer-hint markers are never
interpreted. Observations, SQL capture, and logging keep seeing the
clean statement.
- TraceContextSqlCommenter (storm-micrometer): renders the current span
as a W3C traceparent value in sqlcommenter format
(traceparent='00-{traceId}-{spanId}-{flags}'); statements execute
uncommented when no span is current. micrometer-tracing is an
optional dependency.
- Opt-in activation: storm.tracing.sql-comments=true in the Spring Boot
starters (with a Tracer bean present, shared
StormTracingAutoConfiguration), the sqlCommenter slot on the Ktor
plugin and its database blocks. Off by default: a per-execution
comment changes the statement text on every call, which defeats
driver-side and server-side prepared statement caching.
Fixes #232
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.
Traces show which application code ran a query; the database's own diagnostics — slow query logs, statement views — show statements with no link back to the request that issued them. The sqlcommenter convention bridges the two: the current trace context rides along as a statement comment, so a slow-log entry correlates directly to the trace, and the trace to the exact database-side execution.
Fixes #232
What's included
SqlCommenterSPI (storm-core): contributes per-execution comment content, appended after all statement processing and caching, immediately before the statement is prepared — so Storm's template cache, SQL capture, observations, and logging all keep seeing the clean statement while the database sees the comment. Configured per template via the builder in all three language surfaces (core, Java, Kotlin).!or+as an executable comment or optimizer hint.TraceContextSqlCommenter(storm-micrometer): renders the current span as a W3Ctraceparentvalue in sqlcommenter format (traceparent='00-{traceId}-{spanId}-{flags}'); statements execute uncommented when no span is current.micrometer-tracingis an optional dependency.storm.tracing.sql-comments=truein the Spring Boot starters (with aTracerbean present, via the sharedStormTracingAutoConfiguration), and thesqlCommenterslot on the Ktor plugin and itsdatabase("...")blocks with plugin-level inheritance. Off by default: a per-execution comment changes the statement text on every call, which defeats driver-side and server-side prepared statement caching — the docs state the trade-off.Verification
!40101stays a plain comment);traceparentformat viaSimpleTracer.Tracerpresent, present with the property set.Docs: Observability sections of spring-integration.md and ktor-integration.md, the yaml examples, and the changelog entry with the caching trade-off and the hardening guarantees.