angular.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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, reduce in production!
  58. `;
  59. const performanceErrorHandler = `
  60. {
  61. provide: Sentry.TraceService,
  62. deps: [Router],
  63. },
  64. {
  65. provide: APP_INITIALIZER,
  66. useFactory: () => () => {},
  67. deps: [Sentry.TraceService],
  68. multi: true,
  69. },
  70. `;
  71. function getNpmPackage(angularVersion: AngularVersion) {
  72. return angularVersion === AngularVersion.V12
  73. ? '@sentry/angular-ivy'
  74. : '@sentry/angular';
  75. }
  76. export const steps = ({
  77. sentryInitContent,
  78. errorHandlerProviders,
  79. angularVersion,
  80. ...props
  81. }: StepProps): LayoutProps['steps'] => [
  82. {
  83. type: StepType.INSTALL,
  84. description: (
  85. <p>
  86. {tct(
  87. 'Add the Sentry SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn]:',
  88. {
  89. codeYarn: <code />,
  90. codeNpm: <code />,
  91. }
  92. )}
  93. </p>
  94. ),
  95. configurations: [
  96. {
  97. language: 'bash',
  98. code: [
  99. {
  100. label: 'npm',
  101. value: 'npm',
  102. language: 'bash',
  103. code: `npm install --save ${getNpmPackage(angularVersion)}`,
  104. },
  105. {
  106. label: 'yarn',
  107. value: 'yarn',
  108. language: 'bash',
  109. code: `yarn add ${getNpmPackage(angularVersion)}`,
  110. },
  111. ],
  112. },
  113. ],
  114. },
  115. {
  116. type: StepType.CONFIGURE,
  117. description: t(
  118. 'You should init the Sentry browser SDK in your main.ts file as soon as possible during application load up, before initializing Angular:'
  119. ),
  120. configurations: [
  121. {
  122. language: 'javascript',
  123. code: `
  124. import { enableProdMode } from "@angular/core";
  125. import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
  126. import * as Sentry from "${getNpmPackage(angularVersion)}";
  127. import { AppModule } from "./app/app.module";
  128. Sentry.init({
  129. ${sentryInitContent}
  130. });
  131. enableProdMode();
  132. platformBrowserDynamic()
  133. .bootstrapModule(AppModule)
  134. .then((success) => console.log('Bootstrap success'))
  135. .catch((err) => console.error(err));
  136. `,
  137. },
  138. {
  139. description: t(
  140. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler."
  141. ),
  142. language: 'javascript',
  143. code: `
  144. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  145. import { Router } from "@angular/router";
  146. import * as Sentry from "${getNpmPackage(angularVersion)}";
  147. @NgModule({
  148. // ...
  149. providers: [
  150. {
  151. provide: ErrorHandler,
  152. useValue: Sentry.createErrorHandler({
  153. showDialog: true,
  154. }),
  155. },${errorHandlerProviders}
  156. ],
  157. // ...
  158. })
  159. export class AppModule {}`,
  160. },
  161. ],
  162. },
  163. getUploadSourceMapsStep({
  164. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/angular/sourcemaps/',
  165. ...props,
  166. }),
  167. {
  168. type: StepType.VERIFY,
  169. description: t(
  170. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  171. ),
  172. configurations: [
  173. {
  174. language: 'javascript',
  175. code: `myUndefinedFunction();`,
  176. },
  177. ],
  178. },
  179. ];
  180. export const nextSteps = [
  181. {
  182. id: 'angular-features',
  183. name: t('Angular Features'),
  184. description: t('Learn about our first class integration with the Angular framework.'),
  185. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/features/',
  186. },
  187. {
  188. id: 'performance-monitoring',
  189. name: t('Performance Monitoring'),
  190. description: t(
  191. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  192. ),
  193. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/performance/',
  194. },
  195. {
  196. id: 'session-replay',
  197. name: t('Session Replay'),
  198. description: t(
  199. '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.'
  200. ),
  201. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  202. },
  203. ];
  204. // Configuration End
  205. export function GettingStartedWithAngular({
  206. dsn,
  207. activeProductSelection = [],
  208. organization,
  209. newOrg,
  210. platformKey,
  211. projectId,
  212. ...props
  213. }: ModuleProps) {
  214. const optionValues = useUrlPlatformOptions(platformOptions);
  215. const integrations: string[] = [];
  216. const otherConfigs: string[] = [];
  217. let nextStepDocs = [...nextSteps];
  218. const errorHandlerProviders: string[] = [];
  219. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  220. integrations.push(performanceIntegration.trim());
  221. otherConfigs.push(performanceOtherConfig.trim());
  222. errorHandlerProviders.push(performanceErrorHandler.trim());
  223. nextStepDocs = nextStepDocs.filter(
  224. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  225. );
  226. }
  227. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  228. integrations.push(replayIntegration.trim());
  229. otherConfigs.push(replayOtherConfig.trim());
  230. nextStepDocs = nextStepDocs.filter(
  231. step => step.id !== ProductSolution.SESSION_REPLAY
  232. );
  233. }
  234. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  235. if (integrations.length > 0) {
  236. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  237. }
  238. if (otherConfigs.length > 0) {
  239. sentryInitContent = sentryInitContent.concat(otherConfigs);
  240. }
  241. return (
  242. <Layout
  243. steps={steps({
  244. sentryInitContent: sentryInitContent.join('\n'),
  245. errorHandlerProviders: errorHandlerProviders.join('\n'),
  246. angularVersion: optionValues.angularVersion as AngularVersion,
  247. organization,
  248. newOrg,
  249. platformKey,
  250. projectId,
  251. })}
  252. nextSteps={nextStepDocs}
  253. platformOptions={platformOptions}
  254. newOrg={newOrg}
  255. platformKey={platformKey}
  256. {...props}
  257. />
  258. );
  259. }
  260. export default GettingStartedWithAngular;