angular.tsx 8.5 KB

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