capacitor.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  2. import type {
  3. Docs,
  4. DocsParams,
  5. OnboardingConfig,
  6. PlatformOption,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {
  9. getReplayConfigureDescription,
  10. getUploadSourceMapsStep,
  11. } from 'sentry/components/onboarding/gettingStartedDoc/utils';
  12. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  13. import {t, tct} from 'sentry/locale';
  14. export enum SiblingOption {
  15. ANGULARV10 = 'angularV10',
  16. ANGULARV12 = 'angularV12',
  17. REACT = 'react',
  18. VUE3 = 'vue3',
  19. VUE2 = 'vue2',
  20. }
  21. type PlaformOptionKey = 'siblingOption';
  22. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  23. siblingOption: {
  24. label: t('Sibling Package'),
  25. items: [
  26. {
  27. label: t('Angular 12+'),
  28. value: SiblingOption.ANGULARV12,
  29. },
  30. {
  31. label: t('Angular 10 and 11'),
  32. value: SiblingOption.ANGULARV10,
  33. },
  34. {
  35. label: t('React'),
  36. value: SiblingOption.REACT,
  37. },
  38. {
  39. label: t('Vue 3'),
  40. value: SiblingOption.VUE3,
  41. },
  42. {
  43. label: t('Vue 2'),
  44. value: SiblingOption.VUE2,
  45. },
  46. ],
  47. },
  48. };
  49. type PlatformOptions = typeof platformOptions;
  50. type Params = DocsParams<PlatformOptions>;
  51. const getSentryInitLayout = (params: Params, siblingOption: string): string => {
  52. return `${
  53. siblingOption === SiblingOption.VUE2
  54. ? `Vue,`
  55. : siblingOption === SiblingOption.VUE3
  56. ? 'app,'
  57. : ''
  58. }dsn: "${params.dsn}",
  59. integrations: [${
  60. params.isPerformanceSelected
  61. ? `
  62. new ${getSiblingImportName(siblingOption)}.BrowserTracing({
  63. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  64. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  65. ${
  66. params.isPerformanceSelected ? getPerformanceIntegration(siblingOption) : ''
  67. }})`
  68. : ''
  69. }${
  70. params.isReplaySelected
  71. ? `
  72. new ${getSiblingImportName(siblingOption)}.Replay(),`
  73. : ''
  74. }
  75. ],${
  76. params.isPerformanceSelected
  77. ? `
  78. // Performance Monitoring
  79. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  80. : ''
  81. }${
  82. params.isReplaySelected
  83. ? `
  84. // Session Replay
  85. 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.
  86. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  87. : ''
  88. }`;
  89. };
  90. const getNextStep = (
  91. params: Params
  92. ): {
  93. description: string;
  94. id: string;
  95. link: string;
  96. name: string;
  97. }[] => {
  98. let nextStepDocs = [...nextSteps];
  99. if (params.isPerformanceSelected) {
  100. nextStepDocs = nextStepDocs.filter(
  101. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  102. );
  103. }
  104. if (params.isReplaySelected) {
  105. nextStepDocs = nextStepDocs.filter(
  106. step => step.id !== ProductSolution.SESSION_REPLAY
  107. );
  108. }
  109. return nextStepDocs;
  110. };
  111. const isAngular = (siblingOption: string): boolean =>
  112. siblingOption === SiblingOption.ANGULARV10 ||
  113. siblingOption === SiblingOption.ANGULARV12;
  114. const isVue = (siblingOption: string): boolean =>
  115. siblingOption === SiblingOption.VUE2 || siblingOption === SiblingOption.VUE3;
  116. function getPerformanceIntegration(siblingOption: string): string {
  117. return `${
  118. isVue(siblingOption)
  119. ? `routingInstrumentation: SentryVue.vueRouterInstrumentation(router),`
  120. : isAngular(siblingOption)
  121. ? `routingInstrumentation: SentryAngular.routingInstrumentation,`
  122. : ''
  123. }`;
  124. }
  125. const performanceAngularErrorHandler = `,
  126. {
  127. provide: SentryAngular.TraceService,
  128. deps: [Router],
  129. },
  130. {
  131. provide: APP_INITIALIZER,
  132. useFactory: () => () => {},
  133. deps: [SentryAngular.TraceService],
  134. multi: true,
  135. },`;
  136. const getInstallStep = (params: Params) => [
  137. {
  138. type: StepType.INSTALL,
  139. description: (
  140. <p>
  141. {tct(
  142. `Install the Sentry Capacitor SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn], alongside the Sentry [siblingName:] SDK:`,
  143. {
  144. codeYarn: <code />,
  145. codeNpm: <code />,
  146. siblingName: getSiblingName(params.platformOptions.siblingOption),
  147. }
  148. )}
  149. </p>
  150. ),
  151. configurations: [
  152. {
  153. language: 'bash',
  154. code: [
  155. {
  156. label: 'npm',
  157. value: 'npm',
  158. language: 'bash',
  159. code: `npm install --save @sentry/capacitor ${getNpmPackage(
  160. params.platformOptions.siblingOption
  161. )}`,
  162. },
  163. {
  164. label: 'yarn',
  165. value: 'yarn',
  166. language: 'bash',
  167. code: `yarn add @sentry/capacitor ${getNpmPackage(
  168. params.platformOptions.siblingOption
  169. )} --exact`,
  170. },
  171. ],
  172. },
  173. {
  174. additionalInfo: (
  175. <p>
  176. {tct(
  177. `The version of the Sentry [siblingName:] SDK must match with the version referred by Sentry Capacitor. To check which version of the Sentry [siblingName:] SDK is installed, use the following command: [code:npm info @sentry/capacitor peerDependencies]`,
  178. {
  179. code: <code />,
  180. siblingName: getSiblingName(params.platformOptions.siblingOption),
  181. }
  182. )}
  183. </p>
  184. ),
  185. },
  186. ],
  187. },
  188. ];
  189. const onboarding: OnboardingConfig<PlatformOptions> = {
  190. install: params => getInstallStep(params),
  191. configure: params => [
  192. {
  193. type: StepType.CONFIGURE,
  194. configurations: getSetupConfiguration({params, showExtraStep: true}),
  195. },
  196. getUploadSourceMapsStep({
  197. guideLink:
  198. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/sourcemaps/',
  199. ...params,
  200. }),
  201. ],
  202. verify: _ => [
  203. {
  204. type: StepType.VERIFY,
  205. description: t(
  206. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  207. ),
  208. configurations: [
  209. {
  210. language: 'javascript',
  211. code: `myUndefinedFunction();`,
  212. },
  213. ],
  214. },
  215. ],
  216. nextSteps: params => getNextStep(params),
  217. };
  218. export const nextSteps = [
  219. {
  220. id: 'capacitor-android-setup',
  221. name: t('Capacitor 2 Setup'),
  222. description: t(
  223. 'If you are using Capacitor 2 or older, follow this step to add required changes in order to initialize the Capacitor SDK on Android.'
  224. ),
  225. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/?#capacitor-2---android-specifics',
  226. },
  227. {
  228. id: 'performance-monitoring',
  229. name: t('Performance Monitoring'),
  230. description: t(
  231. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  232. ),
  233. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/performance/',
  234. },
  235. {
  236. id: 'session-replay',
  237. name: t('Session Replay'),
  238. description: t(
  239. '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.'
  240. ),
  241. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  242. },
  243. ];
  244. function getSiblingImportsSetupConfiguration(siblingOption: string): string {
  245. switch (siblingOption) {
  246. case SiblingOption.VUE3:
  247. return `import {createApp} from "vue";
  248. import {createRouter} from "vue-router";`;
  249. case SiblingOption.VUE2:
  250. return `import Vue from "vue";
  251. import Router from "vue-router";`;
  252. default:
  253. return '';
  254. }
  255. }
  256. function getVueConstSetup(siblingOption: string): string {
  257. switch (siblingOption) {
  258. case SiblingOption.VUE3:
  259. return `
  260. const app = createApp({
  261. // ...
  262. });
  263. const router = createRouter({
  264. // ...
  265. });
  266. `;
  267. case SiblingOption.VUE2:
  268. return `
  269. Vue.use(Router);
  270. const router = new Router({
  271. // ...
  272. });
  273. `;
  274. default:
  275. return '';
  276. }
  277. }
  278. function getSetupConfiguration({
  279. params,
  280. showExtraStep,
  281. }: {
  282. params: Params;
  283. showExtraStep: boolean;
  284. }) {
  285. const siblingOption = params.platformOptions.siblingOption;
  286. const sentryInitLayout = getSentryInitLayout(params, siblingOption);
  287. const configuration = [
  288. {
  289. description: tct(
  290. `You should init the Sentry capacitor SDK in your [code:main.ts] file as soon as possible during application load up, before initializing Sentry [siblingName:]:`,
  291. {
  292. siblingName: getSiblingName(siblingOption),
  293. code: <code />,
  294. }
  295. ),
  296. language: 'javascript',
  297. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  298. import * as Sentry from '@sentry/capacitor';
  299. import * as ${getSiblingImportName(siblingOption)} from '${getNpmPackage(
  300. siblingOption
  301. )}';
  302. ${getVueConstSetup(siblingOption)}
  303. Sentry.init({
  304. ${sentryInitLayout}
  305. },
  306. // Forward the init method from ${getNpmPackage(params.platformOptions.siblingOption)}
  307. ${getSiblingImportName(siblingOption)}.init
  308. );`,
  309. },
  310. ];
  311. if (isAngular(siblingOption) && showExtraStep) {
  312. configuration.push({
  313. description: tct(
  314. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler.",
  315. {}
  316. ),
  317. language: 'javascript',
  318. code: `
  319. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  320. import { Router } from "@angular/router";
  321. import * as SentryAngular from "${getNpmPackage(siblingOption)}";
  322. @NgModule({
  323. // ...
  324. providers: [
  325. {
  326. provide: ErrorHandler,
  327. useValue: SentryAngular.createErrorHandler(),
  328. }${params.isPerformanceSelected ? performanceAngularErrorHandler : ' '}
  329. ],
  330. // ...
  331. })
  332. export class AppModule {}`,
  333. });
  334. }
  335. return configuration;
  336. }
  337. function getNpmPackage(siblingOption: string): string {
  338. const packages: Record<SiblingOption, string> = {
  339. [SiblingOption.ANGULARV10]: '@sentry/angular',
  340. [SiblingOption.ANGULARV12]: '@sentry/angular-ivy',
  341. [SiblingOption.REACT]: '@sentry/react',
  342. [SiblingOption.VUE3]: '@sentry/vue',
  343. [SiblingOption.VUE2]: '@sentry/vue',
  344. };
  345. return packages[siblingOption];
  346. }
  347. function getSiblingName(siblingOption: string): string {
  348. siblingOption;
  349. switch (siblingOption) {
  350. case SiblingOption.ANGULARV10:
  351. case SiblingOption.ANGULARV12:
  352. return 'Angular';
  353. case SiblingOption.REACT:
  354. return 'React';
  355. case SiblingOption.VUE2:
  356. case SiblingOption.VUE3:
  357. return 'Vue';
  358. default:
  359. return '';
  360. }
  361. }
  362. function getSiblingImportName(siblingOption: string): string {
  363. siblingOption;
  364. switch (siblingOption) {
  365. case SiblingOption.ANGULARV10:
  366. case SiblingOption.ANGULARV12:
  367. return 'SentryAngular';
  368. case SiblingOption.REACT:
  369. return 'SentryReact';
  370. case SiblingOption.VUE2:
  371. case SiblingOption.VUE3:
  372. return 'SentryVue';
  373. default:
  374. return '';
  375. }
  376. }
  377. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  378. install: params => getInstallStep(params),
  379. configure: params => [
  380. {
  381. type: StepType.CONFIGURE,
  382. configurations: [
  383. {
  384. description: getReplayConfigureDescription({
  385. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  386. }),
  387. configurations: getSetupConfiguration({
  388. params: {...params, isReplaySelected: true},
  389. showExtraStep: false,
  390. }),
  391. },
  392. ],
  393. },
  394. ],
  395. verify: () => [],
  396. nextSteps: () => [],
  397. };
  398. const docs: Docs<PlatformOptions> = {
  399. onboarding,
  400. platformOptions,
  401. replayOnboardingNpm: replayOnboarding,
  402. };
  403. export default docs;