angular.tsx 11 KB

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