angular.tsx 7.1 KB

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