express.tsx 4.3 KB

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