remix.tsx 7.8 KB

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