node.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. } from 'sentry/utils/gettingStartedDocs/node';
  22. type Params = DocsParams;
  23. const getSdkSetupSnippet = () => `
  24. ${getImportInstrumentSnippet()}
  25. // All other imports below
  26. const { createServer } = require("node:http");
  27. const server = createServer((req, res) => {
  28. // server code
  29. });
  30. server.listen(3000, "127.0.0.1");
  31. `;
  32. const onboarding: OnboardingConfig = {
  33. install: (params: Params) => [
  34. {
  35. type: StepType.INSTALL,
  36. description: t('Add the Sentry Node SDK as a dependency:'),
  37. configurations: getInstallConfig(params),
  38. },
  39. ],
  40. configure: (params: Params) => [
  41. {
  42. type: StepType.CONFIGURE,
  43. description: t(
  44. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  45. ),
  46. configurations: [
  47. {
  48. description: tct(
  49. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  50. {code: <code />}
  51. ),
  52. code: [
  53. {
  54. label: 'JavaScript',
  55. value: 'javascript',
  56. language: 'javascript',
  57. filename: 'instrument.(js|mjs)',
  58. code: getSdkInitSnippet(params, 'node'),
  59. },
  60. ],
  61. },
  62. {
  63. description: tct(
  64. "Make sure to import [code1:instrument.js/mjs] at the top of your file. Set up the error handler after all controllers and before any other error middleware. 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].",
  65. {
  66. code1: <code />,
  67. code2: <code />,
  68. docs: (
  69. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/node/install/" />
  70. ),
  71. }
  72. ),
  73. code: [
  74. {
  75. label: 'JavaScript',
  76. value: 'javascript',
  77. language: 'javascript',
  78. filename: 'index.(js|mjs)',
  79. code: getSdkSetupSnippet(),
  80. },
  81. ],
  82. },
  83. ],
  84. },
  85. getUploadSourceMapsStep({
  86. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/node/sourcemaps/',
  87. }),
  88. ],
  89. verify: ({isPerformanceSelected}) => [
  90. {
  91. type: StepType.VERIFY,
  92. description: t(
  93. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  94. ),
  95. configurations: [
  96. {
  97. language: 'javascript',
  98. code: isPerformanceSelected
  99. ? `
  100. const Sentry = require("@sentry/node");
  101. Sentry.startSpan({
  102. op: "test",
  103. name: "My First Test Span",
  104. }, () => {
  105. try {
  106. foo();
  107. } catch (e) {
  108. Sentry.captureException(e);
  109. }
  110. });`
  111. : `
  112. const Sentry = require("@sentry/node");
  113. try {
  114. foo();
  115. } catch (e) {
  116. Sentry.captureException(e);
  117. }`,
  118. },
  119. ],
  120. },
  121. ],
  122. };
  123. const crashReportOnboarding: OnboardingConfig = {
  124. introduction: () => getCrashReportModalIntroduction(),
  125. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  126. configure: () => [
  127. {
  128. type: StepType.CONFIGURE,
  129. description: getCrashReportModalConfigDescription({
  130. link: 'https://docs.sentry.io/platforms/javascript/guides/node/user-feedback/configuration/#crash-report-modal',
  131. }),
  132. },
  133. ],
  134. verify: () => [],
  135. nextSteps: () => [],
  136. };
  137. const docs: Docs = {
  138. onboarding,
  139. replayOnboardingJsLoader,
  140. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  141. crashReportOnboarding,
  142. };
  143. export default docs;