angular.tsx 7.8 KB

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