Repository navigation
feat(cloudflare): Add incoming request span options to httpServerIntegration
#25582
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
isaacs
wants to merge
1
commit into
develop
from
isaacschlueter/js-3971-cloudflare-add-ignoreincomingrequests-ignorestaticassets
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
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
41 changes: 41 additions & 0 deletions
41
dev-packages/cloudflare-integration-tests/suites/integrations/http-server-spans/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,41 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| // A plain worker. The `@sentry/cloudflare/vite` plugin wraps it with `withSentry` at build time, | ||
| // with the options from `instrument.server.ts`. | ||
| export default { | ||
| async fetch(request: Request): Promise<Response> { | ||
| const { pathname } = new URL(request.url); | ||
|
|
||
| if (pathname === '/health') { | ||
| Sentry.captureMessage('Health check'); | ||
| return new Response('ok'); | ||
| } | ||
|
|
||
| if (pathname === '/favicon.ico') { | ||
| Sentry.captureMessage('Static asset'); | ||
| return new Response('icon'); | ||
| } | ||
|
|
||
| if (pathname === '/health/stream') { | ||
| // No span for this request. The message is captured after the handler returns, while the | ||
| // body streams, so it is only sent if the SDK waits for the stream to finish. | ||
| const encoder = new TextEncoder(); | ||
| const body = new ReadableStream<Uint8Array>({ | ||
| async start(controller) { | ||
| controller.enqueue(encoder.encode('a')); | ||
| await new Promise(resolve => setTimeout(resolve, 50)); | ||
| Sentry.captureMessage('Streamed after handler return'); | ||
| controller.enqueue(encoder.encode('b')); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
| return new Response(body, { headers: { 'content-type': 'text/event-stream' } }); | ||
| } | ||
|
|
||
| return new Response('ok', { status: 201 }); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; |
18 changes: 18 additions & 0 deletions
18
...s/cloudflare-integration-tests/suites/integrations/http-server-spans/instrument.server.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,18 @@ | ||
| import { defineCloudflareOptions, httpServerIntegration } from '@sentry/cloudflare'; | ||
|
|
||
| export default defineCloudflareOptions((env: { SENTRY_DSN: string }) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| traceLifecycle: 'static', | ||
| integrations: [ | ||
| httpServerIntegration({ | ||
| ignoreIncomingRequests: urlPath => urlPath.startsWith('/health'), | ||
| onSpanCreated: (span, request) => { | ||
| span.setAttribute('test.on_span_created', new URL(request.url).pathname); | ||
| }, | ||
| onSpanEnd: (span, _request, response) => { | ||
| span.setAttribute('test.on_span_end', response.status); | ||
| }, | ||
| }), | ||
| ], | ||
| })); |
71 changes: 71 additions & 0 deletions
71
dev-packages/cloudflare-integration-tests/suites/integrations/http-server-spans/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,71 @@ | ||
| import type { Envelope, Event } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { createRunner } from '../../../runner'; | ||
|
|
||
| function itemOf(envelope: Envelope): { type: string; event: Event } { | ||
| const [header, event] = envelope[1][0] as [{ type: string }, Event]; | ||
| return { type: header.type, event }; | ||
| } | ||
|
|
||
| function messageEvent(message: string): (envelope: Envelope) => void { | ||
| return envelope => { | ||
| const { type, event } = itemOf(envelope); | ||
| expect(type).toBe('event'); | ||
| expect(event.message).toBe(message); | ||
| }; | ||
| } | ||
|
|
||
| // The `onSpanCreated` and `onSpanEnd` hooks of the worker set these attributes. | ||
| function apiTransaction(envelope: Envelope): void { | ||
| const { type, event } = itemOf(envelope); | ||
| expect(type).toBe('transaction'); | ||
| expect(event.transaction).toBe('GET /api'); | ||
| expect(event.contexts?.trace?.data).toEqual( | ||
| expect.objectContaining({ | ||
| 'test.on_span_created': '/api', | ||
| 'test.on_span_end': 201, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| // Each test makes a request without a span, then a request with one. With `failOnUnexpected`, a | ||
| // transaction for the first request fails the test, as it arrives before the second one. | ||
|
|
||
| it('does not create a span for a request that ignoreIncomingRequests matches', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .unordered() | ||
| .failOnUnexpected() | ||
| .expect(messageEvent('Health check')) | ||
| .expect(apiTransaction) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/health'); | ||
| await runner.makeRequest('get', '/api'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('does not create a span for a static asset request by default', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .unordered() | ||
| .failOnUnexpected() | ||
| .expect(messageEvent('Static asset')) | ||
| .expect(apiTransaction) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/favicon.ico'); | ||
| await runner.makeRequest('get', '/api'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('sends events from a streamed response without a span after the stream finishes', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .unordered() | ||
| .failOnUnexpected() | ||
| .expect(messageEvent('Streamed after handler return')) | ||
| .expect(apiTransaction) | ||
| .start(signal); | ||
|
|
||
| expect(await runner.makeRequest<string>('get', '/health/stream')).toBe('ab'); | ||
| await runner.makeRequest('get', '/api'); | ||
| await runner.completed(); | ||
| }); |
7 changes: 7 additions & 0 deletions
7
...ckages/cloudflare-integration-tests/suites/integrations/http-server-spans/vite.config.mts
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,7 @@ | ||
| import { cloudflare } from '@cloudflare/vite-plugin'; | ||
| import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite'; | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [cloudflare(), sentryCloudflareVitePlugin()], | ||
| }); |
9 changes: 9 additions & 0 deletions
9
...ackages/cloudflare-integration-tests/suites/integrations/http-server-spans/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,9 @@ | ||
| { | ||
| "$schema": "../../../node_modules/wrangler/config-schema.json", | ||
| "name": "cloudflare-integrations-http-server-spans", | ||
| // `main` points at the source entry. The Sentry Vite plugin builds from it, and the runner serves | ||
| // the built output. | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
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
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
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.