angular.tsx 11 KB

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