-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(cloudflare): Wait for span links to be set #21167
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
Open
JPeer264
wants to merge
1
commit into
develop
Choose a base branch
from
jp/wait-for-alarm
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
dev-packages/cloudflare-integration-tests/suites/tracing/durableobject-alarm-links/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { DurableObject } from 'cloudflare:workers'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| TEST_DURABLE_OBJECT: DurableObjectNamespace; | ||
| } | ||
|
|
||
| class AlarmDurableObjectBase extends DurableObject<Env> { | ||
| public constructor(ctx: DurableObjectState, env: Env) { | ||
| super(ctx, env); | ||
| } | ||
|
|
||
| async setAlarm(): Promise<void> { | ||
| await this.ctx.storage.setAlarm(Date.now() + 100); | ||
| } | ||
|
|
||
| async alarm(): Promise<void> { | ||
| await new Promise<void>(resolve => setTimeout(resolve, 10)); | ||
| } | ||
| } | ||
|
|
||
| export const TestDurableObject = Sentry.instrumentDurableObjectWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| enableRpcTracePropagation: true, | ||
| }), | ||
| AlarmDurableObjectBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| { | ||
| async fetch(request: Request, env: Env): Promise<Response> { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/set-alarm') { | ||
| const id = url.searchParams.get('id') || 'default'; | ||
| const doId = env.TEST_DURABLE_OBJECT.idFromName(id); | ||
| const stub = env.TEST_DURABLE_OBJECT.get(doId) as unknown as AlarmDurableObjectBase; | ||
| await stub.setAlarm(); | ||
| return new Response('Alarm scheduled'); | ||
| } | ||
|
|
||
| return new Response('OK'); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
64 changes: 64 additions & 0 deletions
64
dev-packages/cloudflare-integration-tests/suites/tracing/durableobject-alarm-links/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import type { TransactionEvent } from '@sentry/core'; | ||
| import { createRunner } from '../../../runner'; | ||
|
|
||
| it('alarm links to the trace that scheduled it via sentry.previous_trace', async ({ signal }) => { | ||
| let setAlarmTransaction: TransactionEvent | undefined; | ||
| let alarmTransaction: TransactionEvent | undefined; | ||
| const testId = Date.now().toString(); | ||
|
|
||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent; | ||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| transaction: expect.stringContaining('/set-alarm'), | ||
| }), | ||
| ); | ||
| }) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent; | ||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| transaction: 'setAlarm', | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'rpc', | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }), | ||
| }), | ||
| }), | ||
| ); | ||
| setAlarmTransaction = transactionEvent; | ||
| }) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent; | ||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| type: 'transaction', | ||
| transaction: 'alarm', | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'function', | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }), | ||
| }), | ||
| }), | ||
| ); | ||
| alarmTransaction = transactionEvent; | ||
| }) | ||
| .unordered() | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', `/set-alarm?id=${testId}`); | ||
| await runner.completed(); | ||
|
|
||
| const traceData = alarmTransaction!.contexts?.trace?.data as Record<string, unknown> | undefined; | ||
| const previousTrace = traceData?.['sentry.previous_trace'] as string | undefined; | ||
|
|
||
| expect(previousTrace).toBeDefined(); | ||
| expect(previousTrace).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[01]$/); | ||
|
|
||
| const [linkedTraceId] = previousTrace!.split('-'); | ||
| expect(linkedTraceId).toBe(setAlarmTransaction!.contexts?.trace?.trace_id); | ||
| }); |
20 changes: 20 additions & 0 deletions
20
...ages/cloudflare-integration-tests/suites/tracing/durableobject-alarm-links/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "name": "durableobject-alarm-links", | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "migrations": [ | ||
| { | ||
| "new_sqlite_classes": ["TestDurableObject"], | ||
| "tag": "v1", | ||
| }, | ||
| ], | ||
| "durable_objects": { | ||
| "bindings": [ | ||
| { | ||
| "class_name": "TestDurableObject", | ||
| "name": "TEST_DURABLE_OBJECT", | ||
| }, | ||
| ], | ||
| }, | ||
| "compatibility_flags": ["nodejs_als"], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sync handlers skip link await
Medium Severity
For
startNewTracehandlers that return synchronously or throw synchronously, the stored trace link is never awaited and is no longer registered withwaitUntil. The span can end beforegetStoredSpanContextsetssentry.previous_trace, so alarm-style linking can fail for syncalarmimplementations.Reviewed by Cursor Bugbot for commit ce76d4b. Configure here.