angular.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  2. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  3. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. Docs,
  7. DocsParams,
  8. OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  11. import {
  12. getCrashReportJavaScriptInstallStep,
  13. getCrashReportModalConfigDescription,
  14. getCrashReportModalIntroduction,
  15. getFeedbackConfigOptions,
  16. getFeedbackConfigureDescription,
  17. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  18. import {getJSMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  19. import {
  20. getReplayConfigOptions,
  21. getReplayConfigureDescription,
  22. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  23. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  24. import {t, tct} from 'sentry/locale';
  25. type Params = DocsParams;
  26. const getNextStep = (
  27. params: Params
  28. ): {
  29. description: string;
  30. id: string;
  31. link: string;
  32. name: string;
  33. }[] => {
  34. let nextStepDocs = [...nextSteps];
  35. if (params.isPerformanceSelected) {
  36. nextStepDocs = nextStepDocs.filter(
  37. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  38. );
  39. }
  40. if (params.isReplaySelected) {
  41. nextStepDocs = nextStepDocs.filter(
  42. step => step.id !== ProductSolution.SESSION_REPLAY
  43. );
  44. }
  45. return nextStepDocs;
  46. };
  47. const getInstallConfig = () => [
  48. {
  49. language: 'bash',
  50. code: [
  51. {
  52. label: 'npm',
  53. value: 'npm',
  54. language: 'bash',
  55. code: `npm install --save @sentry/angular`,
  56. },
  57. {
  58. label: 'yarn',
  59. value: 'yarn',
  60. language: 'bash',
  61. code: `yarn add @sentry/angular`,
  62. },
  63. {
  64. label: 'pnpm',
  65. value: 'pnpm',
  66. language: 'bash',
  67. code: `pnpm install @sentry/angular`,
  68. },
  69. ],
  70. },
  71. ];
  72. const onboarding: OnboardingConfig = {
  73. install: () => [
  74. {
  75. type: StepType.INSTALL,
  76. description: tct(
  77. 'Add the Sentry SDK as a dependency using [codeNpm:npm], [codeYarn:yarn] or [codePnpm:pnpm]:',
  78. {
  79. codeYarn: <code />,
  80. codeNpm: <code />,
  81. codePnpm: <code />,
  82. }
  83. ),
  84. configurations: getInstallConfig(),
  85. },
  86. ],
  87. configure: (params: Params) => [
  88. {
  89. type: StepType.CONFIGURE,
  90. configurations: [
  91. getSetupConfiguration(params),
  92. {
  93. description: tct(
  94. "Register the Sentry Angular SDK's ErrorHandler and Tracing providers in your [codeModule:app.module.ts] file:",
  95. {
  96. codeModule: <code />,
  97. }
  98. ),
  99. language: 'javascript',
  100. code: `
  101. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  102. import { Router } from "@angular/router";
  103. import * as Sentry from "@sentry/angular";
  104. @NgModule({
  105. // ...
  106. providers: [
  107. {
  108. provide: ErrorHandler,
  109. useValue: Sentry.createErrorHandler({
  110. showDialog: true,
  111. }),
  112. }, {
  113. provide: Sentry.TraceService,
  114. deps: [Router],
  115. },
  116. {
  117. provide: APP_INITIALIZER,
  118. useFactory: () => () => {},
  119. deps: [Sentry.TraceService],
  120. multi: true,
  121. },
  122. ],
  123. // ...
  124. })
  125. export class AppModule {}`,
  126. },
  127. ],
  128. },
  129. getUploadSourceMapsStep({
  130. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/angular/sourcemaps/',
  131. ...params,
  132. }),
  133. ],
  134. verify: () => [
  135. {
  136. type: StepType.VERIFY,
  137. description: t(
  138. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  139. ),
  140. configurations: [
  141. {
  142. language: 'javascript',
  143. code: `myUndefinedFunction();`,
  144. },
  145. ],
  146. },
  147. ],
  148. nextSteps: (params: Params) => getNextStep(params),
  149. };
  150. export const nextSteps = [
  151. {
  152. id: 'angular-features',
  153. name: t('Angular Features'),
  154. description: t('Learn about our first class integration with the Angular framework.'),
  155. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/features/',
  156. },
  157. {
  158. id: 'performance-monitoring',
  159. name: t('Performance Monitoring'),
  160. description: t(
  161. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  162. ),
  163. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/tracing/',
  164. },
  165. {
  166. id: 'session-replay',
  167. name: t('Session Replay'),
  168. description: t(
  169. '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.'
  170. ),
  171. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  172. },
  173. ];
  174. function getSdkSetupSnippet(params: Params) {
  175. return `
  176. import { enableProdMode } from "@angular/core";
  177. import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
  178. import * as Sentry from "@sentry/angular";
  179. import { AppModule } from "./app/app.module";
  180. Sentry.init({
  181. dsn: "${params.dsn}",
  182. integrations: [${
  183. params.isPerformanceSelected
  184. ? `
  185. Sentry.browserTracingIntegration(),`
  186. : ''
  187. }${
  188. params.isProfilingSelected
  189. ? `
  190. Sentry.browserProfilingIntegration(),`
  191. : ''
  192. }${
  193. params.isFeedbackSelected
  194. ? `
  195. Sentry.feedbackIntegration({
  196. // Additional SDK configuration goes in here, for example:
  197. colorScheme: "system",
  198. ${getFeedbackConfigOptions(params.feedbackOptions)}}),`
  199. : ''
  200. }${
  201. params.isReplaySelected
  202. ? `
  203. Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)}),`
  204. : ''
  205. }
  206. ],${
  207. params.isPerformanceSelected
  208. ? `
  209. // Performance Monitoring
  210. tracesSampleRate: 1.0, // Capture 100% of the transactions
  211. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  212. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  213. : ''
  214. }${
  215. params.isReplaySelected
  216. ? `
  217. // Session Replay
  218. 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.
  219. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  220. : ''
  221. }${
  222. params.isProfilingSelected
  223. ? `
  224. // Set profilesSampleRate to 1.0 to profile every transaction.
  225. // Since profilesSampleRate is relative to tracesSampleRate,
  226. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  227. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  228. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  229. profilesSampleRate: 1.0,`
  230. : ''
  231. }
  232. });`;
  233. }
  234. function getSetupConfiguration(params: Params) {
  235. const configuration = {
  236. description: tct(
  237. `Initialize the Sentry Angular SDK in your [code:main.ts] file as early as possible, before initializing Angular:`,
  238. {
  239. code: <code />,
  240. }
  241. ),
  242. language: 'javascript',
  243. code: `
  244. ${getSdkSetupSnippet(params)}
  245. enableProdMode();
  246. platformBrowserDynamic()
  247. .bootstrapModule(AppModule)
  248. .catch((err) => console.error(err));`,
  249. };
  250. return configuration;
  251. }
  252. const replayOnboarding: OnboardingConfig = {
  253. install: () => [
  254. {
  255. type: StepType.INSTALL,
  256. description: tct(
  257. 'In order to use Session Replay, you will need version 7.27.0 of [codeAngular:@sentry/angular] at minimum. You do not need to install any additional packages.',
  258. {
  259. codeAngular: <code />,
  260. codeIvy: <code />,
  261. }
  262. ),
  263. configurations: getInstallConfig(),
  264. },
  265. ],
  266. configure: (params: Params) => [
  267. {
  268. type: StepType.CONFIGURE,
  269. description: getReplayConfigureDescription({
  270. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  271. }),
  272. configurations: [
  273. {
  274. code: [
  275. {
  276. label: 'JavaScript',
  277. value: 'javascript',
  278. language: 'javascript',
  279. code: getSdkSetupSnippet(params),
  280. },
  281. ],
  282. },
  283. ],
  284. additionalInfo: <TracePropagationMessage />,
  285. },
  286. ],
  287. verify: () => [],
  288. nextSteps: () => [],
  289. };
  290. const feedbackOnboarding: OnboardingConfig = {
  291. install: () => [
  292. {
  293. type: StepType.INSTALL,
  294. description: tct(
  295. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [codeAngular:@sentry/angular]) installed, minimum version 7.85.0.',
  296. {
  297. codeAngular: <code />,
  298. codeIvy: <code />,
  299. }
  300. ),
  301. configurations: getInstallConfig(),
  302. },
  303. ],
  304. configure: (params: Params) => [
  305. {
  306. type: StepType.CONFIGURE,
  307. description: getFeedbackConfigureDescription({
  308. linkConfig:
  309. 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/',
  310. linkButton:
  311. 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/#bring-your-own-button',
  312. }),
  313. configurations: [
  314. {
  315. code: [
  316. {
  317. label: 'JavaScript',
  318. value: 'javascript',
  319. language: 'javascript',
  320. code: getSdkSetupSnippet(params),
  321. },
  322. ],
  323. },
  324. ],
  325. additionalInfo: crashReportCallout({
  326. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/#crash-report-modal',
  327. }),
  328. },
  329. ],
  330. verify: () => [],
  331. nextSteps: () => [],
  332. };
  333. const crashReportOnboarding: OnboardingConfig = {
  334. introduction: () => getCrashReportModalIntroduction(),
  335. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  336. configure: () => [
  337. {
  338. type: StepType.CONFIGURE,
  339. description: getCrashReportModalConfigDescription({
  340. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/#crash-report-modal',
  341. }),
  342. additionalInfo: widgetCallout({
  343. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/#user-feedback-widget',
  344. }),
  345. },
  346. ],
  347. verify: () => [],
  348. nextSteps: () => [],
  349. };
  350. const docs: Docs = {
  351. onboarding,
  352. feedbackOnboardingNpm: feedbackOnboarding,
  353. replayOnboardingNpm: replayOnboarding,
  354. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  355. crashReportOnboarding,
  356. };
  357. export default docs;