connect.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 {
  9. getCrashReportJavaScriptInstallStep,
  10. getCrashReportModalConfigDescription,
  11. getCrashReportModalIntroduction,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  13. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  14. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  15. import {t} from 'sentry/locale';
  16. import type {ProductSelectionMap} from 'sentry/utils/gettingStartedDocs/node';
  17. import {
  18. getDefaultNodeImports,
  19. getInstallConfig,
  20. } from 'sentry/utils/gettingStartedDocs/node';
  21. type Params = DocsParams;
  22. const productSelection = (params: Params): ProductSelectionMap => {
  23. return {
  24. [ProductSolution.ERROR_MONITORING]: true,
  25. [ProductSolution.PROFILING]: params.isProfilingSelected,
  26. [ProductSolution.PERFORMANCE_MONITORING]: params.isPerformanceSelected,
  27. [ProductSolution.SESSION_REPLAY]: params.isReplaySelected,
  28. };
  29. };
  30. const getSdkSetupSnippet = (params: Params) => `
  31. ${getDefaultNodeImports({productSelection: productSelection(params)}).join('\n')}
  32. import connect from "connect";
  33. // Configure Sentry before doing anything else
  34. Sentry.init({
  35. dsn: "${params.dsn}",
  36. integrations: [${
  37. params.isProfilingSelected
  38. ? `
  39. nodeProfilingIntegration(),`
  40. : ''
  41. }
  42. ],${
  43. params.isPerformanceSelected
  44. ? `
  45. // Performance Monitoring
  46. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  47. : ''
  48. }${
  49. params.isProfilingSelected
  50. ? `
  51. // Set sampling rate for profiling - this is relative to tracesSampleRate
  52. profilesSampleRate: 1.0,`
  53. : ''
  54. }
  55. });
  56. function mainHandler(req, res) {
  57. throw new Error("My first Sentry error!");
  58. }
  59. function onError(err, req, res, next) {
  60. // The error id is attached to \`res.sentry\` to be returned
  61. // and optionally displayed to the user for support.
  62. res.statusCode = 500;
  63. res.end(res.sentry + "\\n");
  64. }
  65. connect(
  66. // The request handler be the first item
  67. Sentry.Handlers.requestHandler(),
  68. connect.bodyParser(),
  69. connect.cookieParser(),
  70. mainHandler,
  71. // The error handler must be before any other error middleware
  72. Sentry.Handlers.errorHandler(),
  73. // Optional fallthrough error handler
  74. onError
  75. ).listen(3000);
  76. `;
  77. const onboarding: OnboardingConfig = {
  78. install: params => [
  79. {
  80. type: StepType.INSTALL,
  81. description: t('Add the Sentry Node SDK as a dependency:'),
  82. configurations: getInstallConfig(params),
  83. },
  84. ],
  85. configure: params => [
  86. {
  87. type: StepType.CONFIGURE,
  88. description: t('Configure Sentry as a middleware:'),
  89. configurations: [
  90. {
  91. language: 'javascript',
  92. code: getSdkSetupSnippet(params),
  93. },
  94. ],
  95. },
  96. getUploadSourceMapsStep({
  97. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/connect/sourcemaps/',
  98. ...params,
  99. }),
  100. ],
  101. verify: () => [],
  102. };
  103. const crashReportOnboarding: OnboardingConfig = {
  104. introduction: () => getCrashReportModalIntroduction(),
  105. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  106. configure: () => [
  107. {
  108. type: StepType.CONFIGURE,
  109. description: getCrashReportModalConfigDescription({
  110. link: 'https://docs.sentry.io/platforms/javascript/guides/connect/user-feedback/configuration/#crash-report-modal',
  111. }),
  112. },
  113. ],
  114. verify: () => [],
  115. nextSteps: () => [],
  116. };
  117. const docs: Docs = {
  118. onboarding,
  119. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  120. crashReportOnboarding,
  121. };
  122. export default docs;