express.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  2. import type {
  3. Docs,
  4. DocsParams,
  5. OnboardingConfig,
  6. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  7. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  8. import {
  9. getCrashReportJavaScriptInstallStep,
  10. getCrashReportModalConfigDescription,
  11. getCrashReportModalIntroduction,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  13. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  14. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  15. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  16. import {t, tct} from 'sentry/locale';
  17. import type {ProductSelectionMap} from 'sentry/utils/gettingStartedDocs/node';
  18. import {
  19. getDefaultNodeImports,
  20. getInstallConfig,
  21. } from 'sentry/utils/gettingStartedDocs/node';
  22. type Params = DocsParams;
  23. const productSelection = (params: Params): ProductSelectionMap => {
  24. return {
  25. [ProductSolution.ERROR_MONITORING]: true,
  26. [ProductSolution.PROFILING]: params.isProfilingSelected,
  27. [ProductSolution.PERFORMANCE_MONITORING]: params.isPerformanceSelected,
  28. [ProductSolution.SESSION_REPLAY]: params.isReplaySelected,
  29. };
  30. };
  31. const getSdkSetupSnippet = (params: Params) => `
  32. ${getDefaultNodeImports({productSelection: productSelection(params)}).join('\n')}
  33. import express from "express";
  34. const app = express();
  35. Sentry.init({
  36. dsn: "${params.dsn}",
  37. integrations: [${
  38. params.isPerformanceSelected
  39. ? `
  40. // enable HTTP calls tracing
  41. new Sentry.Integrations.Http({ tracing: true }),
  42. // enable Express.js middleware tracing
  43. new Sentry.Integrations.Express({ app }),`
  44. : ''
  45. }${
  46. params.isProfilingSelected
  47. ? `
  48. nodeProfilingIntegration(),`
  49. : ''
  50. }
  51. ],${
  52. params.isPerformanceSelected
  53. ? `
  54. // Performance Monitoring
  55. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  56. : ''
  57. }${
  58. params.isProfilingSelected
  59. ? `
  60. // Set sampling rate for profiling - this is relative to tracesSampleRate
  61. profilesSampleRate: 1.0,`
  62. : ''
  63. }
  64. });
  65. // The request handler must be the first middleware on the app
  66. app.use(Sentry.Handlers.requestHandler());${
  67. params.isPerformanceSelected
  68. ? `
  69. // TracingHandler creates a trace for every incoming request
  70. app.use(Sentry.Handlers.tracingHandler());`
  71. : ''
  72. }
  73. // All your controllers should live here
  74. app.get("/", function rootHandler(req, res) {
  75. res.end("Hello world!");
  76. });
  77. // The error handler must be registered before any other error middleware and after all controllers
  78. app.use(Sentry.Handlers.errorHandler());
  79. // Optional fallthrough error handler
  80. app.use(function onError(err, req, res, next) {
  81. // The error id is attached to \`res.sentry\` to be returned
  82. // and optionally displayed to the user for support.
  83. res.statusCode = 500;
  84. res.end(res.sentry + "\\n");
  85. });
  86. app.listen(3000);
  87. `;
  88. const onboarding: OnboardingConfig = {
  89. install: (params: Params) => [
  90. {
  91. type: StepType.INSTALL,
  92. description: t('Add the Sentry Node SDK as a dependency:'),
  93. configurations: getInstallConfig(params),
  94. },
  95. ],
  96. configure: (params: Params) => [
  97. {
  98. type: StepType.CONFIGURE,
  99. description: tct(
  100. "Initialize Sentry as early as possible in your application's lifecycle, for example in your [code:index.ts/js] entry point:",
  101. {code: <code />}
  102. ),
  103. configurations: [
  104. {
  105. code: [
  106. {
  107. label: 'JavaScript',
  108. value: 'javascript',
  109. language: 'javascript',
  110. code: getSdkSetupSnippet(params),
  111. },
  112. ],
  113. },
  114. ],
  115. },
  116. getUploadSourceMapsStep({
  117. guideLink: 'https://docs.sentry.io/platforms/node/sourcemaps/',
  118. ...params,
  119. }),
  120. ],
  121. verify: () => [
  122. {
  123. type: StepType.VERIFY,
  124. description: t(
  125. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  126. ),
  127. configurations: [
  128. {
  129. language: 'javascript',
  130. code: `
  131. app.get("/debug-sentry", function mainHandler(req, res) {
  132. throw new Error("My first Sentry error!");
  133. });
  134. `,
  135. },
  136. ],
  137. },
  138. ],
  139. };
  140. const crashReportOnboarding: OnboardingConfig = {
  141. introduction: () => getCrashReportModalIntroduction(),
  142. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  143. configure: () => [
  144. {
  145. type: StepType.CONFIGURE,
  146. description: getCrashReportModalConfigDescription({
  147. link: 'https://docs.sentry.io/platforms/node/guides/express/user-feedback/configuration/#crash-report-modal',
  148. }),
  149. },
  150. ],
  151. verify: () => [],
  152. nextSteps: () => [],
  153. };
  154. const docs: Docs = {
  155. onboarding,
  156. replayOnboardingJsLoader,
  157. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  158. crashReportOnboarding,
  159. };
  160. export default docs;