capacitor.tsx 12 KB

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