Repository navigation
feat: Add opt-in SQLCommenter W3C trace context propagation (fixes #6255) - #6664
Open
cloudsealed wants to merge 5 commits into
Open
cloudsealed wants to merge 5 commits into
cloudsealed wants to merge 5 commits into
Conversation
gfoidl
reviewed
Oct 4, 2026
| var traceparent = $"00-{activity.TraceId}-{activity.SpanId}-{(activity.Recorded ? "01" : "00")}"; | ||
| var comment = $"/*traceparent='{traceparent}'*/"; | ||
|
|
||
| return commandText.EndsWith(";") |
Contributor
There was a problem hiding this comment.
Suggested change
| return commandText.EndsWith(";") | |
| return commandText.EndsWith(';') |
use the char-overload.
| var comment = $"/*traceparent='{traceparent}'*/"; | ||
|
|
||
| return commandText.EndsWith(";") | ||
| ? commandText.Substring(0, commandText.Length - 1) + " " + comment + ";" |
Contributor
There was a problem hiding this comment.
Reduce allocations:
Suggested change
| ? commandText.Substring(0, commandText.Length - 1) + " " + comment + ";" | |
| ? $"{commandText.AsSpan(0, commandText.Length - 1)} {comment};" |
…rpolation to reduce allocations
…ests Only append the traceparent comment to unprepared statements. Prepared statements only send Bind (the comment never reached the server) and their FinalCommandText is not regenerated, so the comment accumulated on every execution. Statements being prepared would also bake a stale traceparent into the server-side prepared statement. Tests now assert the exact SQL received by the server via current_query().
Expand the XML docs of EnableSqlCommenterTracePropagation to explain that the traceparent makes the SQL text unique per execution (incompatible with server-side statement caching, see npgsql#6255 and google/sqlcommenter#284), that prepared/auto-prepared statements are never modified, and that the feature is experimental. Add a test for commands with multiple statements.
Covers the Recorded ? "01" : "00" branch in AppendSqlCommenterTraceContext, which no existing test exercised (all prior tests assumed a sampled activity).
Author
|
Thanks @gfoidl! Applied both suggestions in 41a119b. While adding integration tests that assert on |
This branch has not been deployed
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.
Fixes #6255.
Description
This PR adds opt-in support for W3C SQLCommenter trace context propagation in Npgsql, as discussed in #6255.
When enabled via
EnableSqlCommenterTracePropagation(), Npgsql appends the W3Ctraceparentcontext as a SQL comment (/*traceparent='...'*/) to outgoing queries when an activeActivity.Currentis present.Key Changes
EnableSqlCommenterTracePropagation()opt-in method toNpgsqlTracingOptionsBuilder.AppendSqlCommenterTraceContextformatter toNpgsqlActivitySource.NpgsqlCommand.cs.src/Npgsql/PublicAPI.Unshipped.txt.SqlCommenterTracePropagationinTracingTests.cs.Performance Impact
Default configuration (
EnableSqlCommenterTracePropagation = false) incurs zero allocation and zero overhead on the hot path.