express.tsx 4.6 KB

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