react.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  6. import {t, tct} from 'sentry/locale';
  7. import type {Organization, PlatformKey} from 'sentry/types';
  8. type StepProps = {
  9. newOrg: boolean;
  10. organization: Organization;
  11. platformKey: PlatformKey;
  12. projectId: string;
  13. sentryInitContent: string;
  14. };
  15. // Configuration Start
  16. const replayIntegration = `
  17. new Sentry.Replay(),
  18. `;
  19. const replayOtherConfig = `
  20. // Session Replay
  21. 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.
  22. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  23. `;
  24. const performanceIntegration = `
  25. new Sentry.BrowserTracing({
  26. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  27. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  28. }),
  29. `;
  30. const performanceOtherConfig = `
  31. // Performance Monitoring
  32. tracesSampleRate: 1.0, // Capture 100% of the transactions`;
  33. export const steps = ({
  34. sentryInitContent,
  35. ...props
  36. }: Partial<StepProps> = {}): LayoutProps['steps'] => [
  37. {
  38. type: StepType.INSTALL,
  39. description: (
  40. <p>
  41. {tct(
  42. 'Add the Sentry SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn]:',
  43. {
  44. codeYarn: <code />,
  45. codeNpm: <code />,
  46. }
  47. )}
  48. </p>
  49. ),
  50. configurations: [
  51. {
  52. language: 'bash',
  53. code: [
  54. {
  55. label: 'npm',
  56. value: 'npm',
  57. language: 'bash',
  58. code: 'npm install --save @sentry/react',
  59. },
  60. {
  61. label: 'yarn',
  62. value: 'yarn',
  63. language: 'bash',
  64. code: 'yarn add @sentry/react',
  65. },
  66. ],
  67. },
  68. ],
  69. },
  70. {
  71. type: StepType.CONFIGURE,
  72. description: t(
  73. "Initialize Sentry as early as possible in your application's lifecycle."
  74. ),
  75. configurations: [
  76. {
  77. language: 'javascript',
  78. code: `
  79. //...
  80. import * as Sentry from "@sentry/react";
  81. Sentry.init({
  82. ${sentryInitContent}
  83. });
  84. const container = document.getElementById(“app”);
  85. const root = createRoot(container);
  86. root.render(<App />);
  87. `,
  88. },
  89. ],
  90. },
  91. getUploadSourceMapsStep({
  92. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/react/sourcemaps/',
  93. ...props,
  94. }),
  95. {
  96. type: StepType.VERIFY,
  97. description: t(
  98. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  99. ),
  100. configurations: [
  101. {
  102. language: 'javascript',
  103. code: `
  104. return <button onClick={() => methodDoesNotExist()}>Break the world</button>;
  105. `,
  106. },
  107. ],
  108. },
  109. ];
  110. export const nextSteps = [
  111. {
  112. id: 'react-features',
  113. name: t('React Features'),
  114. description: t('Learn about our first class integration with the React framework.'),
  115. link: 'https://docs.sentry.io/platforms/javascript/guides/react/features/',
  116. },
  117. {
  118. id: 'react-router',
  119. name: t('React Router'),
  120. description: t(
  121. 'Configure routing, so Sentry can generate parameterized transaction names for a better overview in Performance Monitoring.'
  122. ),
  123. link: 'https://docs.sentry.io/platforms/javascript/guides/react/configuration/integrations/react-router/',
  124. },
  125. {
  126. id: 'performance-monitoring',
  127. name: t('Performance Monitoring'),
  128. description: t(
  129. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  130. ),
  131. link: 'https://docs.sentry.io/platforms/javascript/guides/react/performance/',
  132. },
  133. {
  134. id: 'session-replay',
  135. name: t('Session Replay'),
  136. description: t(
  137. 'Get to the root cause of an error or latency issue faster by seeing all the technical details related to that issue in one visual replay on your web application.'
  138. ),
  139. link: 'https://docs.sentry.io/platforms/javascript/guides/react/session-replay/',
  140. },
  141. ];
  142. // Configuration End
  143. export function GettingStartedWithReact({
  144. dsn,
  145. activeProductSelection = [],
  146. organization,
  147. newOrg,
  148. platformKey,
  149. projectId,
  150. ...props
  151. }: ModuleProps) {
  152. const integrations: string[] = [];
  153. const otherConfigs: string[] = [];
  154. let nextStepDocs = [...nextSteps];
  155. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  156. integrations.push(performanceIntegration.trim());
  157. otherConfigs.push(performanceOtherConfig.trim());
  158. nextStepDocs = nextStepDocs.filter(
  159. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  160. );
  161. }
  162. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  163. integrations.push(replayIntegration.trim());
  164. otherConfigs.push(replayOtherConfig.trim());
  165. nextStepDocs = nextStepDocs.filter(
  166. step => step.id !== ProductSolution.SESSION_REPLAY
  167. );
  168. }
  169. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  170. if (integrations.length > 0) {
  171. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  172. }
  173. if (otherConfigs.length > 0) {
  174. sentryInitContent = sentryInitContent.concat(otherConfigs);
  175. }
  176. return (
  177. <Layout
  178. steps={steps({
  179. sentryInitContent: sentryInitContent.join('\n'),
  180. organization,
  181. newOrg,
  182. platformKey,
  183. projectId,
  184. })}
  185. nextSteps={nextStepDocs}
  186. newOrg={newOrg}
  187. platformKey={platformKey}
  188. {...props}
  189. />
  190. );
  191. }
  192. export default GettingStartedWithReact;