angular.tsx 8.0 KB

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