-
Notifications
You must be signed in to change notification settings - Fork 72
fix: Wait for events to be flushed #652
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
HazAT
left a comment
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.
Makes sense, I wonder tho why this/what changed compared to before
betegon
left a comment
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.
lgtm!
|
Shouldn’t this be be resolved upstream in Sentry? |
You're totally right. I'll see why this is actually needed. |
(closes #18360) ## Problem In getsentry/sentry-mcp#652 we had to manually wait for the flush to ensure that async operations completed before the response was sent. This was caused by the MCP server streaming data after the actual request has been finished. When using the Cloudflare SDK with MCP servers (or other streaming APIs), spans created during the streaming phase were not being flushed properly because the `flush()` method would complete before these async spans finished. ## Solution During the request, when a span is created for the streaming call, we now register listeners on the CloudflareClient for `spanStart` and `spanEnd` events. The client tracks all pending spans and waits for them to complete before flushing. Specifically: - Added `_pendingSpans` Set to track active span IDs - Created a promise-based mechanism (`_spanCompletionPromise`) to wait for all spans to complete - Modified the `flush()` method to wait for pending spans before calling `super.flush()` - Added a timeout (default 5 seconds) to prevent indefinite waiting ## Test Added a comprehensive E2E test application (`cloudflare-mcp`) that: - Sets up a Cloudflare Worker with an MCP server - Registers a tool that creates spans during execution - Verifies that both the HTTP request span and the MCP tool call span are properly sent to Sentry - Validates span relationships (parent/child) and trace IDs The test confirms that spans are now flushed correctly without manual intervention.
In #652 we had to manually add the flush, which should actually be done by the Cloudflare SDK. This has now landed in `10.28.0` (via [this PR](getsentry/sentry-javascript#18334)) and no manual flushing has to be done anymore. Just to be sure sure I verified on top if everything has been sent locally. This is a trace from my local deployment: https://sentry-sdks.sentry.io/explore/traces/trace/434f1b1e8655444c875bce5c66645376/?fov=0%2C291&node=span-84a7a24ae593ee49&project=4510380305022976&source=traces&statsPeriod=15m&targetId=a505468e10176600×tamp=1764689933 I recommend to review this PR [without whitespaces enabled](https://github.com/getsentry/sentry-mcp/pull/663/files?w=1).
Events which hold attributes such as
organization.slugormcp.*were not flushed correctly.sentry-mcp/packages/mcp-core/src/server.ts
Lines 281 to 286 in ce3ec0f
With this it will re-add these missing events, as it will wait with
waitUntilthat these events are flushed within the tool call.Example of a tool call event