angular.tsx 8.3 KB

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