-
-
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 3 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,15 @@ 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_TRACE_SAMPLE_RATE') ?? ''); | ||
|
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. Env variable name typo: missing 'S' in TRACESHigh Severity The code reads |
||
|
|
||
| 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 |
||
| } | ||


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