Skip to content

fix(cloudflare): Wait for span links to be set - #21167

Merged
JPeer264 merged 4 commits into
developfrom
jp/wait-for-alarm
Jun 1, 2026
Merged

fix(cloudflare): Wait for span links to be set#21167
JPeer264 merged 4 commits into
developfrom
jp/wait-for-alarm

Conversation

@JPeer264

Copy link
Copy Markdown
Member

This came up in #21101 where the link was not set in time and the tests failed. With this PR we are waiting for the links to be actually set, before the rootspan ends. Retrieving the stored span shouldn't take too long - and the work was also before already executed during the response.

@JPeer264
JPeer264 requested a review from a team as a code ownerMay 26, 2026 11:12
@JPeer264
JPeer264 requested review from andreiborza and mydea and removed request for a teamMay 26, 2026 11:12
@JPeer264JPeer264 self-assigned this May 26, 2026
Comment threadpackages/cloudflare/src/wrapMethodWithSentry.ts Outdated

@isaacsisaacs 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.

I think the clanker's objection is valid, unless it's impossible to have a link promise and a non-then-able result. Might be good to add a test showing that case, if possible.

If I'm misreading it, happy to update to 👍

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.

If I understand the clanker's objection, seems like there should be a waitIUntil here to handle the linkPromise if the result is not thenable?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

whoopsie. That was actually a good catch. I actually wanted to go away from waitUntil, as with that it is a little unreliable if a trace link is created or not.

I'll change the logic a little so we don't have duplicated code for sync and async returns.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Ok I had to change it to a sync KV update to make it work for sync alarms. I also didn't want to return a promise for sync alarms, as this would change the users method to be a promise.

I now added also a sync alarm test as proof.

@cursorcursorBot 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.

Reviewed by Cursor Bugbot for commit 3d9cf8c. Configure here.

Comment threadpackages/cloudflare/src/utils/traceLinks.ts Outdated
@github-actions

github-actionsBot commented May 27, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize% ChangeChange
@sentry/browser27.2 kB--
@sentry/browser - with treeshaking flags25.65 kB--
@sentry/browser (incl. Tracing)45.25 kB--
@sentry/browser (incl. Tracing + Span Streaming)47.47 kB--
@sentry/browser (incl. Tracing, Profiling)50.24 kB--
@sentry/browser (incl. Tracing, Replay)84.83 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags74.4 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)89.54 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)102.16 kB--
@sentry/browser (incl. Feedback)44.36 kB--
@sentry/browser (incl. sendFeedback)32.02 kB--
@sentry/browser (incl. FeedbackAsync)37.11 kB--
@sentry/browser (incl. Metrics)28.27 kB--
@sentry/browser (incl. Logs)28.51 kB--
@sentry/browser (incl. Metrics & Logs)29.22 kB--
@sentry/react29.03 kB--
@sentry/react (incl. Tracing)47.52 kB--
@sentry/vue32.22 kB--
@sentry/vue (incl. Tracing)47.16 kB--
@sentry/svelte27.23 kB--
CDN Bundle29.57 kB--
CDN Bundle (incl. Tracing)47.81 kB--
CDN Bundle (incl. Logs, Metrics)31.08 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)49.05 kB--
CDN Bundle (incl. Replay, Logs, Metrics)70.34 kB--
CDN Bundle (incl. Tracing, Replay)85.18 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)86.35 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)91.05 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)92.2 kB--
CDN Bundle - uncompressed87.7 kB--
CDN Bundle (incl. Tracing) - uncompressed144.11 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed92.19 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed147.87 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed216.91 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed262.88 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed266.63 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed276.58 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed280.32 kB--
@sentry/nextjs (client)50.04 kB--
@sentry/sveltekit (client)45.7 kB--
@sentry/core/server76.56 kB--
@sentry/core/browser63.09 kB--
@sentry/node-core61.69 kB-0.01%-2 B 🔽
@sentry/node130.37 kB-0.01%-1 B 🔽
@sentry/node - without tracing74.06 kB-0.01%-2 B 🔽
@sentry/aws-serverless86.26 kB-0.01%-1 B 🔽
@sentry/cloudflare (withSentry) - minified171.49 kB-0.12%-195 B 🔽
@sentry/cloudflare (withSentry)428.63 kB-0.24%-1.02 kB 🔽

View base workflow run

@JPeer264
JPeer264 requested a review from isaacsMay 27, 2026 09:49
return res;
};

const onRejected = (e: unknown) => {

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

note: I outsourced that since it was used 4 times

@github-actions

Copy link
Copy Markdown
Contributor

👋 @mydea, @andreiborza — Please review this PR when you get a chance!

@github-actions

Copy link
Copy Markdown
Contributor

👋 @isaacs — Please review this PR when you get a chance!

@isaacsisaacs 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.

Looks good to me! If you think the clanker's objection is valid, could address it with a try/catch, but that's only important if we care to support versions that don't have the API in use here.

@JPeer264
JPeer264 enabled auto-merge (squash) June 1, 2026 15:59
Comment on lines +39 to 43

originalStorage.kv.put(getTraceLinkKey(methodName), storedContext);
}
} catch (error) {
// Silently ignore storage errors to prevent internal failures from affecting user code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The code assumes originalStorage.kv exists, but it is undefined for legacy KV-backed Durable Objects. This causes trace linking to silently fail for those users.
Severity: MEDIUM

Suggested Fix

Add a guard to check for the existence of originalStorage.kv before attempting to use its methods. This will prevent silent errors and the loss of tracing functionality for users on legacy KV-backed Durable Objects. If backward compatibility is required, consider adding a fallback to the previous asynchronous storage mechanism.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: packages/cloudflare/src/utils/traceLinks.ts#L39-L43
Potential issue: The functions `storeSpanContext` and `getStoredSpanContext` were
updated to use the synchronous KV API via `originalStorage.kv`. However, for legacy
KV-backed Durable Objects, the `originalStorage.kv` property is `undefined`. This leads
to a `TypeError` when `put` or `get` is called. Although the error is caught by a
`try-catch` block, preventing a crash, it results in a silent failure. Consequently,
span context is not stored or retrieved for these legacy durable objects, which breaks
distributed tracing continuity for that specific, supported configuration.

Did we get this right? 👍 / 👎 to inform future reviews.

@JPeer264
JPeer264 merged commit dfeeb11 into developJun 1, 2026
268 of 269 checks passed
@JPeer264
JPeer264 deleted the jp/wait-for-alarm branch June 1, 2026 16:15
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@JPeer264@isaacs