express.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {t, tct} from 'sentry/locale';
  5. import {
  6. getDefaultInitParams,
  7. getDefaultNodeImports,
  8. getInstallSnippet,
  9. getProductInitParams,
  10. getProductIntegrations,
  11. getProductSelectionMap,
  12. joinWithIndentation,
  13. } from 'sentry/utils/gettingStartedDocs/node';
  14. interface StepProps {
  15. hasPerformanceMonitoring: boolean;
  16. importContent: string;
  17. initContent: string;
  18. installSnippet: string;
  19. }
  20. const performanceIntegrations: string[] = [
  21. '// enable HTTP calls tracing',
  22. 'new Sentry.Integrations.Http({ tracing: true }),',
  23. '// enable Express.js middleware tracing',
  24. 'new Sentry.Integrations.Express({ app }),',
  25. ];
  26. export const steps = ({
  27. installSnippet,
  28. importContent,
  29. initContent,
  30. hasPerformanceMonitoring,
  31. }: StepProps): LayoutProps['steps'] => [
  32. {
  33. type: StepType.INSTALL,
  34. description: t('Add the Sentry Node SDK as a dependency:'),
  35. configurations: [
  36. {
  37. language: 'bash',
  38. code: installSnippet,
  39. },
  40. ],
  41. },
  42. {
  43. type: StepType.CONFIGURE,
  44. description: (
  45. <p>
  46. {tct(
  47. "Initialize Sentry as early as possible in your application's lifecycle, for example in your [code:index.ts/js] entry point:",
  48. {code: <code />}
  49. )}
  50. </p>
  51. ),
  52. configurations: [
  53. {
  54. language: 'javascript',
  55. code: `
  56. ${importContent}
  57. const app = express();
  58. Sentry.init({
  59. ${initContent}
  60. });
  61. // The request handler must be the first middleware on the app
  62. app.use(Sentry.Handlers.requestHandler());${
  63. hasPerformanceMonitoring
  64. ? `
  65. // TracingHandler creates a trace for every incoming request
  66. app.use(Sentry.Handlers.tracingHandler());`
  67. : ''
  68. }
  69. // All your controllers should live here
  70. app.get("/", function rootHandler(req, res) {
  71. res.end("Hello world!");
  72. });
  73. // The error handler must be registered before any other error middleware and after all controllers
  74. app.use(Sentry.Handlers.errorHandler());
  75. // Optional fallthrough error handler
  76. app.use(function onError(err, req, res, next) {
  77. // The error id is attached to \`res.sentry\` to be returned
  78. // and optionally displayed to the user for support.
  79. res.statusCode = 500;
  80. res.end(res.sentry + "\\n");
  81. });
  82. app.listen(3000);
  83. `,
  84. },
  85. ],
  86. },
  87. {
  88. type: StepType.VERIFY,
  89. description: t(
  90. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  91. ),
  92. configurations: [
  93. {
  94. language: 'javascript',
  95. code: `
  96. app.get("/debug-sentry", function mainHandler(req, res) {
  97. throw new Error("My first Sentry error!");
  98. });
  99. `,
  100. },
  101. ],
  102. },
  103. ];
  104. export function GettingStartedWithExpress({
  105. dsn,
  106. newOrg,
  107. platformKey,
  108. activeProductSelection = [],
  109. }: ModuleProps) {
  110. const productSelection = getProductSelectionMap(activeProductSelection);
  111. const installSnippet = getInstallSnippet({productSelection});
  112. const imports = getDefaultNodeImports({productSelection});
  113. imports.push('import express from "express";');
  114. const integrations = [
  115. ...(productSelection['performance-monitoring'] ? performanceIntegrations : []),
  116. ...getProductIntegrations({productSelection}),
  117. ];
  118. const integrationParam =
  119. integrations.length > 0
  120. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  121. : null;
  122. const initContent = joinWithIndentation([
  123. ...getDefaultInitParams({dsn}),
  124. ...(integrationParam ? [integrationParam] : []),
  125. ...getProductInitParams({productSelection}),
  126. ]);
  127. return (
  128. <Layout
  129. steps={steps({
  130. installSnippet,
  131. importContent: imports.join('\n'),
  132. initContent,
  133. hasPerformanceMonitoring: productSelection['performance-monitoring'],
  134. })}
  135. newOrg={newOrg}
  136. platformKey={platformKey}
  137. />
  138. );
  139. }
  140. export default GettingStartedWithExpress;