-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(v10/cloudflare): Add Spotlight integration for local dev event forwarding #22796
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
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
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
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,89 @@ | ||
| import type { Client, Envelope, IntegrationFn } from '@sentry/core'; | ||
| import { debug, defineIntegration, serializeEnvelope, suppressTracing } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
|
|
||
| type SpotlightConnectionOptions = { | ||
| /** | ||
| * Set this if the Spotlight Sidecar is not running on localhost:8969. | ||
| * By default, the URL is set to http://localhost:8969/stream | ||
| */ | ||
| sidecarUrl?: string; | ||
| }; | ||
|
|
||
| export const INTEGRATION_NAME = 'Spotlight' as const; | ||
|
|
||
| const _spotlightIntegration = ((options: Partial<SpotlightConnectionOptions> = {}) => { | ||
| const sidecarUrl = options.sidecarUrl || 'http://localhost:8969/stream'; | ||
|
|
||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setup(client) { | ||
| DEBUG_BUILD && debug.log('[Spotlight] Using Sidecar URL', sidecarUrl); | ||
| setupSidecarForwarding(client, sidecarUrl); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Use this integration to send errors and transactions to Spotlight. | ||
| * | ||
| * Learn more about spotlight at https://spotlightjs.com | ||
| * | ||
| * Important: This integration is intended for local development only. | ||
| * Each forwarded envelope counts as a Worker subrequest (50 free / 1000 paid | ||
| * per invocation), so it should not be enabled in production. | ||
| */ | ||
| export const spotlightIntegration = defineIntegration(_spotlightIntegration); | ||
|
|
||
| function setupSidecarForwarding(client: Client, sidecarUrl: string): void { | ||
| const parsedUrl = parseSidecarUrl(sidecarUrl); | ||
| if (!parsedUrl) { | ||
| return; | ||
| } | ||
|
|
||
| let failCount = 0; | ||
|
|
||
| client.on('beforeEnvelope', (envelope: Envelope) => { | ||
| if (failCount > 3) { | ||
| DEBUG_BUILD && debug.warn('[Spotlight] Disabled Sentry -> Spotlight forwarding due to too many failed requests'); | ||
| return; | ||
| } | ||
|
|
||
| const body = serializeEnvelope(envelope); | ||
|
|
||
| suppressTracing(() => { | ||
| fetch(parsedUrl.href, { | ||
| method: 'POST', | ||
| body, | ||
| headers: { | ||
| 'Content-Type': 'application/x-sentry-envelope', | ||
| }, | ||
| }).then( | ||
| res => { | ||
| // Consume the response body to satisfy Cloudflare Workers' requirement | ||
| // that all fetch response bodies are read or cancelled. | ||
| res.text().catch(() => { | ||
| // no-op | ||
| }); | ||
|
|
||
| if (res.status >= 200 && res.status < 400) { | ||
| failCount = 0; | ||
| } | ||
| }, | ||
| () => { | ||
| failCount++; | ||
| DEBUG_BUILD && debug.warn('[Spotlight] Failed to send envelope to Spotlight Sidecar'); | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function parseSidecarUrl(url: string): URL | undefined { | ||
| try { | ||
| return new URL(url); | ||
| } catch { | ||
| DEBUG_BUILD && debug.warn(`[Spotlight] Invalid sidecar URL: ${url}`); | ||
| return undefined; | ||
| } | ||
| } | ||
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.
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.
Bug: The Spotlight integration's circuit breaker does not trigger on HTTP 4xx/5xx errors, only on network failures, preventing it from disabling requests to a failing but reachable sidecar.
Severity: LOW
Suggested Fix
Modify the
fetch()success callback to check the response status. If the status is outside the 200-399 range, increment thefailCount. This ensures that both network errors and HTTP error responses contribute to triggering the circuit breaker, aligning with its intended purpose of handling all failure modes.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.