-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Propagate traceparent to RPC calls - via fetch #19991
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
4
commits into
develop
Choose a base branch
from
jp/rpc-instrument
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb6a835
feat(cloudflare): Propagate traceparent to RPC calls
JPeer264 ef05c42
feat(cloudflare): Use getTracingHeadersForFetchRequest to get trace h…
JPeer264 54a204a
fixup! feat(cloudflare): Use getTracingHeadersForFetchRequest to get …
JPeer264 291bbd3
chore: Change license head
JPeer264 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
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
dev-packages/cloudflare-integration-tests/suites/tracing/propagation/worker-do/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,60 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { DurableObject } from 'cloudflare:workers'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| MY_DURABLE_OBJECT: DurableObjectNamespace; | ||
| MY_QUEUE: Queue; | ||
| } | ||
|
|
||
| class MyDurableObjectBase extends DurableObject<Env> { | ||
| async fetch(request: Request) { | ||
| return new Response('DO is fine'); | ||
| } | ||
| } | ||
|
|
||
| export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| MyDurableObjectBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| { | ||
| async fetch(request, env) { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/queue/send') { | ||
| await env.MY_QUEUE.send({ action: 'test' }); | ||
| return new Response('Queued'); | ||
| } | ||
|
|
||
| const id = env.MY_DURABLE_OBJECT.idFromName('test'); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id); | ||
| const response = await stub.fetch(new Request('http://fake-host/hello')); | ||
| const text = await response.text(); | ||
| return new Response(text); | ||
| }, | ||
|
|
||
| async queue(batch, env, _ctx) { | ||
| const id = env.MY_DURABLE_OBJECT.idFromName('test'); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id); | ||
| for (const message of batch.messages) { | ||
| await stub.fetch(new Request('http://fake-host/hello')); | ||
| message.ack(); | ||
| } | ||
| }, | ||
|
|
||
| async scheduled(controller, env, _ctx) { | ||
| const id = env.MY_DURABLE_OBJECT.idFromName('test'); | ||
| const stub = env.MY_DURABLE_OBJECT.get(id); | ||
| await stub.fetch(new Request('http://fake-host/hello')); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); | ||
206 changes: 206 additions & 0 deletions
206
dev-packages/cloudflare-integration-tests/suites/tracing/propagation/worker-do/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,206 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { createRunner } from '../../../../runner'; | ||
|
|
||
| it('propagates trace from worker to durable object', async ({ signal }) => { | ||
| let workerTraceId: string | undefined; | ||
| let workerSpanId: string | undefined; | ||
| let doTraceId: string | undefined; | ||
| let doParentSpanId: string | undefined; | ||
|
|
||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'http.server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| }), | ||
| origin: 'auto.http.cloudflare', | ||
| }), | ||
| }), | ||
| transaction: 'GET /hello', | ||
| }), | ||
| ); | ||
| doTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; | ||
| }) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'http.server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| }), | ||
| origin: 'auto.http.cloudflare', | ||
| }), | ||
| }), | ||
| transaction: 'GET /', | ||
| }), | ||
| ); | ||
| workerTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| workerSpanId = transactionEvent.contexts?.trace?.span_id as string; | ||
| }) | ||
| .unordered() | ||
| .start(signal); | ||
| await runner.makeRequest('get', '/'); | ||
| await runner.completed(); | ||
|
|
||
| expect(workerTraceId).toBeDefined(); | ||
| expect(doTraceId).toBeDefined(); | ||
| expect(workerTraceId).toBe(doTraceId); | ||
|
|
||
| expect(workerSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBe(workerSpanId); | ||
| }); | ||
|
|
||
| it('propagates trace from queue handler to durable object', async ({ signal }) => { | ||
| let queueTraceId: string | undefined; | ||
| let queueSpanId: string | undefined; | ||
| let doTraceId: string | undefined; | ||
| let doParentSpanId: string | undefined; | ||
|
|
||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'http.server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| }), | ||
| origin: 'auto.http.cloudflare', | ||
| }), | ||
| }), | ||
| transaction: 'GET /hello', | ||
| }), | ||
| ); | ||
| doTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; | ||
| }) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'queue.process', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.faas.cloudflare.queue', | ||
| }), | ||
| origin: 'auto.faas.cloudflare.queue', | ||
| }), | ||
| }), | ||
| transaction: 'process my-queue', | ||
| }), | ||
| ); | ||
| queueTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| queueSpanId = transactionEvent.contexts?.trace?.span_id as string; | ||
| }) | ||
| // Also expect the fetch transaction from the /queue/send request | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'http.server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| }), | ||
| origin: 'auto.http.cloudflare', | ||
| }), | ||
| }), | ||
| transaction: 'GET /queue/send', | ||
| }), | ||
| ); | ||
| }) | ||
| .unordered() | ||
| .start(signal); | ||
| // The fetch handler sends a message to the queue, which triggers the queue consumer | ||
| await runner.makeRequest('get', '/queue/send'); | ||
| await runner.completed(); | ||
|
|
||
| expect(queueTraceId).toBeDefined(); | ||
| expect(doTraceId).toBeDefined(); | ||
| expect(queueTraceId).toBe(doTraceId); | ||
|
|
||
| expect(queueSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBe(queueSpanId); | ||
| }); | ||
|
|
||
| it('propagates trace from scheduled handler to durable object', async ({ signal }) => { | ||
| let scheduledTraceId: string | undefined; | ||
| let scheduledSpanId: string | undefined; | ||
| let doTraceId: string | undefined; | ||
| let doParentSpanId: string | undefined; | ||
|
|
||
| const runner = createRunner(__dirname) | ||
| .withWranglerArgs('--test-scheduled') | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'http.server', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.http.cloudflare', | ||
| }), | ||
| origin: 'auto.http.cloudflare', | ||
| }), | ||
| }), | ||
| transaction: 'GET /hello', | ||
| }), | ||
| ); | ||
| doTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| doParentSpanId = transactionEvent.contexts?.trace?.parent_span_id as string; | ||
| }) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as Event; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| contexts: expect.objectContaining({ | ||
| trace: expect.objectContaining({ | ||
| op: 'faas.cron', | ||
| data: expect.objectContaining({ | ||
| 'sentry.origin': 'auto.faas.cloudflare.scheduled', | ||
| }), | ||
| origin: 'auto.faas.cloudflare.scheduled', | ||
| }), | ||
| }), | ||
| }), | ||
| ); | ||
| scheduledTraceId = transactionEvent.contexts?.trace?.trace_id as string; | ||
| scheduledSpanId = transactionEvent.contexts?.trace?.span_id as string; | ||
| }) | ||
| .unordered() | ||
| .start(signal); | ||
| await runner.makeRequest('get', '/__scheduled?cron=*+*+*+*+*'); | ||
| await runner.completed(); | ||
|
|
||
| expect(scheduledTraceId).toBeDefined(); | ||
| expect(doTraceId).toBeDefined(); | ||
| expect(scheduledTraceId).toBe(doTraceId); | ||
|
|
||
| expect(scheduledSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBeDefined(); | ||
| expect(doParentSpanId).toBe(scheduledSpanId); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
...packages/cloudflare-integration-tests/suites/tracing/propagation/worker-do/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,39 @@ | ||
| { | ||
| "name": "cloudflare-durable-objects", | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "compatibility_flags": ["nodejs_als"], | ||
| "migrations": [ | ||
| { | ||
| "new_sqlite_classes": ["MyDurableObject"], | ||
| "tag": "v1", | ||
| }, | ||
| ], | ||
| "durable_objects": { | ||
| "bindings": [ | ||
| { | ||
| "class_name": "MyDurableObject", | ||
| "name": "MY_DURABLE_OBJECT", | ||
| }, | ||
| ], | ||
| }, | ||
| "queues": { | ||
| "producers": [ | ||
| { | ||
| "binding": "MY_QUEUE", | ||
| "queue": "my-queue", | ||
| }, | ||
| ], | ||
| "consumers": [ | ||
| { | ||
| "queue": "my-queue", | ||
| }, | ||
| ], | ||
| }, | ||
| "triggers": { | ||
| "crons": ["* * * * *"], | ||
| }, | ||
| "vars": { | ||
| "SENTRY_DSN": "https://932e620ee3921c3b4a61c72558ad88ce@o447951.ingest.us.sentry.io/4509553159831552", | ||
| }, | ||
| } | ||
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.