angular.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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/performance/',
  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.isFeedbackSelected
  189. ? `
  190. Sentry.feedbackIntegration({
  191. // Additional SDK configuration goes in here, for example:
  192. colorScheme: "system",
  193. ${getFeedbackConfigOptions(params.feedbackOptions)}}),`
  194. : ''
  195. }${
  196. params.isReplaySelected
  197. ? `
  198. Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)}),`
  199. : ''
  200. }
  201. ],${
  202. params.isPerformanceSelected
  203. ? `
  204. // Performance Monitoring
  205. tracesSampleRate: 1.0, // Capture 100% of the transactions
  206. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  207. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  208. : ''
  209. }${
  210. params.isReplaySelected
  211. ? `
  212. // Session Replay
  213. 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.
  214. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  215. : ''
  216. }
  217. });`;
  218. }
  219. function getSetupConfiguration(params: Params) {
  220. const configuration = {
  221. description: tct(
  222. `Initialize the Sentry Angular SDK in your [code:main.ts] file as early as possible, before initializing Angular:`,
  223. {
  224. code: <code />,
  225. }
  226. ),
  227. language: 'javascript',
  228. code: `
  229. ${getSdkSetupSnippet(params)}
  230. enableProdMode();
  231. platformBrowserDynamic()
  232. .bootstrapModule(AppModule)
  233. .catch((err) => console.error(err));`,
  234. };
  235. return configuration;
  236. }
  237. const replayOnboarding: OnboardingConfig = {
  238. install: () => [
  239. {
  240. type: StepType.INSTALL,
  241. description: tct(
  242. '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.',
  243. {
  244. codeAngular: <code />,
  245. codeIvy: <code />,
  246. }
  247. ),
  248. configurations: getInstallConfig(),
  249. },
  250. ],
  251. configure: (params: Params) => [
  252. {
  253. type: StepType.CONFIGURE,
  254. description: getReplayConfigureDescription({
  255. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/session-replay/',
  256. }),
  257. configurations: [
  258. {
  259. code: [
  260. {
  261. label: 'JavaScript',
  262. value: 'javascript',
  263. language: 'javascript',
  264. code: getSdkSetupSnippet(params),
  265. },
  266. ],
  267. },
  268. ],
  269. additionalInfo: <TracePropagationMessage />,
  270. },
  271. ],
  272. verify: () => [],
  273. nextSteps: () => [],
  274. };
  275. const feedbackOnboarding: OnboardingConfig = {
  276. install: () => [
  277. {
  278. type: StepType.INSTALL,
  279. description: tct(
  280. '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.',
  281. {
  282. codeAngular: <code />,
  283. codeIvy: <code />,
  284. }
  285. ),
  286. configurations: getInstallConfig(),
  287. },
  288. ],
  289. configure: (params: Params) => [
  290. {
  291. type: StepType.CONFIGURE,
  292. description: getFeedbackConfigureDescription({
  293. linkConfig:
  294. 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/',
  295. linkButton:
  296. 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/#bring-your-own-button',
  297. }),
  298. configurations: [
  299. {
  300. code: [
  301. {
  302. label: 'JavaScript',
  303. value: 'javascript',
  304. language: 'javascript',
  305. code: getSdkSetupSnippet(params),
  306. },
  307. ],
  308. },
  309. ],
  310. additionalInfo: crashReportCallout({
  311. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/#crash-report-modal',
  312. }),
  313. },
  314. ],
  315. verify: () => [],
  316. nextSteps: () => [],
  317. };
  318. const crashReportOnboarding: OnboardingConfig = {
  319. introduction: () => getCrashReportModalIntroduction(),
  320. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  321. configure: () => [
  322. {
  323. type: StepType.CONFIGURE,
  324. description: getCrashReportModalConfigDescription({
  325. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/configuration/#crash-report-modal',
  326. }),
  327. additionalInfo: widgetCallout({
  328. link: 'https://docs.sentry.io/platforms/javascript/guides/angular/user-feedback/#user-feedback-widget',
  329. }),
  330. },
  331. ],
  332. verify: () => [],
  333. nextSteps: () => [],
  334. };
  335. const docs: Docs = {
  336. onboarding,
  337. feedbackOnboardingNpm: feedbackOnboarding,
  338. replayOnboardingNpm: replayOnboarding,
  339. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  340. crashReportOnboarding,
  341. };
  342. export default docs;