koa.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. getCrashReportApiIntroduction,
  10. getCrashReportInstallDescription,
  11. getCrashReportJavaScriptInstallStep,
  12. getCrashReportModalConfigDescription,
  13. getCrashReportModalIntroduction,
  14. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  15. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  16. import {t, tct} from 'sentry/locale';
  17. import {
  18. getImportInstrumentSnippet,
  19. getInstallConfig,
  20. getSdkInitSnippet,
  21. getSentryImportSnippet,
  22. } from 'sentry/utils/gettingStartedDocs/node';
  23. type Params = DocsParams;
  24. const getSdkSetupSnippet = () => `
  25. ${getImportInstrumentSnippet()}
  26. ${getSentryImportSnippet('node')}
  27. const Koa = require("koa");
  28. const app = new Koa();
  29. Sentry.setupKoaErrorHandler(app);
  30. // All your controllers should live here
  31. app.listen(3000);`;
  32. const getVerifySnippet = () => `
  33. app.use(async function () {
  34. throw new Error("My first Sentry error!");
  35. });
  36. `;
  37. const onboarding: OnboardingConfig = {
  38. install: params => [
  39. {
  40. type: StepType.INSTALL,
  41. description: t('Add the Sentry Node SDK as a dependency:'),
  42. configurations: getInstallConfig(params),
  43. },
  44. ],
  45. configure: params => [
  46. {
  47. type: StepType.CONFIGURE,
  48. description: t(
  49. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  50. ),
  51. configurations: [
  52. {
  53. description: tct(
  54. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  55. {code: <code />}
  56. ),
  57. code: [
  58. {
  59. label: 'JavaScript',
  60. value: 'javascript',
  61. language: 'javascript',
  62. filename: 'instrument.(js|mjs)',
  63. code: getSdkInitSnippet(params, 'node'),
  64. },
  65. ],
  66. },
  67. {
  68. description: tct(
  69. "Make sure to import [code1:instrument.js/mjs] at the top of your file. Set up the error handler after all controllers and before any other error middleware. This setup is typically done in your application's entry point file, which is usually [code2:index.(js|ts)].",
  70. {code1: <code />, code2: <code />}
  71. ),
  72. code: [
  73. {
  74. label: 'JavaScript',
  75. value: 'javascript',
  76. language: 'javascript',
  77. code: getSdkSetupSnippet(),
  78. },
  79. ],
  80. },
  81. ],
  82. },
  83. getUploadSourceMapsStep({
  84. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/koa/sourcemaps/',
  85. ...params,
  86. }),
  87. ],
  88. verify: () => [
  89. {
  90. type: StepType.VERIFY,
  91. description: t(
  92. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  93. ),
  94. configurations: [
  95. {
  96. language: 'javascript',
  97. code: getVerifySnippet(),
  98. },
  99. ],
  100. },
  101. ],
  102. };
  103. const feedbackOnboardingNode: OnboardingConfig = {
  104. introduction: () => getCrashReportApiIntroduction(),
  105. install: () => [
  106. {
  107. type: StepType.INSTALL,
  108. description: getCrashReportInstallDescription(),
  109. configurations: [
  110. {
  111. code: [
  112. {
  113. label: 'JavaScript',
  114. value: 'javascript',
  115. language: 'javascript',
  116. code: `import * as Sentry from "@sentry/node";
  117. const eventId = Sentry.captureMessage("User Feedback");
  118. // OR: const eventId = Sentry.lastEventId();
  119. const userFeedback = {
  120. event_id: eventId,
  121. name: "John Doe",
  122. email: "john@doe.com",
  123. comments: "I really like your App, thanks!",
  124. };
  125. Sentry.captureUserFeedback(userFeedback);
  126. `,
  127. },
  128. ],
  129. },
  130. ],
  131. },
  132. ],
  133. configure: () => [],
  134. verify: () => [],
  135. nextSteps: () => [],
  136. };
  137. const crashReportOnboarding: OnboardingConfig = {
  138. introduction: () => getCrashReportModalIntroduction(),
  139. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  140. configure: () => [
  141. {
  142. type: StepType.CONFIGURE,
  143. description: getCrashReportModalConfigDescription({
  144. link: 'https://docs.sentry.io/platforms/javascript/guides/koa/user-feedback/configuration/#crash-report-modal',
  145. }),
  146. },
  147. ],
  148. verify: () => [],
  149. nextSteps: () => [],
  150. };
  151. const docs: Docs = {
  152. onboarding,
  153. feedbackOnboardingCrashApi: feedbackOnboardingNode,
  154. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  155. crashReportOnboarding,
  156. };
  157. export default docs;