|
| 1 | +import type { Client, Envelope, IntegrationFn } from '@sentry/core'; |
| 2 | +import { debug, defineIntegration, serializeEnvelope, suppressTracing } from '@sentry/core'; |
| 3 | +import { DEBUG_BUILD } from '../debug-build'; |
| 4 | + |
| 5 | +type SpotlightConnectionOptions = { |
| 6 | + /** |
| 7 | + * Set this if the Spotlight Sidecar is not running on localhost:8969. |
| 8 | + * By default, the URL is set to http://localhost:8969/stream |
| 9 | + */ |
| 10 | + sidecarUrl?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export const INTEGRATION_NAME = 'Spotlight' as const; |
| 14 | + |
| 15 | +const _spotlightIntegration = ((options: Partial<SpotlightConnectionOptions> = {}) => { |
| 16 | + const sidecarUrl = options.sidecarUrl || 'http://localhost:8969/stream'; |
| 17 | + |
| 18 | + return { |
| 19 | + name: INTEGRATION_NAME, |
| 20 | + setup(client) { |
| 21 | + DEBUG_BUILD && debug.log('[Spotlight] Using Sidecar URL', sidecarUrl); |
| 22 | + setupSidecarForwarding(client, sidecarUrl); |
| 23 | + }, |
| 24 | + }; |
| 25 | +}) satisfies IntegrationFn; |
| 26 | + |
| 27 | +/** |
| 28 | + * Use this integration to send errors and transactions to Spotlight. |
| 29 | + * |
| 30 | + * Learn more about spotlight at https://spotlightjs.com |
| 31 | + * |
| 32 | + * Important: This integration is intended for local development only. |
| 33 | + * Each forwarded envelope counts as a Worker subrequest (50 free / 1000 paid |
| 34 | + * per invocation), so it should not be enabled in production. |
| 35 | + */ |
| 36 | +export const spotlightIntegration = defineIntegration(_spotlightIntegration); |
| 37 | + |
| 38 | +function setupSidecarForwarding(client: Client, sidecarUrl: string): void { |
| 39 | + const parsedUrl = parseSidecarUrl(sidecarUrl); |
| 40 | + if (!parsedUrl) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + let failCount = 0; |
| 45 | + |
| 46 | + client.on('beforeEnvelope', (envelope: Envelope) => { |
| 47 | + if (failCount > 3) { |
| 48 | + DEBUG_BUILD && debug.warn('[Spotlight] Disabled Sentry -> Spotlight forwarding due to too many failed requests'); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const body = serializeEnvelope(envelope); |
| 53 | + |
| 54 | + suppressTracing(() => { |
| 55 | + fetch(parsedUrl.href, { |
| 56 | + method: 'POST', |
| 57 | + body, |
| 58 | + headers: { |
| 59 | + 'Content-Type': 'application/x-sentry-envelope', |
| 60 | + }, |
| 61 | + }).then( |
| 62 | + res => { |
| 63 | + // Consume the response body to satisfy Cloudflare Workers' requirement |
| 64 | + // that all fetch response bodies are read or cancelled. |
| 65 | + res.text().catch(() => { |
| 66 | + // no-op |
| 67 | + }); |
| 68 | + |
| 69 | + if (res.status >= 200 && res.status < 400) { |
| 70 | + failCount = 0; |
| 71 | + } |
| 72 | + }, |
| 73 | + () => { |
| 74 | + failCount++; |
| 75 | + DEBUG_BUILD && debug.warn('[Spotlight] Failed to send envelope to Spotlight Sidecar'); |
| 76 | + }, |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function parseSidecarUrl(url: string): URL | undefined { |
| 83 | + try { |
| 84 | + return new URL(url); |
| 85 | + } catch { |
| 86 | + DEBUG_BUILD && debug.warn(`[Spotlight] Invalid sidecar URL: ${url}`); |
| 87 | + return undefined; |
| 88 | + } |
| 89 | +} |
0 commit comments