react.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import {Fragment} from '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. Docs,
  9. DocsParams,
  10. OnboardingConfig,
  11. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  12. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  13. import {
  14. getCrashReportJavaScriptInstallStep,
  15. getCrashReportModalConfigDescription,
  16. getCrashReportModalIntroduction,
  17. getFeedbackConfigOptions,
  18. getFeedbackConfigureDescription,
  19. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  20. import {getJSMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  21. import {
  22. getProfilingDocumentHeaderConfigurationStep,
  23. MaybeBrowserProfilingBetaWarning,
  24. } from 'sentry/components/onboarding/gettingStartedDoc/utils/profilingOnboarding';
  25. import {
  26. getReplayConfigOptions,
  27. getReplayConfigureDescription,
  28. getReplayVerifyStep,
  29. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  30. import {featureFlagOnboarding} from 'sentry/gettingStartedDocs/javascript/javascript';
  31. import {t, tct} from 'sentry/locale';
  32. type Params = DocsParams;
  33. const getSdkSetupSnippet = (params: Params) => `
  34. import * as Sentry from "@sentry/react";
  35. Sentry.init({
  36. dsn: "${params.dsn.public}",
  37. integrations: [${
  38. params.isPerformanceSelected
  39. ? `
  40. Sentry.browserTracingIntegration(),`
  41. : ''
  42. }${
  43. params.isProfilingSelected
  44. ? `
  45. Sentry.browserProfilingIntegration(),`
  46. : ''
  47. }${
  48. params.isFeedbackSelected
  49. ? `
  50. Sentry.feedbackIntegration({
  51. // Additional SDK configuration goes in here, for example:
  52. colorScheme: "system",
  53. ${getFeedbackConfigOptions(params.feedbackOptions)}}),`
  54. : ''
  55. }${
  56. params.isReplaySelected
  57. ? `
  58. Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)}),`
  59. : ''
  60. }
  61. ],${
  62. params.isPerformanceSelected
  63. ? `
  64. // Tracing
  65. tracesSampleRate: 1.0, // Capture 100% of the transactions
  66. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  67. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  68. : ''
  69. }${
  70. params.isProfilingSelected
  71. ? `
  72. // Set profilesSampleRate to 1.0 to profile every transaction.
  73. // Since profilesSampleRate is relative to tracesSampleRate,
  74. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  75. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  76. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  77. profilesSampleRate: 1.0,`
  78. : ''
  79. }${
  80. params.isReplaySelected
  81. ? `
  82. // Session Replay
  83. 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.
  84. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  85. : ''
  86. }
  87. });
  88. const container = document.getElementById(“app”);
  89. const root = createRoot(container);
  90. root.render(<App />);
  91. `;
  92. const getVerifySnippet = () => `
  93. return <button onClick={() => {throw new Error("This is your first error!");}}>Break the world</button>;
  94. `;
  95. const getInstallConfig = () => [
  96. {
  97. language: 'bash',
  98. code: [
  99. {
  100. label: 'npm',
  101. value: 'npm',
  102. language: 'bash',
  103. code: 'npm install --save @sentry/react',
  104. },
  105. {
  106. label: 'yarn',
  107. value: 'yarn',
  108. language: 'bash',
  109. code: 'yarn add @sentry/react',
  110. },
  111. ],
  112. },
  113. ];
  114. const onboarding: OnboardingConfig = {
  115. introduction: params => (
  116. <Fragment>
  117. <MaybeBrowserProfilingBetaWarning {...params} />
  118. <p>
  119. {tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  120. strong: <strong />,
  121. })}
  122. </p>
  123. </Fragment>
  124. ),
  125. install: () => [
  126. {
  127. type: StepType.INSTALL,
  128. description: tct(
  129. 'Add the Sentry SDK as a dependency using [code:npm] or [code:yarn]:',
  130. {code: <code />}
  131. ),
  132. configurations: getInstallConfig(),
  133. },
  134. ],
  135. configure: (params: Params) => [
  136. {
  137. type: StepType.CONFIGURE,
  138. description: t(
  139. "Initialize Sentry as early as possible in your application's lifecycle."
  140. ),
  141. configurations: [
  142. {
  143. code: [
  144. {
  145. label: 'JavaScript',
  146. value: 'javascript',
  147. language: 'javascript',
  148. code: getSdkSetupSnippet(params),
  149. },
  150. ],
  151. },
  152. ...(params.isProfilingSelected
  153. ? [getProfilingDocumentHeaderConfigurationStep()]
  154. : []),
  155. ],
  156. },
  157. getUploadSourceMapsStep({
  158. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/react/sourcemaps/',
  159. ...params,
  160. }),
  161. ],
  162. verify: () => [
  163. {
  164. type: StepType.VERIFY,
  165. description: t(
  166. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  167. ),
  168. configurations: [
  169. {
  170. code: [
  171. {
  172. label: 'React',
  173. value: 'react',
  174. language: 'javascript',
  175. code: getVerifySnippet(),
  176. },
  177. ],
  178. },
  179. ],
  180. },
  181. ],
  182. nextSteps: () => [
  183. {
  184. id: 'react-features',
  185. name: t('React Features'),
  186. description: t('Learn about our first class integration with the React framework.'),
  187. link: 'https://docs.sentry.io/platforms/javascript/guides/react/features/',
  188. },
  189. {
  190. id: 'react-router',
  191. name: t('React Router'),
  192. description: t(
  193. 'Configure routing, so Sentry can generate parameterized transaction names for a better overview on the Performance page.'
  194. ),
  195. link: 'https://docs.sentry.io/platforms/javascript/guides/react/configuration/integrations/react-router/',
  196. },
  197. ],
  198. };
  199. const replayOnboarding: OnboardingConfig = {
  200. install: () => [
  201. {
  202. type: StepType.INSTALL,
  203. description: tct(
  204. 'Add the Sentry SDK as a dependency using [code:npm] or [code:yarn]. You need a minimum version 7.27.0 of [code:@sentry/react] in order to use Session Replay. You do not need to install any additional packages.',
  205. {code: <code />}
  206. ),
  207. configurations: getInstallConfig(),
  208. },
  209. ],
  210. configure: (params: Params) => [
  211. {
  212. type: StepType.CONFIGURE,
  213. description: getReplayConfigureDescription({
  214. link: 'https://docs.sentry.io/platforms/javascript/guides/react/session-replay/',
  215. }),
  216. configurations: [
  217. {
  218. code: [
  219. {
  220. label: 'JavaScript',
  221. value: 'javascript',
  222. language: 'javascript',
  223. code: getSdkSetupSnippet(params),
  224. },
  225. ],
  226. additionalInfo: <TracePropagationMessage />,
  227. },
  228. ],
  229. },
  230. ],
  231. verify: getReplayVerifyStep(),
  232. nextSteps: () => [],
  233. };
  234. const feedbackOnboarding: OnboardingConfig = {
  235. install: () => [
  236. {
  237. type: StepType.INSTALL,
  238. description: tct(
  239. '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.',
  240. {
  241. code: <code />,
  242. }
  243. ),
  244. configurations: getInstallConfig(),
  245. },
  246. ],
  247. configure: (params: Params) => [
  248. {
  249. type: StepType.CONFIGURE,
  250. description: getFeedbackConfigureDescription({
  251. linkConfig:
  252. 'https://docs.sentry.io/platforms/javascript/guides/react/user-feedback/configuration/',
  253. linkButton:
  254. 'https://docs.sentry.io/platforms/javascript/guides/react/user-feedback/configuration/#bring-your-own-button',
  255. }),
  256. configurations: [
  257. {
  258. code: [
  259. {
  260. label: 'JavaScript',
  261. value: 'javascript',
  262. language: 'javascript',
  263. code: getSdkSetupSnippet(params),
  264. },
  265. ],
  266. },
  267. ],
  268. additionalInfo: crashReportCallout({
  269. link: 'https://docs.sentry.io/platforms/javascript/guides/react/user-feedback/#crash-report-modal',
  270. }),
  271. },
  272. ],
  273. verify: () => [],
  274. nextSteps: () => [],
  275. };
  276. const crashReportOnboarding: OnboardingConfig = {
  277. introduction: () => getCrashReportModalIntroduction(),
  278. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  279. configure: () => [
  280. {
  281. type: StepType.CONFIGURE,
  282. description: getCrashReportModalConfigDescription({
  283. link: 'https://docs.sentry.io/platforms/javascript/guides/react/user-feedback/configuration/#crash-report-modal',
  284. }),
  285. additionalInfo: widgetCallout({
  286. link: 'https://docs.sentry.io/platforms/javascript/guides/react/user-feedback/#user-feedback-widget',
  287. }),
  288. },
  289. ],
  290. verify: () => [],
  291. nextSteps: () => [],
  292. };
  293. const performanceOnboarding: OnboardingConfig = {
  294. introduction: () =>
  295. t(
  296. "Adding Performance to your React project is simple. Make sure you've got these basics down."
  297. ),
  298. install: onboarding.install,
  299. configure: params => [
  300. {
  301. type: StepType.CONFIGURE,
  302. description: t(
  303. "Configuration should happen as early as possible in your application's lifecycle."
  304. ),
  305. configurations: [
  306. {
  307. language: 'javascript',
  308. code: `
  309. import React from "react";
  310. import ReactDOM from "react-dom";
  311. import * as Sentry from "@sentry/react";
  312. import App from "./App";
  313. Sentry.init({
  314. dsn: "${params.dsn.public}",
  315. integrations: [Sentry.browserTracingIntegration()],
  316. // Set tracesSampleRate to 1.0 to capture 100%
  317. // of transactions for performance monitoring.
  318. // We recommend adjusting this value in production
  319. tracesSampleRate: 1.0,
  320. // Set \`tracePropagationTargets\` to control for which URLs distributed tracing should be enabled
  321. tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
  322. });
  323. ReactDOM.render(<App />, document.getElementById("root"));
  324. // Can also use with React Concurrent Mode
  325. // ReactDOM.createRoot(document.getElementById('root')).render(<App />);
  326. `,
  327. additionalInfo: tct(
  328. '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].',
  329. {
  330. code: <code />,
  331. linkTracingOptions: (
  332. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/configuration/options/#tracing-options" />
  333. ),
  334. linkTracesSampler: (
  335. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/configuration/sampling/" />
  336. ),
  337. linkSampleTransactions: (
  338. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/configuration/sampling/" />
  339. ),
  340. }
  341. ),
  342. },
  343. ],
  344. },
  345. ],
  346. verify: () => [
  347. {
  348. type: StepType.VERIFY,
  349. description: tct(
  350. 'Verify that performance monitoring is working correctly with our [link:automatic instrumentation] by simply using your React application.',
  351. {
  352. link: (
  353. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/tracing/instrumentation/automatic-instrumentation/" />
  354. ),
  355. }
  356. ),
  357. configurations: [
  358. {
  359. description: tct(
  360. 'You have the option to manually construct a transaction using [link:custom instrumentation].',
  361. {
  362. link: (
  363. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/tracing/instrumentation/custom-instrumentation/" />
  364. ),
  365. }
  366. ),
  367. language: 'javascript',
  368. code: `
  369. const transaction = Sentry.startTransaction({ name: "test-transaction" });
  370. const span = transaction.startChild({ op: "functionX" }); // This function returns a Span
  371. // exampleFunctionCall();
  372. span.finish(); // Remember that only finished spans will be sent with the transaction
  373. transaction.finish(); // Finishing the transaction will send it to Sentry`,
  374. },
  375. {
  376. description: tct(
  377. 'In addition, [code:@sentry/react] exports a [code:withProfiler] higher order component that can be used to capture React-related spans for specific React components:',
  378. {code: <code />}
  379. ),
  380. language: 'javascript',
  381. code: `
  382. import * as Sentry from "@sentry/react";
  383. function App(props) {
  384. return <div />;
  385. }
  386. export default Sentry.withProfiler(App);
  387. `,
  388. additionalInfo: tct(
  389. 'Learn more about the profiler in [link:React Component Tracking].',
  390. {
  391. link: (
  392. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/react/features/component-tracking/" />
  393. ),
  394. }
  395. ),
  396. },
  397. ],
  398. },
  399. ],
  400. nextSteps: () => [],
  401. };
  402. const profilingOnboarding: OnboardingConfig = {
  403. ...onboarding,
  404. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  405. };
  406. const docs: Docs = {
  407. onboarding,
  408. feedbackOnboardingNpm: feedbackOnboarding,
  409. replayOnboarding,
  410. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  411. performanceOnboarding,
  412. crashReportOnboarding,
  413. profilingOnboarding,
  414. featureFlagOnboarding: featureFlagOnboarding,
  415. };
  416. export default docs;