fastify.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  9. import {
  10. getCrashReportJavaScriptInstallStep,
  11. getCrashReportModalConfigDescription,
  12. getCrashReportModalIntroduction,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  15. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  16. import {t, tct} from 'sentry/locale';
  17. import {
  18. getImportInstrumentSnippet,
  19. getInstallConfig,
  20. getSdkInitSnippet,
  21. getSentryImportSnippet,
  22. } from 'sentry/utils/gettingStartedDocs/node';
  23. type Params = DocsParams;
  24. const getSdkSetupSnippet = () => `
  25. ${getImportInstrumentSnippet()}
  26. // All other imports below
  27. ${getSentryImportSnippet('node')}
  28. const Fastify = require('fastify')
  29. const app = Fastify();
  30. Sentry.setupFastifyErrorHandler(app);
  31. app.get("/", function rootHandler(req, res) {
  32. res.send("Hello world!");
  33. });
  34. app.listen(3000);
  35. `;
  36. const onboarding: OnboardingConfig = {
  37. install: (params: Params) => [
  38. {
  39. type: StepType.INSTALL,
  40. description: t('Add the Sentry Node SDK as a dependency:'),
  41. configurations: getInstallConfig(params),
  42. },
  43. ],
  44. configure: (params: Params) => [
  45. {
  46. type: StepType.CONFIGURE,
  47. description: t(
  48. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  49. ),
  50. configurations: [
  51. {
  52. description: tct(
  53. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  54. {code: <code />}
  55. ),
  56. code: [
  57. {
  58. label: 'JavaScript',
  59. value: 'javascript',
  60. language: 'javascript',
  61. filename: 'instrument.(js|mjs)',
  62. code: getSdkInitSnippet(params, 'node'),
  63. },
  64. ],
  65. },
  66. {
  67. description: tct(
  68. "Make sure to import [code1:instrument.js/mjs] at the top of your file. Set up the error handler. This setup is typically done in your application's entry point file, which is usually [code2:index.(js|ts)]. If you're running your application in ESM mode, or looking for alternative ways to set up Sentry, read about [docs:installation methods in our docs].",
  69. {
  70. code1: <code />,
  71. code2: <code />,
  72. docs: (
  73. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/fastify/install/" />
  74. ),
  75. }
  76. ),
  77. code: [
  78. {
  79. label: 'JavaScript',
  80. value: 'javascript',
  81. language: 'javascript',
  82. filename: 'index.(js|mjs)',
  83. code: getSdkSetupSnippet(),
  84. },
  85. ],
  86. },
  87. ],
  88. },
  89. getUploadSourceMapsStep({
  90. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/fastify/sourcemaps/',
  91. ...params,
  92. }),
  93. ],
  94. verify: () => [
  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. app.get("/debug-sentry", function mainHandler(req, res) {
  105. throw new Error("My first Sentry error!");
  106. });
  107. `,
  108. },
  109. ],
  110. },
  111. ],
  112. };
  113. const crashReportOnboarding: OnboardingConfig = {
  114. introduction: () => getCrashReportModalIntroduction(),
  115. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  116. configure: () => [
  117. {
  118. type: StepType.CONFIGURE,
  119. description: getCrashReportModalConfigDescription({
  120. link: 'https://docs.sentry.io/platforms/javascript/guides/express/user-feedback/configuration/#crash-report-modal',
  121. }),
  122. },
  123. ],
  124. verify: () => [],
  125. nextSteps: () => [],
  126. };
  127. const docs: Docs = {
  128. onboarding,
  129. replayOnboardingJsLoader,
  130. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  131. crashReportOnboarding,
  132. };
  133. export default docs;