-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core,cloudflare): Enable certain fields with env variables #19245
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
Changes from all commits
5882b29
6223f4f
a7d8744
6c2f692
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { envToBool } from '@sentry/core'; | ||
| import type { CloudflareOptions } from './client'; | ||
|
|
||
| /** | ||
|
|
@@ -17,6 +18,12 @@ function isVersionMetadata(value: unknown): value is CfVersionMetadata { | |
| return typeof value === 'object' && value !== null && 'id' in value && typeof value.id === 'string'; | ||
| } | ||
|
|
||
| function getEnvVar<T extends Record<string, unknown>>(env: unknown, varName: keyof T): string | undefined { | ||
| return typeof env === 'object' && env !== null && varName in env && typeof (env as T)[varName] === 'string' | ||
| ? ((env as T)[varName] as string) | ||
| : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Merges the options passed in from the user with the options we read from | ||
| * the Cloudflare `env` environment variable object. | ||
|
|
@@ -31,7 +38,7 @@ function isVersionMetadata(value: unknown): value is CfVersionMetadata { | |
| * | ||
| * @returns The final options. | ||
| */ | ||
| export function getFinalOptions(userOptions: CloudflareOptions, env: unknown): CloudflareOptions { | ||
| export function getFinalOptions(userOptions: CloudflareOptions = {}, env: unknown): CloudflareOptions { | ||
| if (typeof env !== 'object' || env === null) { | ||
| return userOptions; | ||
| } | ||
|
|
@@ -44,5 +51,16 @@ export function getFinalOptions(userOptions: CloudflareOptions, env: unknown): C | |
| ? env.CF_VERSION_METADATA.id | ||
| : undefined; | ||
|
|
||
| return { release, ...userOptions }; | ||
| const tracesSampleRate = | ||
| userOptions.tracesSampleRate ?? parseFloat(getEnvVar(env, 'SENTRY_TRACES_SAMPLE_RATE') ?? ''); | ||
|
|
||
| return { | ||
| release, | ||
| ...userOptions, | ||
| dsn: userOptions.dsn ?? getEnvVar(env, 'SENTRY_DSN'), | ||
|
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: can we also add
Member
Author
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. Great idea. Added. I also added |
||
| environment: userOptions.environment ?? getEnvVar(env, 'SENTRY_ENVIRONMENT'), | ||
| tracesSampleRate: isFinite(tracesSampleRate) ? tracesSampleRate : undefined, | ||
| debug: userOptions.debug ?? envToBool(getEnvVar(env, 'SENTRY_DEBUG')), | ||
|
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.
|
||
| tunnel: userOptions.tunnel ?? getEnvVar(env, 'SENTRY_TUNNEL'), | ||
| }; | ||
|
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. Spread operator overrides env-derived fallback valuesMedium Severity The |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,11 @@ const MOCK_ENV = { | |
| SENTRY_RELEASE: '1.1.1', | ||
| }; | ||
|
|
||
| // Mock env without DSN for tests that should not initialize the SDK | ||
| const MOCK_ENV_WITHOUT_DSN = { | ||
|
Member
Author
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. note: It seems that the SDK was not initialized for the tests where this is used. By now also allowing |
||
| SENTRY_RELEASE: '1.1.1', | ||
| }; | ||
|
|
||
| function addDelayedWaitUntil(context: ExecutionContext) { | ||
| context.waitUntil(new Promise<void>(resolve => setTimeout(() => resolve()))); | ||
| } | ||
|
|
@@ -149,12 +154,12 @@ describe('withSentry', () => { | |
| addDelayedWaitUntil(_context); | ||
| return new Response('test'); | ||
| }, | ||
| } satisfies ExportedHandler<typeof MOCK_ENV>; | ||
| } satisfies ExportedHandler<typeof MOCK_ENV_WITHOUT_DSN>; | ||
|
|
||
| const wrappedHandler = withSentry(vi.fn(), handler); | ||
| const waits: Promise<unknown>[] = []; | ||
| const waitUntil = vi.fn(promise => waits.push(promise)); | ||
| await wrappedHandler.fetch?.(new Request('https://example.com'), MOCK_ENV, { | ||
| await wrappedHandler.fetch?.(new Request('https://example.com'), MOCK_ENV_WITHOUT_DSN, { | ||
| waitUntil, | ||
| } as unknown as ExecutionContext); | ||
| expect(flush).not.toBeCalled(); | ||
|
|
@@ -389,12 +394,12 @@ describe('withSentry', () => { | |
| addDelayedWaitUntil(_context); | ||
| return; | ||
| }, | ||
| } satisfies ExportedHandler<typeof MOCK_ENV>; | ||
| } satisfies ExportedHandler<typeof MOCK_ENV_WITHOUT_DSN>; | ||
|
|
||
| const wrappedHandler = withSentry(vi.fn(), handler); | ||
| const waits: Promise<unknown>[] = []; | ||
| const waitUntil = vi.fn(promise => waits.push(promise)); | ||
| await wrappedHandler.scheduled?.(createMockScheduledController(), MOCK_ENV, { | ||
| await wrappedHandler.scheduled?.(createMockScheduledController(), MOCK_ENV_WITHOUT_DSN, { | ||
| waitUntil, | ||
| } as unknown as ExecutionContext); | ||
| expect(flush).not.toBeCalled(); | ||
|
|
@@ -628,12 +633,12 @@ describe('withSentry', () => { | |
| addDelayedWaitUntil(_context); | ||
| return; | ||
| }, | ||
| } satisfies ExportedHandler<typeof MOCK_ENV>; | ||
| } satisfies ExportedHandler<typeof MOCK_ENV_WITHOUT_DSN>; | ||
|
|
||
| const wrappedHandler = withSentry(vi.fn(), handler); | ||
| const waits: Promise<unknown>[] = []; | ||
| const waitUntil = vi.fn(promise => waits.push(promise)); | ||
| await wrappedHandler.email?.(createMockEmailMessage(), MOCK_ENV, { | ||
| await wrappedHandler.email?.(createMockEmailMessage(), MOCK_ENV_WITHOUT_DSN, { | ||
| waitUntil, | ||
| } as unknown as ExecutionContext); | ||
| expect(flush).not.toBeCalled(); | ||
|
|
@@ -871,12 +876,12 @@ describe('withSentry', () => { | |
| addDelayedWaitUntil(_context); | ||
| return; | ||
| }, | ||
| } satisfies ExportedHandler<typeof MOCK_ENV>; | ||
| } satisfies ExportedHandler<typeof MOCK_ENV_WITHOUT_DSN>; | ||
|
|
||
| const wrappedHandler = withSentry(vi.fn(), handler); | ||
| const waits: Promise<unknown>[] = []; | ||
| const waitUntil = vi.fn(promise => waits.push(promise)); | ||
| await wrappedHandler.queue?.(createMockQueueBatch(), MOCK_ENV, { | ||
| await wrappedHandler.queue?.(createMockQueueBatch(), MOCK_ENV_WITHOUT_DSN, { | ||
| waitUntil, | ||
| } as unknown as ExecutionContext); | ||
| expect(flush).not.toBeCalled(); | ||
|
|
@@ -1069,12 +1074,12 @@ describe('withSentry', () => { | |
| addDelayedWaitUntil(_context); | ||
| return; | ||
| }, | ||
| } satisfies ExportedHandler<typeof MOCK_ENV>; | ||
| } satisfies ExportedHandler<typeof MOCK_ENV_WITHOUT_DSN>; | ||
|
|
||
| const wrappedHandler = withSentry(vi.fn(), handler); | ||
| const waits: Promise<unknown>[] = []; | ||
| const waitUntil = vi.fn(promise => waits.push(promise)); | ||
| await wrappedHandler.tail?.(createMockTailEvent(), MOCK_ENV, { | ||
| await wrappedHandler.tail?.(createMockTailEvent(), MOCK_ENV_WITHOUT_DSN, { | ||
| waitUntil, | ||
| } as unknown as ExecutionContext); | ||
| expect(flush).not.toBeCalled(); | ||
|
|
||


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.
note: I made this return
undefinedas it was also working before theoretically. I adapted the types to also allowundefinedjust to have this case also handled