nestjs.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. } from 'sentry/utils/gettingStartedDocs/node';
  23. type Params = DocsParams;
  24. const getSdkSetupSnippet = () => `
  25. ${getImportInstrumentSnippet('esm')}
  26. // All other imports below
  27. import { NestFactory } from '@nestjs/core';
  28. import { AppModule } from './app.module';
  29. async function bootstrap() {
  30. const app = await NestFactory.create(AppModule);
  31. await app.listen(3000);
  32. }
  33. bootstrap();
  34. `;
  35. const getAppModuleSnippet = () => `
  36. import { Module } from '@nestjs/common';
  37. import { SentryModule } from '@sentry/nestjs/setup';
  38. import { AppController } from './app.controller';
  39. import { AppService } from './app.service';
  40. @Module({
  41. imports: [
  42. SentryModule.forRoot(),
  43. // ...other modules
  44. ],
  45. controllers: [AppController],
  46. providers: [AppService],
  47. })
  48. export class AppModule {}
  49. `;
  50. const getVerifySnippet = () => `
  51. @Get("/debug-sentry")
  52. getError() {
  53. throw new Error("My first Sentry error!");
  54. }
  55. `;
  56. const onboarding: OnboardingConfig = {
  57. install: params => [
  58. {
  59. type: StepType.INSTALL,
  60. description: t('Add the Sentry NestJS SDK as a dependency:'),
  61. configurations: getInstallConfig(params, {
  62. basePackage: '@sentry/nestjs',
  63. }),
  64. },
  65. ],
  66. configure: params => [
  67. {
  68. type: StepType.CONFIGURE,
  69. description: t(
  70. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  71. ),
  72. configurations: [
  73. {
  74. description: tct(
  75. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  76. {code: <code />}
  77. ),
  78. code: [
  79. {
  80. label: 'JavaScript',
  81. value: 'javascript',
  82. language: 'javascript',
  83. filename: 'instrument.(js|ts)',
  84. code: getSdkInitSnippet(params, 'nestjs', 'esm'),
  85. },
  86. ],
  87. },
  88. {
  89. description: tct(
  90. 'Import [code1:instrument.js/mjs] in your [code2:main.ts/js] file:',
  91. {
  92. code1: <code />,
  93. code2: <code />,
  94. docs: (
  95. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
  96. ),
  97. }
  98. ),
  99. code: [
  100. {
  101. label: 'JavaScript',
  102. value: 'javascript',
  103. language: 'javascript',
  104. filename: 'main.(js|ts)',
  105. code: getSdkSetupSnippet(),
  106. },
  107. ],
  108. },
  109. {
  110. description: tct(
  111. 'Then you can add the [code1:SentryModule] as a root module. The [code2:SentryModule] needs to be registered before any other module that should be instrumented by Sentry.',
  112. {
  113. code1: <code />,
  114. code2: <code />,
  115. docs: (
  116. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
  117. ),
  118. }
  119. ),
  120. code: [
  121. {
  122. label: 'JavaScript',
  123. value: 'javascript',
  124. language: 'javascript',
  125. filename: 'app.module.(js|ts)',
  126. code: getAppModuleSnippet(),
  127. },
  128. ],
  129. },
  130. ],
  131. },
  132. getUploadSourceMapsStep({
  133. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/sourcemaps/',
  134. ...params,
  135. }),
  136. ],
  137. verify: () => [
  138. {
  139. type: StepType.VERIFY,
  140. description: t(
  141. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  142. ),
  143. configurations: [
  144. {
  145. language: 'javascript',
  146. code: getVerifySnippet(),
  147. },
  148. ],
  149. },
  150. ],
  151. };
  152. const feedbackOnboardingNode: OnboardingConfig = {
  153. introduction: () => getCrashReportApiIntroduction(),
  154. install: () => [
  155. {
  156. type: StepType.INSTALL,
  157. description: getCrashReportInstallDescription(),
  158. configurations: [
  159. {
  160. code: [
  161. {
  162. label: 'JavaScript',
  163. value: 'javascript',
  164. language: 'javascript',
  165. code: `import * as Sentry from "@sentry/node";
  166. const eventId = Sentry.captureMessage("User Feedback");
  167. // OR: const eventId = Sentry.lastEventId();
  168. const userFeedback = {
  169. event_id: eventId,
  170. name: "John Doe",
  171. email: "john@doe.com",
  172. comments: "I really like your App, thanks!",
  173. };
  174. Sentry.captureUserFeedback(userFeedback);
  175. `,
  176. },
  177. ],
  178. },
  179. ],
  180. },
  181. ],
  182. configure: () => [],
  183. verify: () => [],
  184. nextSteps: () => [],
  185. };
  186. const crashReportOnboarding: OnboardingConfig = {
  187. introduction: () => getCrashReportModalIntroduction(),
  188. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  189. configure: () => [
  190. {
  191. type: StepType.CONFIGURE,
  192. description: getCrashReportModalConfigDescription({
  193. link: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/user-feedback/configuration/#crash-report-modal',
  194. }),
  195. },
  196. ],
  197. verify: () => [],
  198. nextSteps: () => [],
  199. };
  200. const docs: Docs = {
  201. onboarding,
  202. feedbackOnboardingCrashApi: feedbackOnboardingNode,
  203. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  204. crashReportOnboarding,
  205. };
  206. export default docs;