angular.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  2. import tracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import type {
  5. Docs,
  6. DocsParams,
  7. OnboardingConfig,
  8. PlatformOption,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  11. import {getFeedbackConfigureDescription} from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  12. import {getJSMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  13. import {
  14. getReplayConfigOptions,
  15. getReplayConfigureDescription,
  16. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  17. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  18. import {t, tct} from 'sentry/locale';
  19. export enum AngularVersion {
  20. V10 = 'v10',
  21. V12 = 'v12',
  22. }
  23. type PlatformOptionKey = 'siblingOption';
  24. const platformOptions: Record<PlatformOptionKey, PlatformOption> = {
  25. siblingOption: {
  26. label: t('Angular Version'),
  27. items: [
  28. {
  29. label: t('Angular 12+'),
  30. value: AngularVersion.V12,
  31. },
  32. {
  33. label: t('Angular 10 & 11'),
  34. value: AngularVersion.V10,
  35. },
  36. ],
  37. },
  38. };
  39. type PlatformOptions = typeof platformOptions;
  40. type Params = DocsParams<PlatformOptions>;
  41. const getNextStep = (
  42. params: Params
  43. ): {
  44. description: string;
  45. id: string;
  46. link: string;
  47. name: string;
  48. }[] => {
  49. let nextStepDocs = [...nextSteps];
  50. if (params.isPerformanceSelected) {
  51. nextStepDocs = nextStepDocs.filter(
  52. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  53. );
  54. }
  55. if (params.isReplaySelected) {
  56. nextStepDocs = nextStepDocs.filter(
  57. step => step.id !== ProductSolution.SESSION_REPLAY
  58. );
  59. }
  60. return nextStepDocs;
  61. };
  62. function getNpmPackage(angularVersion) {
  63. return angularVersion === AngularVersion.V12
  64. ? '@sentry/angular-ivy'
  65. : '@sentry/angular';
  66. }
  67. const getInstallConfig = (params: Params) => [
  68. {
  69. language: 'bash',
  70. code: [
  71. {
  72. label: 'npm',
  73. value: 'npm',
  74. language: 'bash',
  75. code: `npm install --save ${getNpmPackage(params.platformOptions.siblingOption)}`,
  76. },
  77. {
  78. label: 'yarn',
  79. value: 'yarn',
  80. language: 'bash',
  81. code: `yarn add ${getNpmPackage(params.platformOptions.siblingOption)}`,
  82. },
  83. ],
  84. },
  85. ];
  86. const onboarding: OnboardingConfig<PlatformOptions> = {
  87. install: (params: Params) => [
  88. {
  89. type: StepType.INSTALL,
  90. description: tct(
  91. 'Add the Sentry SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn]:',
  92. {
  93. codeYarn: <code />,
  94. codeNpm: <code />,
  95. }
  96. ),
  97. configurations: getInstallConfig(params),
  98. },
  99. ],
  100. configure: (params: Params) => [
  101. {
  102. type: StepType.CONFIGURE,
  103. configurations: [
  104. getSetupConfiguration(params),
  105. {
  106. description: t(
  107. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler."
  108. ),
  109. language: 'javascript',
  110. code: `
  111. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  112. import { Router } from "@angular/router";
  113. import * as Sentry from "${getNpmPackage(params.platformOptions.siblingOption)}";
  114. @NgModule({
  115. // ...
  116. providers: [
  117. {
  118. provide: ErrorHandler,
  119. useValue: Sentry.createErrorHandler({
  120. showDialog: true,
  121. }),
  122. }, {
  123. provide: Sentry.TraceService,
  124. deps: [Router],
  125. },
  126. {
  127. provide: APP_INITIALIZER,
  128. useFactory: () => () => {},
  129. deps: [Sentry.TraceService],
  130. multi: true,
  131. },
  132. ],
  133. // ...
  134. })
  135. export class AppModule {}`,
  136. },
  137. ],
  138. },
  139. getUploadSourceMapsStep({
  140. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/angular/sourcemaps/',
  141. ...params,
  142. }),
  143. ],
  144. verify: () => [
  145. {
  146. type: StepType.VERIFY,
  147. description: t(
  148. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  149. ),
  150. configurations: [
  151. {
  152. language: 'javascript',
  153. code: `myUndefinedFunction();`,
  154. },
  155. ],
  156. },
  157. ],
  158. nextSteps: (params: Params) => getNextStep(params),
  159. };
  160. export const nextSteps = [
  161. {
  162. id: 'angular-features',
  163. name: t('Angular Features'),
  164. description: t('Learn about our first class integration with the Angular framework.'),
  165. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/features/',
  166. },
  167. {
  168. id: 'performance-monitoring',
  169. name: t('Performance Monitoring'),
  170. description: t(
  171. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  172. ),
  173. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/performance/',
  174. },
  175. {
  176. id: 'session-replay',
  177. name: t('Session Replay'),
  178. description: t(
  179. 'Get to the root cause of an error or latency issue faster by seeing all the technical details related to that issue in one visual replay on your web application.'
  180. ),
  181. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  182. },
  183. ];
  184. function getSdkSetupSnippet(params: Params) {
  185. const siblingOption = params.platformOptions.siblingOption;
  186. return `
  187. import { enableProdMode } from "@angular/core";
  188. import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
  189. import * as Sentry from "${getNpmPackage(siblingOption)}";
  190. import { AppModule } from "./app/app.module";
  191. Sentry.init({
  192. dsn: "${params.dsn}",
  193. integrations: [${
  194. params.isPerformanceSelected
  195. ? `
  196. Sentry.browserTracingIntegration(),`
  197. : ''
  198. }${
  199. params.isFeedbackSelected
  200. ? `
  201. Sentry.feedbackIntegration({
  202. // Additional SDK configuration goes in here, for example:
  203. colorScheme: "light",
  204. }),`
  205. : ''
  206. }${
  207. params.isReplaySelected
  208. ? `
  209. Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)}),`
  210. : ''
  211. }
  212. ],${
  213. params.isPerformanceSelected
  214. ? `
  215. // Performance Monitoring
  216. tracesSampleRate: 1.0, // Capture 100% of the transactions
  217. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  218. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  219. : ''
  220. }${
  221. params.isReplaySelected
  222. ? `
  223. // Session Replay
  224. replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
  225. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  226. : ''
  227. }
  228. });`;
  229. }
  230. function getSetupConfiguration(params: Params) {
  231. const configuration = {
  232. description: tct(
  233. `You should init the Sentry browser SDK in your [code:main.ts] file as soon as possible during application load up, before initializing Angular:`,
  234. {
  235. code: <code />,
  236. }
  237. ),
  238. language: 'javascript',
  239. code: `
  240. ${getSdkSetupSnippet(params)}
  241. enableProdMode();
  242. platformBrowserDynamic()
  243. .bootstrapModule(AppModule)
  244. .then((success) => console.log('Bootstrap success'))
  245. .catch((err) => console.error(err));`,
  246. };
  247. return configuration;
  248. }
  249. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  250. install: (params: Params) => [
  251. {
  252. type: StepType.INSTALL,
  253. description: tct(
  254. 'In order to use Session Replay, you will need version 7.27.0 of [codeAngular:@sentry/angular] or [codeIvy:@sentry/angular-ivy] at minimum. You do not need to install any additional packages.',
  255. {
  256. codeAngular: <code />,
  257. codeIvy: <code />,
  258. }
  259. ),
  260. configurations: getInstallConfig(params),
  261. },
  262. ],
  263. configure: (params: Params) => [
  264. {
  265. type: StepType.CONFIGURE,
  266. description: getReplayConfigureDescription({
  267. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  268. }),
  269. configurations: [
  270. {
  271. code: [
  272. {
  273. label: 'JavaScript',
  274. value: 'javascript',
  275. language: 'javascript',
  276. code: getSdkSetupSnippet(params),
  277. },
  278. ],
  279. },
  280. ],
  281. additionalInfo: tracePropagationMessage,
  282. },
  283. ],
  284. verify: () => [],
  285. nextSteps: () => [],
  286. };
  287. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  288. install: (params: Params) => [
  289. {
  290. type: StepType.INSTALL,
  291. description: tct(
  292. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [codeAngular:@sentry/angular] or [codeIvy:@sentry/angular-ivy]) installed, minimum version 7.85.0.',
  293. {
  294. codeAngular: <code />,
  295. codeIvy: <code />,
  296. }
  297. ),
  298. configurations: getInstallConfig(params),
  299. },
  300. ],
  301. configure: (params: Params) => [
  302. {
  303. type: StepType.CONFIGURE,
  304. description: getFeedbackConfigureDescription({
  305. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/',
  306. }),
  307. configurations: [
  308. {
  309. code: [
  310. {
  311. label: 'JavaScript',
  312. value: 'javascript',
  313. language: 'javascript',
  314. code: getSdkSetupSnippet(params),
  315. },
  316. ],
  317. },
  318. ],
  319. additionalInfo: crashReportCallout({
  320. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/#crash-report-modal',
  321. }),
  322. },
  323. ],
  324. verify: () => [],
  325. nextSteps: () => [],
  326. };
  327. const docs: Docs<PlatformOptions> = {
  328. onboarding,
  329. platformOptions,
  330. feedbackOnboardingNpm: feedbackOnboarding,
  331. replayOnboardingNpm: replayOnboarding,
  332. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  333. };
  334. export default docs;