Content-Length: 429866 | pFad | http://github.com/getsentry/sentry-javascript/pull/16681

3C feat(cloudflare): Flush after `waitUntil` by 0xbad0c0d3 · Pull Request #16681 · getsentry/sentry-javascript · GitHub
Skip to content

feat(cloudflare): Flush after waitUntil #16681

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

Merged
merged 7 commits into from
Jun 26, 2025

Conversation

0xbad0c0d3
Copy link
Contributor

This PR aim is to send events if all (if any was scheduled) waitUntil promises were finished. Otherwise you may loose events.
This fixes: #16559

@0xbad0c0d3 0xbad0c0d3 force-pushed the flush-after-waituntils branch from e91f0d9 to 16578ab Compare June 23, 2025 13:26
@timfish
Copy link
Collaborator

timfish commented Jun 23, 2025

Rather than add an internal makeFlushAfterAll, could we not make the flush export work like this?

@mydea mydea requested a review from AbhiPrasad June 23, 2025 14:15
@AbhiPrasad
Copy link
Member

Rather than add an internal makeFlushAfterAll, could we not make the flush export work like this?

That would be nice, we just need to make flush get access to the ExecutionContext. Maybe we can attach it to the client and then flush can grab it that way?

I'm a bit scared patching waitUntil for all circumstances. Can we somehow just do this only if you're calling sentry methods inside waitUntil?

@0xbad0c0d3
Copy link
Contributor Author

I think we can do it easily with AsyncLocalStorage. I will redo it this way and will see. But, I am not very familiar with the code-base. Do we use flush anywhere else? Except @sentry/cloudflare?

@AbhiPrasad
Copy link
Member

flush is exported from every SDK, but they all call

public flush(timeout?: number): PromiseLike<boolean> {

We can just change CloudflareClient to wrap flush accordingly.

@0xbad0c0d3
Copy link
Contributor Author

Rather than add an internal makeFlushAfterAll, could we not make the flush export work like this?

That would be nice, we just need to make flush get access to the ExecutionContext. Maybe we can attach it to the client and then flush can grab it that way?

I'm a bit scared patching waitUntil for all circumstances. Can we somehow just do this only if you're calling sentry methods inside waitUntil?

Why only sentry methods? Exception might happen after the flush() promise was resolved and the buffer will not be flushed. Or I am wrong in my assumption?

@AbhiPrasad
Copy link
Member

There are other usages of waitUntil that I want to make sure we don't interfere with. I guess because this is only to a single function invocation the promise back pressure shouldn't be problematic.

Let's make the change to the client then. I'll test this out with https://github.com/getsentry/sentry-mcp, @timfish would appreciate if you can also use your test apps to test this.

cod1k added 2 commits June 25, 2025 16:27
…FlushLock`, ensuring proper synchronization with `waitUntil`. Update tests to validate new `flush` behavior.
@0xbad0c0d3
Copy link
Contributor Author

0xbad0c0d3 commented Jun 25, 2025

There are other usages of waitUntil that I want to make sure we don't interfere with. I guess because this is only to a single function invocation the promise back pressure shouldn't be problematic.

Let's make the change to the client then. I'll test this out with https://github.com/getsentry/sentry-mcp, @timfish would appreciate if you can also use your test apps to test this.

I also fixed out-of-scope linter issue (the same module was imported twice: 86f5251)

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Really nice change @0xbad0c0d3 - just one quick thing about the init API

@@ -36,16 +38,19 @@ export function getDefaultIntegrations(options: CloudflareOptions): Integration[
/**
* Initializes the cloudflare SDK.
*/
export function init(options: CloudflareOptions): CloudflareClient | undefined {
export function init(options: CloudflareOptions, ctx: ExecutionContext | void): CloudflareClient | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

Let's make ctx: ExecutionContext | void part of CloudflareOptions instead of a separate arg.

void probably be replaced with ctx?: ExecutionContext as well.

@0xbad0c0d3
Copy link
Contributor Author

Also found that was missed to modify durable object wrapper

This commit introduces unit tests to validate the functionality of `instrumentDurableObjectWithSentry`. The tests ensure proper instrumentation of all methods, correct handling of `waitUntil` promises, and interaction with Sentry's `flush` logic.
@0xbad0c0d3
Copy link
Contributor Author

Basic tests for durableobject has been added

@AbhiPrasad AbhiPrasad changed the title Flush after waituntils feat(cloudflare): Flush after waitUntil Jun 26, 2025
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Mind running yarn fix to format? Then we are good to merge!

This commit ensures consistency by adding a missing semicolon in the `ctx` property of the `CloudflareOptions` interface. No functional changes were made, but it improves code style and adheres to linting rules.
@AbhiPrasad AbhiPrasad merged commit c20a3c4 into getsentry:develop Jun 26, 2025
143 checks passed
AbhiPrasad added a commit that referenced this pull request Jun 26, 2025
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #16681

Co-authored-by: AbhiPrasad <18689448+AbhiPrasad@users.noreply.github.com>
Sign up for free to 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.

Figure out why capturing events inside waitUntil doesn’t work in cloudflare
3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/getsentry/sentry-javascript/pull/16681

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy