-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(cloudflare): Initialize once per workflow run and preserve scope for step.do
#17582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I don't think this will work reliably in production. Cloudflare workflows don't run like the code might suggest. The workflow can be suspended between steps and retries and all state is lost. The only state that is persisted are the return values from We derive the https://developers.cloudflare.com/workflows/build/rules-of-workflows/
This means that any spans created outside of I'm seeking clarification from Cloudflare because I'm not even convinced the users example will work reliably if the workflow gets suspended. If no state is preserved between the steps, how will it know far it has iterated through class WorkflowIngestBase extends WorkflowEntrypoint<Env, Param> {
async run(event, step): Promise<void> {
const page = step.do('fetch page'() => {...})
for (const item in page.item){
await step.do('process page item', async () => {...})
await step.do('save item', async () => {...})
}
}
} |
const config = typeof configOrCallback === 'function' ? undefined : configOrCallback; | ||
|
||
const instrumentedCallback: () => Promise<T> = async () => { | ||
return workflowStepWithSentry(this._instanceId, this._options, async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can it be a problem that we no longer create a sentry instance here? 🤔 is there always an existing sentry instance outside here...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we always need to create a Sentry instance (or maybe check that one exists?) inside step.do
because these can be run in a new execution context if the workflow gets suspended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under the assumption that run
is called on every new invocation of the workflow (e.g. after hibernation), we should have a client here
Thanks Tim for the review! I am not 100% sure if this will really violate the rules here 🤔 The docs are not too clear about that. This is how I understood it:
|
If Ok, so I'm more convinced that |
Hey from the Workflows team 👋
Yes, at the moment, every time "hibernation" happens Not sure what would be the best way to fix this (in the specific context of the sentry-sdk) - but I would recommend only creating spans inside the Initializing Sentry at the start of every |
Thanks for the clarification! 🙌 Sentry still starts the span inside I only added a test-case where a span is created around |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like this will be a good improvement! This also has the bonus that it will also catch exceptions in run
but outside of step.do
which would have been missed before?
We should possibly update the docs to discourage users from creating spans outside of step.do
and explain why.
## DESCRIBE YOUR PR Adds notice about rather not creating spans outside of `step.do` mentioned in this PR: getsentry/sentry-javascript#17582 ref: getsentry/sentry-javascript#17419 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs)
Previously, our Cloudflare Workflows instrumentation created a new Sentry client inside every
step.do
. This resets the tracing context: a custom span started withstartSpan
around multiplestep.do
would finish under a different client/scope than its children.This change initializes Sentry once per workflow run and preserves the active scope across steps. We capture the current scope in
step.do
and pass it tostartSpan
.A new test was added to check that a
step.do
span becomes a child of a surrounding custom span.fixes #17419
ref (for being able to test this): cloudflare/workerd#5030
Multiple

step.do
with a surroundingstartSpan
call will now result in this: