capacitor.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  9. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  10. import {t, tct} from 'sentry/locale';
  11. export enum SiblingOption {
  12. ANGULARV10 = 'angularV10',
  13. ANGULARV12 = 'angularV12',
  14. REACT = 'react',
  15. VUE3 = 'vue3',
  16. VUE2 = 'vue2',
  17. }
  18. type PlaformOptionKey = 'siblingOption';
  19. const getSentryInitLayout = (params: Params): string => {
  20. const otherConfigs: string[] = [];
  21. let content: string[] = [];
  22. let integrations: string[] = [];
  23. if (params.isPerformanceSelected) {
  24. integrations = getPerformanceIntegration(params.platformOptions.siblingOption);
  25. otherConfigs.push(performanceOtherConfig);
  26. }
  27. if (params.isReplaySelected) {
  28. integrations.push(replayIntegration.trim());
  29. otherConfigs.push(replayOtherConfig.trim());
  30. }
  31. if (params.platformOptions.siblingOption === SiblingOption.VUE3) {
  32. content.push(`app`);
  33. } else if (params.platformOptions.siblingOption === SiblingOption.VUE2) {
  34. content.push(`Vue`);
  35. }
  36. content.push(`dsn: "${params.dsn}"`);
  37. if (integrations.length > 0) {
  38. content.push(`integrations: [
  39. ${integrations}
  40. ]`);
  41. }
  42. if (otherConfigs.length > 0) {
  43. content = content.concat(otherConfigs);
  44. }
  45. return content.join();
  46. };
  47. const getNextStep = (
  48. params: Params
  49. ): {
  50. description: string;
  51. id: string;
  52. link: string;
  53. name: string;
  54. }[] => {
  55. let nextStepDocs = [...nextSteps];
  56. if (params.isPerformanceSelected) {
  57. nextStepDocs = nextStepDocs.filter(
  58. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  59. );
  60. }
  61. if (params.isReplaySelected) {
  62. nextStepDocs = nextStepDocs.filter(
  63. step => step.id !== ProductSolution.SESSION_REPLAY
  64. );
  65. }
  66. return nextStepDocs;
  67. };
  68. const isAngular = (siblingOption: string): boolean =>
  69. siblingOption === SiblingOption.ANGULARV10 ||
  70. siblingOption === SiblingOption.ANGULARV12;
  71. const isVue = (siblingOption: string): boolean =>
  72. siblingOption === SiblingOption.VUE2 || siblingOption === SiblingOption.VUE3;
  73. // Configuration Start
  74. const platformOptions: Record<PlaformOptionKey, PlatformOption> = {
  75. siblingOption: {
  76. label: t('Sibling Package'),
  77. items: [
  78. {
  79. label: t('Angular 12+'),
  80. value: SiblingOption.ANGULARV12,
  81. },
  82. {
  83. label: t('Angular 10 and 11'),
  84. value: SiblingOption.ANGULARV10,
  85. },
  86. {
  87. label: t('React'),
  88. value: SiblingOption.REACT,
  89. },
  90. {
  91. label: t('Vue 3'),
  92. value: SiblingOption.VUE3,
  93. },
  94. {
  95. label: t('Vue 2'),
  96. value: SiblingOption.VUE2,
  97. },
  98. ],
  99. },
  100. };
  101. type PlatformOptions = typeof platformOptions;
  102. type Params = DocsParams<PlatformOptions>;
  103. const replayIntegration = `
  104. new SiblingSdk.Replay(),
  105. `;
  106. const replayOtherConfig = `
  107. // Session Replay
  108. 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.
  109. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  110. `;
  111. function getPerformanceIntegration(siblingOption: string): string[] {
  112. const integration: string[] = [];
  113. integration.push(`new SiblingSdk.BrowserTracing({
  114. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  115. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/]`);
  116. if (isVue(siblingOption)) {
  117. integration.push(
  118. 'routingInstrumentation: SiblingSdk.vueRouterInstrumentation(router)'
  119. );
  120. }
  121. if (isAngular(siblingOption)) {
  122. integration.push('routingInstrumentation: SiblingSdk.routingInstrumentation');
  123. }
  124. integration.push(`}),`);
  125. return integration;
  126. }
  127. const performanceOtherConfig = `
  128. // Performance Monitoring
  129. tracesSampleRate: 1.0, // Capture 100% of the transactions`;
  130. const performanceAngularErrorHandler = `,
  131. {
  132. provide: SiblingSdk.TraceService,
  133. deps: [Router],
  134. },
  135. {
  136. provide: APP_INITIALIZER,
  137. useFactory: () => () => {},
  138. deps: [SiblingSdk.TraceService],
  139. multi: true,
  140. },`;
  141. const onboarding: OnboardingConfig<PlatformOptions> = {
  142. install: params => [
  143. {
  144. type: StepType.INSTALL,
  145. description: (
  146. <p>
  147. {tct(
  148. `Install the Sentry Capacitor SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn], alongside the Sentry [siblingName:] SDK:`,
  149. {
  150. codeYarn: <code />,
  151. codeNpm: <code />,
  152. siblingName: getSiblingName(params.platformOptions.siblingOption),
  153. }
  154. )}
  155. </p>
  156. ),
  157. configurations: [
  158. {
  159. language: 'bash',
  160. code: [
  161. {
  162. label: 'npm',
  163. value: 'npm',
  164. language: 'bash',
  165. code: `npm install --save @sentry/capacitor ${getNpmPackage(
  166. params.platformOptions.siblingOption
  167. )}`,
  168. },
  169. {
  170. label: 'yarn',
  171. value: 'yarn',
  172. language: 'bash',
  173. code: `yarn add @sentry/capacitor ${getNpmPackage(
  174. params.platformOptions.siblingOption
  175. )} --exact`,
  176. },
  177. ],
  178. },
  179. {
  180. additionalInfo: (
  181. <p>
  182. {tct(
  183. `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]`,
  184. {
  185. code: <code />,
  186. siblingName: getSiblingName(params.platformOptions.siblingOption),
  187. }
  188. )}
  189. </p>
  190. ),
  191. },
  192. ],
  193. },
  194. ],
  195. configure: params => [
  196. {
  197. type: StepType.CONFIGURE,
  198. configurations: getSetupConfiguration(params),
  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(params: Params) {
  283. const sentryInitLayout = getSentryInitLayout(params);
  284. const siblingOption = params.platformOptions.siblingOption;
  285. const configuration = [
  286. {
  287. description: tct(
  288. `You should init the Sentry capacitor SDK in your main.ts file as soon as possible during application load up, before initializing Sentry [siblingName:]:`,
  289. {
  290. siblingName: getSiblingName(siblingOption),
  291. }
  292. ),
  293. language: 'javascript',
  294. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  295. import * as Sentry from '@sentry/capacitor';
  296. import * as SiblingSdk from '${getNpmPackage(siblingOption)}';
  297. ${getVueConstSetup(siblingOption)}
  298. Sentry.init({
  299. ${sentryInitLayout}
  300. },
  301. // Forward the init method from ${getNpmPackage(params.platformOptions.siblingOption)}
  302. SiblingSdk.init
  303. );`,
  304. },
  305. ];
  306. if (isAngular(siblingOption)) {
  307. configuration.push({
  308. description: tct(
  309. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler.",
  310. {}
  311. ),
  312. language: 'javascript',
  313. code: `
  314. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  315. import { Router } from "@angular/router";
  316. import * as SiblingSdk from "${getNpmPackage(siblingOption)}";
  317. @NgModule({
  318. // ...
  319. providers: [
  320. {
  321. provide: ErrorHandler,
  322. useValue: SiblingSdk.createErrorHandler(),
  323. }${params.isPerformanceSelected ? performanceAngularErrorHandler : ' '}
  324. ],
  325. // ...
  326. })
  327. export class AppModule {}`,
  328. });
  329. }
  330. return configuration;
  331. }
  332. function getNpmPackage(siblingOption: string): string {
  333. const packages: Record<SiblingOption, string> = {
  334. [SiblingOption.ANGULARV10]: '@sentry/angular',
  335. [SiblingOption.ANGULARV12]: '@sentry/angular-ivy',
  336. [SiblingOption.REACT]: '@sentry/react',
  337. [SiblingOption.VUE3]: '@sentry/vue',
  338. [SiblingOption.VUE2]: '@sentry/vue',
  339. };
  340. return packages[siblingOption];
  341. }
  342. function getSiblingName(siblingOption: string): string {
  343. siblingOption;
  344. switch (siblingOption) {
  345. case SiblingOption.ANGULARV10:
  346. case SiblingOption.ANGULARV12:
  347. return 'Angular';
  348. case SiblingOption.REACT:
  349. return 'React';
  350. case SiblingOption.VUE2:
  351. case SiblingOption.VUE3:
  352. return 'Vue';
  353. default:
  354. return '';
  355. }
  356. }
  357. // Configuration End
  358. const docs: Docs<PlatformOptions> = {
  359. onboarding,
  360. platformOptions,
  361. };
  362. export default docs;