remix.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  6. import {t, tct} from 'sentry/locale';
  7. // Configuration Start
  8. const replayIntegration = `
  9. new Sentry.Replay(),
  10. `;
  11. const replayOtherConfig = `
  12. // Session Replay
  13. 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.
  14. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  15. `;
  16. const performanceIntegration = `
  17. new Sentry.BrowserTracing({
  18. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  19. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  20. routingInstrumentation: Sentry.remixRouterInstrumentation(
  21. useEffect,
  22. useLocation,
  23. useMatches
  24. ),
  25. }),
  26. `;
  27. const performanceOtherConfig = `
  28. // Performance Monitoring
  29. tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
  30. `;
  31. const prismaConfig = `
  32. new Sentry.Integrations.Prisma({ client: prisma })
  33. `;
  34. export const steps = ({
  35. sentryInitContent,
  36. sentryInitContentServer,
  37. }: {
  38. sentryInitContent?: string;
  39. sentryInitContentServer?: string[];
  40. } = {}): LayoutProps['steps'] => [
  41. {
  42. type: StepType.INSTALL,
  43. description: t(
  44. 'Sentry captures data by using an SDK within your application’s runtime.'
  45. ),
  46. configurations: [
  47. {
  48. language: 'bash',
  49. code: `
  50. # Using yarn
  51. yarn add @sentry/remix
  52. # Using npm
  53. npm install --save @sentry/remix
  54. `,
  55. },
  56. ],
  57. },
  58. {
  59. type: StepType.CONFIGURE,
  60. description: t(
  61. 'Import and initialize Sentry in your Remix entry points for both the client and server:'
  62. ),
  63. configurations: [
  64. {
  65. language: 'javascript',
  66. code: `
  67. import { useLocation, useMatches } from "@remix-run/react";
  68. import * as Sentry from "@sentry/remix";
  69. import { useEffect } from "react";
  70. Sentry.init({
  71. ${sentryInitContent}
  72. });
  73. `,
  74. },
  75. {
  76. language: 'javascript',
  77. description: (
  78. <p>
  79. {tct(
  80. `Initialize Sentry in your entry point for the server to capture exceptions and get performance metrics for your [action] and [loader] functions. You can also initialize Sentry's database integrations, such as Prisma, to get spans for your database calls:`,
  81. {
  82. action: (
  83. <ExternalLink href="https://remix.run/docs/en/v1/api/conventions#action" />
  84. ),
  85. loader: (
  86. <ExternalLink href="https://remix.run/docs/en/1.18.1/api/conventions#loader" />
  87. ),
  88. }
  89. )}
  90. </p>
  91. ),
  92. code: `
  93. ${
  94. (sentryInitContentServer ?? []).length > 1
  95. ? `import { prisma } from "~/db.server";`
  96. : ''
  97. }
  98. import * as Sentry from "@sentry/remix";
  99. Sentry.init({
  100. ${sentryInitContentServer?.join('\n')}
  101. });
  102. `,
  103. },
  104. {
  105. description: t(
  106. 'Lastly, wrap your Remix root with "withSentry" to catch React component errors and to get parameterized router transactions:'
  107. ),
  108. language: 'javascript',
  109. code: `
  110. import {
  111. Links,
  112. LiveReload,
  113. Meta,
  114. Outlet,
  115. Scripts,
  116. ScrollRestoration,
  117. } from "@remix-run/react";
  118. import { withSentry } from "@sentry/remix";
  119. function App() {
  120. return (
  121. <html>
  122. <head>
  123. <Meta />
  124. <Links />
  125. </head>
  126. <body>
  127. <Outlet />
  128. <ScrollRestoration />
  129. <Scripts />
  130. <LiveReload />
  131. </body>
  132. </html>
  133. );
  134. }
  135. export default withSentry(App);
  136. `,
  137. },
  138. ],
  139. },
  140. {
  141. type: StepType.VERIFY,
  142. description: t(
  143. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  144. ),
  145. configurations: [
  146. {
  147. language: 'javascript',
  148. code: `
  149. return <button onClick={() => methodDoesNotExist()}>Break the world</button>;
  150. `,
  151. },
  152. ],
  153. },
  154. ];
  155. export const nextSteps = [
  156. {
  157. id: 'source-maps',
  158. name: t('Source Maps'),
  159. description: t('Learn how to enable readable stack traces in your Sentry errors.'),
  160. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/sourcemaps/',
  161. },
  162. {
  163. id: 'performance-monitoring',
  164. name: t('Performance Monitoring'),
  165. description: t(
  166. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  167. ),
  168. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/performance/',
  169. },
  170. {
  171. id: 'session-replay',
  172. name: t('Session Replay'),
  173. description: t(
  174. '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.'
  175. ),
  176. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/session-replay/',
  177. },
  178. ];
  179. // Configuration End
  180. export function GettingStartedWithRemix({
  181. dsn,
  182. activeProductSelection = [],
  183. ...props
  184. }: ModuleProps) {
  185. const integrations: string[] = [];
  186. const otherConfigs: string[] = [];
  187. let nextStepDocs = [...nextSteps];
  188. const sentryInitContentServer: string[] = [`dsn: "${dsn}",`];
  189. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  190. integrations.push(performanceIntegration.trim());
  191. otherConfigs.push(performanceOtherConfig.trim());
  192. sentryInitContentServer.push(prismaConfig.trim());
  193. sentryInitContentServer.push(performanceOtherConfig.trim());
  194. nextStepDocs = nextStepDocs.filter(
  195. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  196. );
  197. }
  198. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  199. integrations.push(replayIntegration.trim());
  200. otherConfigs.push(replayOtherConfig.trim());
  201. nextStepDocs = nextStepDocs.filter(
  202. step => step.id !== ProductSolution.SESSION_REPLAY
  203. );
  204. }
  205. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  206. if (integrations.length > 0) {
  207. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  208. }
  209. if (otherConfigs.length > 0) {
  210. sentryInitContent = sentryInitContent.concat(otherConfigs);
  211. }
  212. return (
  213. <Layout
  214. steps={steps({
  215. sentryInitContent: sentryInitContent.join('\n'),
  216. sentryInitContentServer,
  217. })}
  218. nextSteps={nextStepDocs}
  219. {...props}
  220. />
  221. );
  222. }
  223. export default GettingStartedWithRemix;