javascript.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import {css} from '@emotion/react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  4. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  5. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  6. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  7. import type {
  8. BasePlatformOptions,
  9. Docs,
  10. DocsParams,
  11. OnboardingConfig,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  13. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  14. import {
  15. getCrashReportJavaScriptInstallStep,
  16. getCrashReportModalConfigDescription,
  17. getCrashReportModalIntroduction,
  18. getFeedbackConfigOptions,
  19. getFeedbackConfigureDescription,
  20. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  21. import {getJSMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  22. import {
  23. getProfilingDocumentHeaderConfigurationStep,
  24. MaybeBrowserProfilingBetaWarning,
  25. } from 'sentry/components/onboarding/gettingStartedDoc/utils/profilingOnboarding';
  26. import {
  27. getReplayConfigOptions,
  28. getReplayConfigureDescription,
  29. getReplayVerifyStep,
  30. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  31. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  32. import {t, tct} from 'sentry/locale';
  33. import {space} from 'sentry/styles/space';
  34. import {trackAnalytics} from 'sentry/utils/analytics';
  35. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  36. import {updateDynamicSdkLoaderOptions} from './jsLoader/updateDynamicSdkLoaderOptions';
  37. export enum InstallationMode {
  38. AUTO = 'auto',
  39. MANUAL = 'manual',
  40. }
  41. const platformOptions = {
  42. installationMode: {
  43. label: t('Installation Mode'),
  44. items: [
  45. {
  46. label: t('Loader Script'),
  47. value: InstallationMode.AUTO,
  48. },
  49. {
  50. label: t('Npm/Yarn'),
  51. value: InstallationMode.MANUAL,
  52. },
  53. ],
  54. defaultValue: InstallationMode.AUTO,
  55. },
  56. } satisfies BasePlatformOptions;
  57. type PlatformOptions = typeof platformOptions;
  58. type Params = DocsParams<PlatformOptions>;
  59. const isAutoInstall = (params: Params) =>
  60. params.platformOptions.installationMode === InstallationMode.AUTO;
  61. const getSdkSetupSnippet = (params: Params) => `
  62. import * as Sentry from "@sentry/browser";
  63. Sentry.init({
  64. dsn: "${params.dsn.public}",
  65. integrations: [${
  66. params.isPerformanceSelected
  67. ? `
  68. Sentry.browserTracingIntegration(),`
  69. : ''
  70. }${
  71. params.isProfilingSelected
  72. ? `
  73. Sentry.browserProfilingIntegration(),`
  74. : ''
  75. }${
  76. params.isFeedbackSelected
  77. ? `
  78. Sentry.feedbackIntegration({
  79. // Additional SDK configuration goes in here, for example:
  80. colorScheme: "system",
  81. ${getFeedbackConfigOptions(params.feedbackOptions)}}),`
  82. : ''
  83. }${
  84. params.isReplaySelected
  85. ? `
  86. Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)}),`
  87. : ''
  88. }
  89. ],${
  90. params.isPerformanceSelected
  91. ? `
  92. // Tracing
  93. tracesSampleRate: 1.0, // Capture 100% of the transactions
  94. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  95. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  96. : ''
  97. }${
  98. params.isReplaySelected
  99. ? `
  100. // Session Replay
  101. replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
  102. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  103. : ''
  104. }${
  105. params.isProfilingSelected
  106. ? `
  107. // Set profilesSampleRate to 1.0 to profile every transaction.
  108. // Since profilesSampleRate is relative to tracesSampleRate,
  109. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  110. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  111. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  112. profilesSampleRate: 1.0,`
  113. : ''
  114. }
  115. });
  116. `;
  117. const getVerifyJSSnippet = () => `
  118. myUndefinedFunction();`;
  119. const getInstallConfig = () => [
  120. {
  121. language: 'bash',
  122. code: [
  123. {
  124. label: 'npm',
  125. value: 'npm',
  126. language: 'bash',
  127. code: 'npm install --save @sentry/browser',
  128. },
  129. {
  130. label: 'yarn',
  131. value: 'yarn',
  132. language: 'bash',
  133. code: 'yarn add @sentry/browser',
  134. },
  135. ],
  136. },
  137. ];
  138. const getVerifyConfig = () => [
  139. {
  140. type: StepType.VERIFY,
  141. description: t(
  142. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  143. ),
  144. configurations: [
  145. {
  146. code: [
  147. {
  148. label: 'Javascript',
  149. value: 'javascript',
  150. language: 'javascript',
  151. code: getVerifyJSSnippet(),
  152. },
  153. ],
  154. },
  155. ],
  156. },
  157. ];
  158. const loaderScriptOnboarding: OnboardingConfig<PlatformOptions> = {
  159. introduction: () =>
  160. tct('In this quick guide you’ll use our [strong: Loader Script] to set up:', {
  161. strong: <strong />,
  162. }),
  163. install: params => [
  164. {
  165. type: StepType.INSTALL,
  166. description: t('Add this script tag to the top of the page:'),
  167. configurations: [
  168. {
  169. language: 'html',
  170. code: [
  171. {
  172. label: 'HTML',
  173. value: 'html',
  174. language: 'html',
  175. code: `
  176. <script
  177. src="${params.dsn.cdn}"
  178. crossorigin="anonymous"
  179. ></script>`,
  180. },
  181. ],
  182. },
  183. ],
  184. },
  185. ],
  186. configure: params => [
  187. {
  188. title: t('Configure SDK (Optional)'),
  189. description: t(
  190. "Initialize Sentry as early as possible in your application's lifecycle."
  191. ),
  192. collapsible: true,
  193. configurations: [
  194. {
  195. language: 'html',
  196. code: [
  197. {
  198. label: 'HTML',
  199. value: 'html',
  200. language: 'html',
  201. code: `
  202. <script>
  203. Sentry.onLoad(function() {
  204. Sentry.init({${
  205. !(params.isPerformanceSelected || params.isReplaySelected)
  206. ? `
  207. // You can add any additional configuration here`
  208. : ''
  209. }${
  210. params.isPerformanceSelected
  211. ? `
  212. // Tracing
  213. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  214. : ''
  215. }${
  216. params.isReplaySelected
  217. ? `
  218. // Session Replay
  219. replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
  220. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  221. : ''
  222. }
  223. });
  224. });
  225. </script>`,
  226. },
  227. ],
  228. },
  229. ],
  230. onOptionalToggleClick: showOptionalConfig => {
  231. if (showOptionalConfig) {
  232. trackAnalytics('onboarding.js_loader_npm_docs_optional_shown', {
  233. organization: params.organization,
  234. platform: params.platformKey,
  235. project_id: params.projectId,
  236. });
  237. }
  238. },
  239. },
  240. ],
  241. verify: getVerifyConfig,
  242. nextSteps: () => [
  243. {
  244. id: 'source-maps',
  245. name: t('Source Maps'),
  246. description: t('Learn how to enable readable stack traces in your Sentry errors.'),
  247. link: 'https://docs.sentry.io/platforms/javascript/sourcemaps/',
  248. },
  249. ],
  250. onPageLoad: params => {
  251. return () => {
  252. trackAnalytics('onboarding.setup_loader_docs_rendered', {
  253. organization: params.organization,
  254. platform: params.platformKey,
  255. project_id: params.projectId,
  256. });
  257. };
  258. },
  259. onPlatformOptionsChange: params => {
  260. return () => {
  261. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  262. organization: params.organization,
  263. platform: params.platformKey,
  264. project_id: params.projectId,
  265. });
  266. };
  267. },
  268. onProductSelectionChange: params => {
  269. return products => {
  270. updateDynamicSdkLoaderOptions({
  271. orgSlug: params.organization.slug,
  272. projectSlug: params.projectSlug,
  273. products,
  274. projectKey: params.projectKeyId,
  275. api: params.api,
  276. });
  277. };
  278. },
  279. onProductSelectionLoad: params => {
  280. return products => {
  281. updateDynamicSdkLoaderOptions({
  282. orgSlug: params.organization.slug,
  283. projectSlug: params.projectSlug,
  284. products,
  285. projectKey: params.projectKeyId,
  286. api: params.api,
  287. });
  288. };
  289. },
  290. };
  291. const packageManagerOnboarding: OnboardingConfig<PlatformOptions> = {
  292. introduction: () =>
  293. tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  294. strong: <strong />,
  295. }),
  296. install: () => [
  297. {
  298. type: StepType.INSTALL,
  299. description: t(
  300. 'Sentry captures data by using an SDK within your application’s runtime.'
  301. ),
  302. configurations: getInstallConfig(),
  303. },
  304. ],
  305. configure: params => [
  306. {
  307. type: StepType.CONFIGURE,
  308. description: t(
  309. "Initialize Sentry as early as possible in your application's lifecycle."
  310. ),
  311. configurations: [
  312. {
  313. code: [
  314. {
  315. label: 'JavaScript',
  316. value: 'javascript',
  317. language: 'javascript',
  318. code: getSdkSetupSnippet(params),
  319. },
  320. ],
  321. },
  322. ...(params.isProfilingSelected
  323. ? [getProfilingDocumentHeaderConfigurationStep()]
  324. : []),
  325. ],
  326. },
  327. getUploadSourceMapsStep({
  328. guideLink: 'https://docs.sentry.io/platforms/javascript/sourcemaps/',
  329. ...params,
  330. }),
  331. ],
  332. verify: getVerifyConfig,
  333. nextSteps: () => [],
  334. onPageLoad: params => {
  335. return () => {
  336. trackAnalytics('onboarding.js_loader_npm_docs_shown', {
  337. organization: params.organization,
  338. platform: params.platformKey,
  339. project_id: params.projectId,
  340. });
  341. };
  342. },
  343. onPlatformOptionsChange: params => {
  344. return () => {
  345. trackAnalytics('onboarding.setup_loader_docs_rendered', {
  346. organization: params.organization,
  347. platform: params.platformKey,
  348. project_id: params.projectId,
  349. });
  350. };
  351. },
  352. };
  353. const onboarding: OnboardingConfig<PlatformOptions> = {
  354. introduction: params => (
  355. <div
  356. css={css`
  357. display: flex;
  358. flex-direction: column;
  359. gap: ${space(1)};
  360. `}
  361. >
  362. <MaybeBrowserProfilingBetaWarning {...params} />
  363. <TextBlock noMargin>
  364. {isAutoInstall(params)
  365. ? loaderScriptOnboarding.introduction?.(params)
  366. : packageManagerOnboarding.introduction?.(params)}
  367. </TextBlock>
  368. </div>
  369. ),
  370. install: params =>
  371. isAutoInstall(params)
  372. ? loaderScriptOnboarding.install(params)
  373. : packageManagerOnboarding.install(params),
  374. configure: (params: Params) =>
  375. isAutoInstall(params)
  376. ? loaderScriptOnboarding.configure(params)
  377. : packageManagerOnboarding.configure(params),
  378. verify: params =>
  379. isAutoInstall(params)
  380. ? loaderScriptOnboarding.verify(params)
  381. : packageManagerOnboarding.verify(params),
  382. nextSteps: params =>
  383. isAutoInstall(params)
  384. ? loaderScriptOnboarding.nextSteps?.(params)
  385. : packageManagerOnboarding.nextSteps?.(params),
  386. onPageLoad: params =>
  387. isAutoInstall(params)
  388. ? loaderScriptOnboarding.onPageLoad?.(params)
  389. : packageManagerOnboarding.onPageLoad?.(params),
  390. onProductSelectionChange: params =>
  391. isAutoInstall(params)
  392. ? loaderScriptOnboarding.onProductSelectionChange?.(params)
  393. : packageManagerOnboarding.onProductSelectionChange?.(params),
  394. onPlatformOptionsChange: params =>
  395. isAutoInstall(params)
  396. ? loaderScriptOnboarding.onPlatformOptionsChange?.(params)
  397. : packageManagerOnboarding.onPlatformOptionsChange?.(params),
  398. onProductSelectionLoad: params =>
  399. isAutoInstall(params)
  400. ? loaderScriptOnboarding.onProductSelectionLoad?.(params)
  401. : packageManagerOnboarding.onProductSelectionLoad?.(params),
  402. };
  403. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  404. install: () => [
  405. {
  406. type: StepType.INSTALL,
  407. description: tct(
  408. 'For the Session Replay to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/react]) installed, minimum version 7.27.0.',
  409. {
  410. code: <code />,
  411. }
  412. ),
  413. configurations: getInstallConfig(),
  414. },
  415. ],
  416. configure: (params: Params) => [
  417. {
  418. type: StepType.CONFIGURE,
  419. description: getReplayConfigureDescription({
  420. link: 'https://docs.sentry.io/platforms/javascript/session-replay/',
  421. }),
  422. configurations: [
  423. {
  424. code: [
  425. {
  426. label: 'JavaScript',
  427. value: 'javascript',
  428. language: 'javascript',
  429. code: getSdkSetupSnippet(params),
  430. },
  431. ],
  432. },
  433. ],
  434. additionalInfo: <TracePropagationMessage />,
  435. },
  436. ],
  437. verify: getReplayVerifyStep(),
  438. nextSteps: () => [],
  439. };
  440. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  441. install: () => [
  442. {
  443. type: StepType.INSTALL,
  444. description: tct(
  445. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/react]) installed, minimum version 7.85.0.',
  446. {
  447. code: <code />,
  448. }
  449. ),
  450. configurations: getInstallConfig(),
  451. },
  452. ],
  453. configure: (params: Params) => [
  454. {
  455. type: StepType.CONFIGURE,
  456. description: getFeedbackConfigureDescription({
  457. linkConfig:
  458. 'https://docs.sentry.io/platforms/javascript/user-feedback/configuration/',
  459. linkButton:
  460. 'https://docs.sentry.io/platforms/javascript/user-feedback/configuration/#bring-your-own-button',
  461. }),
  462. configurations: [
  463. {
  464. code: [
  465. {
  466. label: 'JavaScript',
  467. value: 'javascript',
  468. language: 'javascript',
  469. code: getSdkSetupSnippet(params),
  470. },
  471. ],
  472. },
  473. ],
  474. additionalInfo: crashReportCallout({
  475. link: 'https://docs.sentry.io/platforms/javascript/user-feedback/#crash-report-modal',
  476. }),
  477. },
  478. ],
  479. verify: () => [],
  480. nextSteps: () => [],
  481. };
  482. const crashReportOnboarding: OnboardingConfig<PlatformOptions> = {
  483. introduction: () => getCrashReportModalIntroduction(),
  484. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  485. configure: () => [
  486. {
  487. type: StepType.CONFIGURE,
  488. description: getCrashReportModalConfigDescription({
  489. link: 'https://docs.sentry.io/platforms/javascript/user-feedback/configuration/#crash-report-modal',
  490. }),
  491. additionalInfo: widgetCallout({
  492. link: 'https://docs.sentry.io/platforms/javascript/user-feedback/#user-feedback-widget',
  493. }),
  494. },
  495. ],
  496. verify: () => [],
  497. nextSteps: () => [],
  498. };
  499. const performanceOnboarding: OnboardingConfig<PlatformOptions> = {
  500. introduction: () =>
  501. t(
  502. "Adding Performance to your Browser JavaScript project is simple. Make sure you've got these basics down."
  503. ),
  504. install: () => [
  505. {
  506. type: StepType.INSTALL,
  507. description: tct(
  508. 'Install our JavaScript browser SDK using either [code:yarn] or [code:npm]:',
  509. {code: <code />}
  510. ),
  511. configurations: getInstallConfig(),
  512. },
  513. ],
  514. configure: params => [
  515. {
  516. type: StepType.CONFIGURE,
  517. description: t(
  518. "Configuration should happen as early as possible in your application's lifecycle."
  519. ),
  520. configurations: [
  521. {
  522. language: 'javascript',
  523. code: `
  524. import * as Sentry from "@sentry/browser";
  525. Sentry.init({
  526. dsn: "${params.dsn.public}",
  527. integrations: [Sentry.browserTracingIntegration()],
  528. // Set tracesSampleRate to 1.0 to capture 100%
  529. // of transactions for performance monitoring.
  530. // We recommend adjusting this value in production
  531. tracesSampleRate: 1.0,
  532. // Set \`tracePropagationTargets\` to control for which URLs distributed tracing should be enabled
  533. tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
  534. });
  535. `,
  536. additionalInfo: tct(
  537. 'We recommend adjusting the value of [code:tracesSampleRate] in production. Learn more about tracing [linkTracingOptions:options], how to use the [linkTracesSampler:traces_sampler] function, or how to [linkSampleTransactions:sample transactions].',
  538. {
  539. code: <code />,
  540. linkTracingOptions: (
  541. <ExternalLink href="https://docs.sentry.io/platforms/javascript/configuration/options/#tracing-options" />
  542. ),
  543. linkTracesSampler: (
  544. <ExternalLink href="https://docs.sentry.io/platforms/javascript/configuration/sampling/" />
  545. ),
  546. linkSampleTransactions: (
  547. <ExternalLink href="https://docs.sentry.io/platforms/javascript/configuration/sampling/" />
  548. ),
  549. }
  550. ),
  551. },
  552. ],
  553. },
  554. ],
  555. verify: () => [
  556. {
  557. type: StepType.VERIFY,
  558. description: tct(
  559. 'Verify that performance monitoring is working correctly with our [link:automatic instrumentation] by simply using your JavaScript application.',
  560. {
  561. link: (
  562. <ExternalLink href="https://docs.sentry.io/platforms/javascript/tracing/instrumentation/automatic-instrumentation/" />
  563. ),
  564. }
  565. ),
  566. configurations: [
  567. {
  568. description: tct(
  569. 'You have the option to manually construct a transaction using [link:custom instrumentation].',
  570. {
  571. link: (
  572. <ExternalLink href="https://docs.sentry.io/platforms/javascript/tracing/instrumentation/custom-instrumentation/" />
  573. ),
  574. }
  575. ),
  576. language: 'javascript',
  577. code: `
  578. const transaction = Sentry.startTransaction({ name: "test-transaction" });
  579. const span = transaction.startChild({ op: "functionX" }); // This function returns a Span
  580. // exampleFunctionCall();
  581. span.finish(); // Remember that only finished spans will be sent with the transaction
  582. transaction.finish(); // Finishing the transaction will send it to Sentry`,
  583. },
  584. ],
  585. },
  586. ],
  587. nextSteps: () => [],
  588. };
  589. const profilingOnboarding: OnboardingConfig<PlatformOptions> = {
  590. ...onboarding,
  591. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  592. };
  593. const docs: Docs<PlatformOptions> = {
  594. onboarding,
  595. feedbackOnboardingNpm: feedbackOnboarding,
  596. replayOnboarding,
  597. replayOnboardingJsLoader,
  598. performanceOnboarding,
  599. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  600. crashReportOnboarding,
  601. platformOptions,
  602. profilingOnboarding,
  603. };
  604. export default docs;