express.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 {PlatformKey} from 'sentry/data/platformCategories';
  5. import {t, tct} from 'sentry/locale';
  6. import type {Organization} from 'sentry/types';
  7. type StepProps = {
  8. newOrg: boolean;
  9. organization: Organization;
  10. platformKey: PlatformKey;
  11. projectId: string;
  12. sentryInitContent: string;
  13. };
  14. const performanceIntegrations: string[] = [
  15. `// enable HTTP calls tracing
  16. new Sentry.Integrations.Http({ tracing: true }),`,
  17. `// enable Express.js middleware tracing
  18. new Sentry.Integrations.Express({ app }),`,
  19. ];
  20. const performanceOtherConfig = `// Performance Monitoring
  21. tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!`;
  22. export const steps = ({
  23. sentryInitContent,
  24. }: Partial<StepProps> = {}): LayoutProps['steps'] => [
  25. {
  26. type: StepType.INSTALL,
  27. description: t('Add the Sentry Node SDK as a dependency:'),
  28. configurations: [
  29. {
  30. language: 'bash',
  31. code: `
  32. # Using yarn
  33. yarn add @sentry/node
  34. # Using npm
  35. npm install --save @sentry/node
  36. `,
  37. },
  38. ],
  39. },
  40. {
  41. type: StepType.CONFIGURE,
  42. description: (
  43. <p>
  44. {tct(
  45. "Initialize Sentry as early as possible in your application's lifecycle, for example in your [code:index.ts/js] entry point:",
  46. {code: <code />}
  47. )}
  48. </p>
  49. ),
  50. configurations: [
  51. {
  52. language: 'javascript',
  53. code: `
  54. import * as Sentry from "@sentry/node";
  55. import express from "express";
  56. // or using CommonJS
  57. // const Sentry = require('@sentry/node');
  58. // const express = require('express');
  59. const app = express();
  60. Sentry.init({
  61. ${sentryInitContent},
  62. });
  63. // Trace incoming requests
  64. app.use(Sentry.Handlers.requestHandler());
  65. app.use(Sentry.Handlers.tracingHandler());
  66. // All your controllers should live here
  67. app.get("/", function rootHandler(req, res) {
  68. res.end("Hello world!");
  69. });
  70. // The error handler must be registered before any other error middleware and after all controllers
  71. app.use(Sentry.Handlers.errorHandler());
  72. // Optional fallthrough error handler
  73. app.use(function onError(err, req, res, next) {
  74. // The error id is attached to \`res.sentry\` to be returned
  75. // and optionally displayed to the user for support.
  76. res.statusCode = 500;
  77. res.end(res.sentry + "\\n");
  78. });
  79. app.listen(3000);
  80. `,
  81. },
  82. ],
  83. },
  84. {
  85. type: StepType.VERIFY,
  86. description: t(
  87. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  88. ),
  89. configurations: [
  90. {
  91. language: 'javascript',
  92. code: `
  93. app.get("/debug-sentry", function mainHandler(req, res) {
  94. throw new Error("My first Sentry error!");
  95. });
  96. `,
  97. },
  98. ],
  99. },
  100. ];
  101. export function GettingStartedWithExpress({dsn, newOrg, platformKey}: ModuleProps) {
  102. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  103. const integrations = [...performanceIntegrations];
  104. const otherConfigs = [performanceOtherConfig];
  105. if (integrations.length > 0) {
  106. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  107. }
  108. if (otherConfigs.length > 0) {
  109. sentryInitContent = sentryInitContent.concat(otherConfigs);
  110. }
  111. return (
  112. <Layout
  113. steps={steps({
  114. sentryInitContent: sentryInitContent.join('\n'),
  115. })}
  116. newOrg={newOrg}
  117. platformKey={platformKey}
  118. />
  119. );
  120. }
  121. export default GettingStartedWithExpress;