Skip to content

fix(core): preserve span links through beforeSendSpan - #25566

Open
chiliec wants to merge 1 commit into
getsentry:developfrom
chiliec:fix/beforesendspan-drops-span-links
Open

chiliec wants to merge 1 commit into
getsentry:developfrom
chiliec:fix/beforesendspan-drops-span-links

Conversation

@chiliec

@chiliec chiliec commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #25545

Problem

Setting beforeSendSpan — even an identity callback (span) => span — drops the root span's contexts.trace.links from every transaction, so the previous_trace link is lost and the trace view no longer links to the previous trace.

In processBeforeSend, the root span goes through beforeSendSpan(convertTransactionEventToSpanJson(event)) and the result is merged back with merge(event, convertSpanJsonToTransactionEvent(result)). The two converters in packages/core/src/utils/transactionEvent.ts never copied links, and merge (default depth 2) replaces contexts.trace whole, so the links set by spanToTransactionTraceContext are discarded. Thanks to @arobison for the detailed root-cause trace in the issue.

Fix

Carry links through both conversions. TraceContext.links and SpanJSON.links are both SpanLinkJSON[], so this is a direct pass-through.

Tests

Extended the existing transactionEvent.test.ts round-trip cases (convertTransactionEventToSpanJson and convertSpanJsonToTransactionEvent) to assert links (including a previous_trace link) survive.

Verification (local):

npx vitest run test/lib/utils/transactionEvent.test.ts
# with the fix:    Test Files 1 passed (1) | Tests 5 passed (5)
# fix reverted:    Tests 2 failed | 3 passed (5)
#   × convertTransactionEventToSpanJson > should convert a full transaction event to span JSON  (missing "links")
#   × convertSpanJsonToTransactionEvent > should convert a full span JSON to transaction event  (missing "links")

Ran in packages/core with Node 22.12 / vitest 4.1.11.

The transaction-event/span-JSON round trip in processBeforeSend did not
carry contexts.trace.links, so setting beforeSendSpan (even an identity
callback) dropped the root span's links, including the previous_trace
link, from every transaction.

Carry links through convertTransactionEventToSpanJson and
convertSpanJsonToTransactionEvent.

Fixes getsentry#25545
@github-actions github-actions Bot added the external PR from an external contributor label Oct 20, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 65b319a. Configure here.

Comment thread packages/core/test/lib/utils/transactionEvent.test.ts

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks! Just a minor change, we need to assert the result. Happy to approve once that's done.

[SENTRY_EXCLUSIVE_TIME]: 123.45,
other: 'value',
},
links: [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions cover the converters, but the links actually got lost in the merge(event, ...) step in processBeforeSend.

Could you also assert contexts.trace.links on the sent transaction in client.test.ts?

Comment thread packages/core/test/lib/utils/transactionEvent.test.ts

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting beforeSendSpan drops the root span's links from transactions (loses the previous_trace link)

2 participants