connect.tsx 3.1 KB

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