-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add environment variable support for Spotlight configuration #18198
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
base: develop
Are you sure you want to change the base?
Changes from 4 commits
6cb5513
f16380f
fda3d61
bcff50d
cd42ebd
0ac2828
0915965
6ea8d90
870be3b
1cf7f7f
b6fda2b
469940b
be6f1b5
c9b13e3
d129a79
f542f62
4c28587
f9b555a
e12b3fc
ab56171
6cbd1fd
3bc5309
c78da67
fb3f764
d556863
64c4692
75f2b49
9f1e28f
a23d53a
90eca4e
f16915a
c7c9b95
cb298e2
5fdcf7d
c12edfc
2ea8c45
433c0ac
212a8bb
516ede0
5df223a
102dc61
d4662e5
7cffb67
dde88ca
9c6d19a
de9943d
f7d1ffd
3cf3a95
42d832c
4c6aaad
d9d7124
a313e1c
77be213
0d69896
e32150d
2346b4c
e549260
93eceb5
33df6a0
669c71b
8d639f4
ca898d9
855ff97
5deeb64
9ef14ff
fcab9df
693c80e
7c1107f
9a9ba91
65860d4
1ab6f28
7501e60
351046b
eca64b5
0ad25a3
b9d3337
b2716ec
94d86a0
43572cf
df539cf
c23f1d7
e5409e6
bf2e634
b16f01d
40c4967
1642e35
67d1d4b
ce30f02
5bc01a0
939e0cd
f24fa13
c7652fe
bb65b10
d62b54e
12481ec
d873831
4f2f69d
0a8c255
bae7fb6
d72f2f2
87f6dc6
f00a377
6465550
8d3702a
1c05b4c
130bd92
70435c7
7d9e165
6e7eb39
ad6b021
03eb6e5
73ea2d6
51262cf
fa8debc
ee9ae35
4189085
86cce1f
76980e2
1614dbb
7b8612c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * Safely gets an environment variable value with defensive guards for browser environments. | ||
| * Checks process.env which is transformed by most bundlers (Webpack, Vite, Rollup, Rspack, Parcel, etc.) | ||
| * at build time. | ||
| * | ||
| * Note: We don't check import.meta.env because: | ||
| * 1. Bundlers only replace static references like `import.meta.env.VITE_VAR`, not dynamic access | ||
| * 2. Dynamic access causes syntax errors in unsupported environments | ||
| * 3. Most bundlers transform process.env references anyway | ||
|
||
| * | ||
| * @param key - The environment variable key to look up | ||
| * @returns The value of the environment variable or undefined if not found | ||
| */ | ||
| export function getEnvValue(key: string): string | undefined { | ||
| try { | ||
| if (typeof process !== 'undefined' && process.env) { | ||
| const value = process.env[key]; | ||
| if (value !== undefined) { | ||
| return value; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| // Silently ignore - process might not be accessible or might throw in some environments | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: VITE_SENTRY_SPOTLIGHT env var won't work in standard Vite appsThe Additional Locations (1) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { debug, envToBool } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import { getEnvValue } from './env'; | ||
|
|
||
| /** | ||
| * Environment variable keys to check for Spotlight configuration, in priority order. | ||
| * The first one found with a value will be used. | ||
| * | ||
| * IMPORTANT: Framework-specific variables (PUBLIC_*, NEXT_PUBLIC_*, etc.) are prioritized | ||
| * over the generic SENTRY_SPOTLIGHT to support Docker Compose setups where: | ||
| * - Backend services need SENTRY_SPOTLIGHT=http://host.internal.docker:8969/stream | ||
| * - Frontend code needs localhost (via framework-specific vars like NEXT_PUBLIC_SENTRY_SPOTLIGHT=http://localhost:8969/stream) | ||
| * | ||
| * SENTRY_SPOTLIGHT is kept as a fallback for: | ||
| * - Simple non-Docker setups | ||
| * - Remote Spotlight instances when no framework-specific var is set | ||
| */ | ||
| const SPOTLIGHT_ENV_KEYS = [ | ||
| 'PUBLIC_SENTRY_SPOTLIGHT', // SvelteKit, Astro, Qwik | ||
| 'NEXT_PUBLIC_SENTRY_SPOTLIGHT', // Next.js | ||
| 'VITE_SENTRY_SPOTLIGHT', // Vite | ||
| 'NUXT_PUBLIC_SENTRY_SPOTLIGHT', // Nuxt | ||
| 'REACT_APP_SENTRY_SPOTLIGHT', // Create React App | ||
| 'VUE_APP_SENTRY_SPOTLIGHT', // Vue CLI | ||
| 'GATSBY_SENTRY_SPOTLIGHT', // Gatsby | ||
| 'SENTRY_SPOTLIGHT', // Fallback/base name - works in Parcel, Webpack, Rspack, Rollup, Rolldown, Node.js | ||
| ] as const; | ||
|
|
||
| /** | ||
| * Gets the Spotlight configuration from environment variables. | ||
| * Checks multiple environment variable prefixes in priority order to support | ||
| * different bundlers and frameworks. | ||
| * | ||
| * @returns The resolved Spotlight configuration (boolean | string | undefined) | ||
| */ | ||
| export function getSpotlightConfig(): boolean | string | undefined { | ||
| for (const key of SPOTLIGHT_ENV_KEYS) { | ||
| const value = getEnvValue(key); | ||
|
|
||
| if (value !== undefined) { | ||
| // Try to parse as boolean first (strict mode) | ||
| const boolValue = envToBool(value, { strict: true }); | ||
|
|
||
| if (boolValue !== null) { | ||
| // It's a valid boolean value | ||
| if (DEBUG_BUILD) { | ||
| debug.log(`[Spotlight] Found ${key}=${String(boolValue)} in environment variables`); | ||
| } | ||
| return boolValue; | ||
| } | ||
|
|
||
| // Not a boolean, treat as custom URL string | ||
| if (DEBUG_BUILD) { | ||
| debug.log(`[Spotlight] Found ${key}=${value} (custom URL) in environment variables`); | ||
| } | ||
| return value; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // No Spotlight configuration found in environment | ||
| return undefined; | ||
|
Comment on lines
62
to
65
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: neat size trick: You can let the function return |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.