fix(node): Prevent PostgresJs integration from emitting duplicate spans per query#21364
Merged
Conversation
s1gr1d
approved these changes
Jun 8, 2026
| // Skip if this query came from an instrumented sql instance (already handled by wrapper), | ||
| // or if handle() was already called (postgres.js calls handle() from then/catch/finally — | ||
| // only the first call executes SQL, subsequent calls are no-ops). | ||
| if ((this as Record<symbol, unknown>)[QUERY_FROM_INSTRUMENTED_SQL] || this.executed) { |
Member
There was a problem hiding this comment.
Nit: put this.executed as a first condition. Makes it a bit easier to read.
…ns per query postgres.js calls handle() from then/catch/finally, but only the first invocation executes SQL (guarded by this.executed). The patched handle was creating a new span on every call, inflating span count 2-3x. Fixes #21355 Co-Authored-By: Claude claude-opus-4-6 <noreply@anthropic.com>
Co-Authored-By: Claude claude-opus-4-6 <noreply@anthropic.com>
chargome
approved these changes
Jun 8, 2026
Member
chargome
left a comment
There was a problem hiding this comment.
Just one q, otherwise looks good!
| const wrappedHandle = async function (this: { executed?: boolean }, ...args: unknown[]): Promise<unknown> { | ||
| // postgres.js calls handle() from then/catch/finally — only the first call executes SQL, | ||
| // subsequent calls are no-ops (guarded by this.executed). Skip span creation for no-ops. | ||
| if (this.executed || !_shouldCreateSpans(options)) { |
Member
There was a problem hiding this comment.
q: Is this available on all versions that we support?
Member
Author
There was a problem hiding this comment.
Yes, seems to have been available since v3 which we target with >= 3.0.0 < 4.
10ecfaa to
3e94d7e
Compare
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.
postgres.js calls handle() from then/catch/finally, but only the first invocation executes SQL (guarded by this.executed). The patched handle was creating a new span on every call, inflating span count 2-3x.
Fixes #21355