cloudflare-workers.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  9. import {
  10. getCrashReportJavaScriptInstallStep,
  11. getCrashReportModalConfigDescription,
  12. getCrashReportModalIntroduction,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import {t, tct} from 'sentry/locale';
  15. import {getInstallConfig} from 'sentry/utils/gettingStartedDocs/node';
  16. type Params = DocsParams;
  17. const getSdkConfigureSnippetToml = () => `
  18. compatibility_flags = ["nodejs_compat"]
  19. # compatibility_flags = ["nodejs_als"]
  20. `;
  21. const getSdkConfigureSnippetJson = () => `
  22. {
  23. "compatibility_flags": [
  24. "nodejs_compat"
  25. ],
  26. "compatibility_date": "2024-09-23"
  27. }`;
  28. const getSdkSetupSnippet = (params: Params) => `
  29. import * as Sentry from "@sentry/cloudflare";
  30. export default Sentry.withSentry(
  31. env => ({
  32. dsn: "${params.dsn.public}",
  33. // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
  34. // Learn more at
  35. // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  36. tracesSampleRate: 1.0,
  37. }),
  38. {
  39. async fetch(request, env, ctx) {
  40. return new Response('Hello World!');
  41. },
  42. } satisfies ExportedHandler<Env>,
  43. );`;
  44. const getVerifySnippet = () => `
  45. setTimeout(() => {
  46. throw new Error();
  47. });`;
  48. const onboarding: OnboardingConfig = {
  49. introduction: () =>
  50. t(
  51. 'In this quick guide you’ll set up and configure the Sentry Cloudflare SDK for the use in your Cloudflare Workers application.'
  52. ),
  53. install: params => [
  54. {
  55. type: StepType.INSTALL,
  56. description: t('Add the Sentry Cloudflare SDK as a dependency:'),
  57. configurations: getInstallConfig(params, {
  58. basePackage: '@sentry/cloudflare',
  59. }),
  60. },
  61. ],
  62. configure: params => [
  63. {
  64. type: StepType.CONFIGURE,
  65. description: t(
  66. "Configuration should happen as early as possible in your application's lifecycle."
  67. ),
  68. configurations: [
  69. {
  70. description: tct(
  71. "To use the SDK, you'll need to set either the [code:nodejs_compat] or [code:nodejs_als] compatibility flags in your [code:wrangler.json]/[code:wrangler.toml]. This is because the SDK needs access to the [code:AsyncLocalStorage] API to work correctly.",
  72. {
  73. code: <code />,
  74. }
  75. ),
  76. code: [
  77. {
  78. label: 'JSON',
  79. value: 'json',
  80. language: 'json',
  81. filename: 'wrangler.json',
  82. code: getSdkConfigureSnippetJson(),
  83. },
  84. {
  85. label: 'Toml',
  86. value: 'toml',
  87. language: 'toml',
  88. filename: 'wrangler.toml',
  89. code: getSdkConfigureSnippetToml(),
  90. },
  91. ],
  92. },
  93. {
  94. description: tct(
  95. 'In order to initialize the SDK, wrap your handler with the [code:withSentry] function. Note that you can turn off almost all side effects using the respective options.',
  96. {
  97. code: <code />,
  98. guideLink: (
  99. <ExternalLink href="https://developers.cloudflare.com/pages/functions/middleware/" />
  100. ),
  101. }
  102. ),
  103. code: [
  104. {
  105. label: 'TypeScript',
  106. value: 'typescript',
  107. language: 'typescript',
  108. code: getSdkSetupSnippet(params),
  109. },
  110. ],
  111. },
  112. ],
  113. },
  114. getUploadSourceMapsStep({
  115. guideLink:
  116. 'https://docs.sentry.io/platforms/javascript/guides/cloudflare/sourcemaps/',
  117. ...params,
  118. }),
  119. ],
  120. verify: () => [
  121. {
  122. type: StepType.VERIFY,
  123. description: t(
  124. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  125. ),
  126. configurations: [
  127. {
  128. language: 'javascript',
  129. code: getVerifySnippet(),
  130. },
  131. ],
  132. },
  133. ],
  134. };
  135. const crashReportOnboarding: OnboardingConfig = {
  136. introduction: () => getCrashReportModalIntroduction(),
  137. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  138. configure: () => [
  139. {
  140. type: StepType.CONFIGURE,
  141. description: getCrashReportModalConfigDescription({
  142. link: 'https://docs.sentry.io/platforms/javascript/guides/cloudflare/user-feedback/configuration/#crash-report-modal',
  143. }),
  144. },
  145. ],
  146. verify: () => [],
  147. nextSteps: () => [],
  148. };
  149. const docs: Docs = {
  150. onboarding,
  151. crashReportOnboarding,
  152. };
  153. export default docs;