express.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {PlatformKey} from 'sentry/data/platformCategories';
  6. import {t, tct} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types';
  8. type StepProps = {
  9. newOrg: boolean;
  10. organization: Organization;
  11. platformKey: PlatformKey;
  12. projectId: string;
  13. sentryInitContent: string;
  14. };
  15. const performanceIntegrations: string[] = [
  16. `// enable HTTP calls tracing
  17. new Sentry.Integrations.Http({ tracing: true }),`,
  18. `// enable Express.js middleware tracing
  19. new Sentry.Integrations.Express({ app }),`,
  20. ];
  21. const performanceOtherConfig = `// Performance Monitoring
  22. tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!`;
  23. export const steps = ({
  24. sentryInitContent,
  25. ...props
  26. }: Partial<StepProps> = {}): LayoutProps['steps'] => [
  27. {
  28. type: StepType.INSTALL,
  29. description: t('Add the Sentry Node SDK as a dependency:'),
  30. configurations: [
  31. {
  32. language: 'bash',
  33. code: `
  34. # Using yarn
  35. yarn add @sentry/node
  36. # Using npm
  37. npm install --save @sentry/node
  38. `,
  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. import * as Sentry from "@sentry/node";
  57. import express from "express";
  58. // or using CommonJS
  59. // const Sentry = require('@sentry/node');
  60. // const express = require('express');
  61. const app = express();
  62. Sentry.init({
  63. ${sentryInitContent},
  64. });
  65. // Trace incoming requests
  66. app.use(Sentry.Handlers.requestHandler());
  67. app.use(Sentry.Handlers.tracingHandler());
  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. },
  84. ],
  85. },
  86. getUploadSourceMapsStep({
  87. guideLink: 'https://docs.sentry.io/platforms/node/guides/express/sourcemaps/',
  88. ...props,
  89. }),
  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: `
  99. app.get("/debug-sentry", function mainHandler(req, res) {
  100. throw new Error("My first Sentry error!");
  101. });
  102. `,
  103. },
  104. ],
  105. },
  106. ];
  107. export function GettingStartedWithExpress({
  108. dsn,
  109. organization,
  110. newOrg,
  111. platformKey,
  112. projectId,
  113. }: ModuleProps) {
  114. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  115. const integrations = [...performanceIntegrations];
  116. const otherConfigs = [performanceOtherConfig];
  117. if (integrations.length > 0) {
  118. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  119. }
  120. if (otherConfigs.length > 0) {
  121. sentryInitContent = sentryInitContent.concat(otherConfigs);
  122. }
  123. return (
  124. <Layout
  125. steps={steps({
  126. sentryInitContent: sentryInitContent.join('\n'),
  127. organization,
  128. newOrg,
  129. platformKey,
  130. projectId,
  131. })}
  132. newOrg={newOrg}
  133. platformKey={platformKey}
  134. />
  135. );
  136. }
  137. export default GettingStartedWithExpress;