angular.tsx 8.0 KB

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