remix.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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`;
  30. const prismaIntegration = `new Sentry.Integrations.Prisma({ client: prisma }),`;
  31. export const steps = ({
  32. sentryInitContent,
  33. sentryInitContentServer,
  34. }: {
  35. sentryInitContent?: string;
  36. sentryInitContentServer?: string[];
  37. } = {}): LayoutProps['steps'] => [
  38. {
  39. type: StepType.INSTALL,
  40. description: (
  41. <p>
  42. {tct(
  43. 'Add the Sentry SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn]:',
  44. {
  45. codeYarn: <code />,
  46. codeNpm: <code />,
  47. }
  48. )}
  49. </p>
  50. ),
  51. configurations: [
  52. {
  53. language: 'bash',
  54. code: [
  55. {
  56. label: 'npm',
  57. value: 'npm',
  58. language: 'bash',
  59. code: 'npm install --save @sentry/remix',
  60. },
  61. {
  62. label: 'yarn',
  63. value: 'yarn',
  64. language: 'bash',
  65. code: 'yarn add @sentry/remix',
  66. },
  67. ],
  68. },
  69. ],
  70. },
  71. {
  72. type: StepType.CONFIGURE,
  73. description: (
  74. <p>
  75. {tct(
  76. 'Import and initialize Sentry in your Remix entry points for the client ([clientFile:entry.client.tsx]):',
  77. {
  78. clientFile: <code />,
  79. }
  80. )}
  81. </p>
  82. ),
  83. configurations: [
  84. {
  85. language: 'javascript',
  86. code: `
  87. import { useLocation, useMatches } from "@remix-run/react";
  88. import * as Sentry from "@sentry/remix";
  89. import { useEffect } from "react";
  90. Sentry.init({
  91. ${sentryInitContent}
  92. });
  93. `,
  94. },
  95. {
  96. language: 'javascript',
  97. description: (
  98. <p>
  99. {tct(
  100. `Initialize Sentry in your entry point for the server ([serverFile:entry.server.tsx]) 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:`,
  101. {
  102. action: (
  103. <ExternalLink href="https://remix.run/docs/en/v1/api/conventions#action" />
  104. ),
  105. loader: (
  106. <ExternalLink href="https://remix.run/docs/en/1.18.1/api/conventions#loader" />
  107. ),
  108. serverFile: <code />,
  109. }
  110. )}
  111. </p>
  112. ),
  113. code: `
  114. ${
  115. (sentryInitContentServer ?? []).length > 1
  116. ? `import { prisma } from "~/db.server";`
  117. : ''
  118. }
  119. import * as Sentry from "@sentry/remix";
  120. Sentry.init({
  121. ${sentryInitContentServer?.join('\n')}
  122. });
  123. `,
  124. },
  125. {
  126. description: t(
  127. 'Lastly, wrap your Remix root with "withSentry" to catch React component errors and to get parameterized router transactions:'
  128. ),
  129. language: 'javascript',
  130. code: `
  131. import {
  132. Links,
  133. LiveReload,
  134. Meta,
  135. Outlet,
  136. Scripts,
  137. ScrollRestoration,
  138. } from "@remix-run/react";
  139. import { withSentry } from "@sentry/remix";
  140. function App() {
  141. return (
  142. <html>
  143. <head>
  144. <Meta />
  145. <Links />
  146. </head>
  147. <body>
  148. <Outlet />
  149. <ScrollRestoration />
  150. <Scripts />
  151. <LiveReload />
  152. </body>
  153. </html>
  154. );
  155. }
  156. export default withSentry(App);
  157. `,
  158. },
  159. ],
  160. },
  161. {
  162. type: StepType.VERIFY,
  163. description: t(
  164. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  165. ),
  166. configurations: [
  167. {
  168. language: 'jsx',
  169. code: `
  170. <button type="button" onClick={() => { throw new Error("Sentry Frontend Error") }}>
  171. Throw Test Error
  172. </button>
  173. `,
  174. },
  175. ],
  176. },
  177. ];
  178. export const nextSteps = [
  179. {
  180. id: 'source-maps',
  181. name: t('Source Maps'),
  182. description: t('Learn how to enable readable stack traces in your Sentry errors.'),
  183. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/sourcemaps/',
  184. },
  185. {
  186. id: 'performance-monitoring',
  187. name: t('Performance Monitoring'),
  188. description: t(
  189. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  190. ),
  191. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/performance/',
  192. },
  193. {
  194. id: 'session-replay',
  195. name: t('Session Replay'),
  196. description: t(
  197. '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.'
  198. ),
  199. link: 'https://docs.sentry.io/platforms/javascript/guides/remix/session-replay/',
  200. },
  201. ];
  202. // Configuration End
  203. export function GettingStartedWithRemix({
  204. dsn,
  205. activeProductSelection = [],
  206. ...props
  207. }: ModuleProps) {
  208. const integrations: string[] = [];
  209. const otherConfigs: string[] = [];
  210. const serverIntegrations: string[] = [];
  211. const otherConfigsServer: string[] = [];
  212. let nextStepDocs = [...nextSteps];
  213. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  214. integrations.push(performanceIntegration.trim());
  215. otherConfigs.push(performanceOtherConfig.trim());
  216. serverIntegrations.push(prismaIntegration.trim());
  217. otherConfigsServer.push(performanceOtherConfig.trim());
  218. nextStepDocs = nextStepDocs.filter(
  219. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  220. );
  221. }
  222. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  223. integrations.push(replayIntegration.trim());
  224. otherConfigs.push(replayOtherConfig.trim());
  225. nextStepDocs = nextStepDocs.filter(
  226. step => step.id !== ProductSolution.SESSION_REPLAY
  227. );
  228. }
  229. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  230. let sentryInitContentServer: string[] = [`dsn: "${dsn}",`];
  231. if (integrations.length > 0) {
  232. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  233. }
  234. if (serverIntegrations.length) {
  235. sentryInitContentServer = sentryInitContentServer.concat(
  236. 'integrations: [',
  237. serverIntegrations,
  238. '],'
  239. );
  240. }
  241. if (otherConfigs.length > 0) {
  242. sentryInitContent = sentryInitContent.concat(otherConfigs);
  243. }
  244. if (otherConfigsServer.length) {
  245. sentryInitContentServer = sentryInitContentServer.concat(otherConfigsServer);
  246. }
  247. return (
  248. <Layout
  249. steps={steps({
  250. sentryInitContent: sentryInitContent.join('\n'),
  251. sentryInitContentServer,
  252. })}
  253. nextSteps={nextStepDocs}
  254. {...props}
  255. />
  256. );
  257. }
  258. export default GettingStartedWithRemix;