koa.tsx 5.2 KB

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