connect.tsx 3.4 KB

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