svelte.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import {Fragment} from 'react';
  2. import {buildSdkConfig} from 'sentry/components/onboarding/gettingStartedDoc/buildSdkConfig';
  3. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  4. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  5. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  6. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  7. import type {
  8. Docs,
  9. DocsParams,
  10. OnboardingConfig,
  11. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  12. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  13. import {
  14. getCrashReportJavaScriptInstallStep,
  15. getCrashReportModalConfigDescription,
  16. getCrashReportModalIntroduction,
  17. getFeedbackConfigOptions,
  18. getFeedbackConfigureDescription,
  19. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  20. import {
  21. getProfilingDocumentHeaderConfigurationStep,
  22. MaybeBrowserProfilingBetaWarning,
  23. } from 'sentry/components/onboarding/gettingStartedDoc/utils/profilingOnboarding';
  24. import {
  25. getReplayConfigOptions,
  26. getReplayConfigureDescription,
  27. getReplayVerifyStep,
  28. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  29. import {featureFlagOnboarding} from 'sentry/gettingStartedDocs/javascript/javascript';
  30. import {t, tct} from 'sentry/locale';
  31. type Params = DocsParams;
  32. const getIntegrations = (params: Params): string[] => {
  33. const integrations = [];
  34. if (params.isPerformanceSelected) {
  35. integrations.push(`Sentry.browserTracingIntegration()`);
  36. }
  37. if (params.isProfilingSelected) {
  38. integrations.push(`Sentry.browserProfilingIntegration()`);
  39. }
  40. if (params.isReplaySelected) {
  41. integrations.push(
  42. `Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)})`
  43. );
  44. }
  45. if (params.isFeedbackSelected) {
  46. integrations.push(`
  47. Sentry.feedbackIntegration({
  48. colorScheme: "system",
  49. ${getFeedbackConfigOptions(params.feedbackOptions)}
  50. }),`);
  51. }
  52. return integrations;
  53. };
  54. const getDynamicParts = (params: Params): string[] => {
  55. const dynamicParts: string[] = [];
  56. if (params.isPerformanceSelected) {
  57. dynamicParts.push(`
  58. // Tracing
  59. tracesSampleRate: 1.0, // Capture 100% of the transactions
  60. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  61. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/]`);
  62. }
  63. if (params.isReplaySelected) {
  64. dynamicParts.push(`
  65. // Session Replay
  66. 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.
  67. replaysOnErrorSampleRate: 1.0 // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`);
  68. }
  69. if (params.isProfilingSelected) {
  70. dynamicParts.push(`
  71. // Set profilesSampleRate to 1.0 to profile every transaction.
  72. // Since profilesSampleRate is relative to tracesSampleRate,
  73. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  74. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  75. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  76. profilesSampleRate: 1.0`);
  77. }
  78. return dynamicParts;
  79. };
  80. const getSdkSetupSnippet = (params: Params) => {
  81. const config = buildSdkConfig({
  82. params,
  83. staticParts: [`dsn: "${params.dsn.public}"`],
  84. getIntegrations,
  85. getDynamicParts,
  86. });
  87. return `
  88. import "./app.css";
  89. import App from "./App.svelte";
  90. import * as Sentry from "@sentry/svelte";
  91. Sentry.init({
  92. ${config}
  93. });
  94. const app = new App({
  95. target: document.getElementById("app"),
  96. });
  97. export default app;
  98. `;
  99. };
  100. const getVerifySnippet = () => `
  101. // SomeComponent.svelte
  102. <button type="button" on:click="{() => {throw new Error("This is your first error!");}}">
  103. Break the world
  104. </button>`;
  105. const getInstallConfig = () => [
  106. {
  107. language: 'bash',
  108. code: [
  109. {
  110. label: 'npm',
  111. value: 'npm',
  112. language: 'bash',
  113. code: 'npm install --save @sentry/svelte',
  114. },
  115. {
  116. label: 'yarn',
  117. value: 'yarn',
  118. language: 'bash',
  119. code: 'yarn add @sentry/svelte',
  120. },
  121. ],
  122. },
  123. ];
  124. const onboarding: OnboardingConfig = {
  125. introduction: params => (
  126. <Fragment>
  127. <MaybeBrowserProfilingBetaWarning {...params} />
  128. <p>
  129. {tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  130. strong: <strong />,
  131. })}
  132. </p>
  133. </Fragment>
  134. ),
  135. install: () => [
  136. {
  137. type: StepType.INSTALL,
  138. description: tct(
  139. 'Add the Sentry SDK as a dependency using [code:npm] or [code:yarn]:',
  140. {
  141. code: <code />,
  142. }
  143. ),
  144. configurations: getInstallConfig(),
  145. },
  146. ],
  147. configure: (params: Params) => [
  148. {
  149. type: StepType.CONFIGURE,
  150. description: tct(
  151. "Initialize Sentry as early as possible in your application's lifecycle, usually your Svelte app's entry point ([code:main.ts/js]):",
  152. {code: <code />}
  153. ),
  154. configurations: [
  155. {
  156. code: [
  157. {
  158. label: 'JavaScript',
  159. value: 'javascript',
  160. language: 'javascript',
  161. code: getSdkSetupSnippet(params),
  162. },
  163. ],
  164. },
  165. ...(params.isProfilingSelected
  166. ? [getProfilingDocumentHeaderConfigurationStep()]
  167. : []),
  168. ],
  169. },
  170. getUploadSourceMapsStep({
  171. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/svelte/sourcemaps/',
  172. ...params,
  173. }),
  174. ],
  175. verify: () => [
  176. {
  177. type: StepType.VERIFY,
  178. description: t(
  179. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  180. ),
  181. configurations: [
  182. {
  183. code: [
  184. {
  185. label: 'JavaScript',
  186. value: 'javascript',
  187. language: 'javascript',
  188. code: getVerifySnippet(),
  189. },
  190. ],
  191. },
  192. ],
  193. },
  194. ],
  195. nextSteps: () => [
  196. {
  197. id: 'svelte-features',
  198. name: t('Svelte Features'),
  199. description: t(
  200. 'Learn about our first class integration with the Svelte framework.'
  201. ),
  202. link: 'https://docs.sentry.io/platforms/javascript/guides/svelte/features/',
  203. },
  204. ],
  205. };
  206. const replayOnboarding: OnboardingConfig = {
  207. install: () => [
  208. {
  209. type: StepType.INSTALL,
  210. description: tct(
  211. 'You need a minimum version 7.27.0 of [code:@sentry/svelte] in order to use Session Replay. You do not need to install any additional packages.',
  212. {
  213. code: <code />,
  214. }
  215. ),
  216. configurations: getInstallConfig(),
  217. },
  218. ],
  219. configure: (params: Params) => [
  220. {
  221. type: StepType.CONFIGURE,
  222. description: getReplayConfigureDescription({
  223. link: 'https://docs.sentry.io/platforms/javascript/guides/svelte/session-replay/',
  224. }),
  225. configurations: [
  226. {
  227. code: [
  228. {
  229. label: 'JavaScript',
  230. value: 'javascript',
  231. language: 'javascript',
  232. code: getSdkSetupSnippet(params),
  233. },
  234. ],
  235. additionalInfo: <TracePropagationMessage />,
  236. },
  237. ],
  238. },
  239. ],
  240. verify: getReplayVerifyStep(),
  241. nextSteps: () => [],
  242. };
  243. const feedbackOnboarding: OnboardingConfig = {
  244. install: () => [
  245. {
  246. type: StepType.INSTALL,
  247. description: tct(
  248. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/svelte]) installed, minimum version 7.85.0.',
  249. {
  250. code: <code />,
  251. }
  252. ),
  253. configurations: getInstallConfig(),
  254. },
  255. ],
  256. configure: (params: Params) => [
  257. {
  258. type: StepType.CONFIGURE,
  259. description: getFeedbackConfigureDescription({
  260. linkConfig:
  261. 'https://docs.sentry.io/platforms/javascript/guides/svelte/user-feedback/configuration/',
  262. linkButton:
  263. 'https://docs.sentry.io/platforms/javascript/guides/svelte/user-feedback/configuration/#bring-your-own-button',
  264. }),
  265. configurations: [
  266. {
  267. code: [
  268. {
  269. label: 'JavaScript',
  270. value: 'javascript',
  271. language: 'javascript',
  272. code: getSdkSetupSnippet(params),
  273. },
  274. ],
  275. },
  276. ],
  277. additionalInfo: crashReportCallout({
  278. link: 'https://docs.sentry.io/platforms/javascript/guides/svelte/user-feedback/#crash-report-modal',
  279. }),
  280. },
  281. ],
  282. verify: () => [],
  283. nextSteps: () => [],
  284. };
  285. const crashReportOnboarding: OnboardingConfig = {
  286. introduction: () => getCrashReportModalIntroduction(),
  287. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  288. configure: () => [
  289. {
  290. type: StepType.CONFIGURE,
  291. description: getCrashReportModalConfigDescription({
  292. link: 'https://docs.sentry.io/platforms/javascript/guides/svelte/user-feedback/configuration/#crash-report-modal',
  293. }),
  294. additionalInfo: widgetCallout({
  295. link: 'https://docs.sentry.io/platforms/javascript/guides/svelte/user-feedback/#user-feedback-widget',
  296. }),
  297. },
  298. ],
  299. verify: () => [],
  300. nextSteps: () => [],
  301. };
  302. const profilingOnboarding: OnboardingConfig = {
  303. ...onboarding,
  304. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  305. };
  306. const docs: Docs = {
  307. onboarding,
  308. feedbackOnboardingNpm: feedbackOnboarding,
  309. replayOnboarding,
  310. crashReportOnboarding,
  311. profilingOnboarding,
  312. featureFlagOnboarding,
  313. };
  314. export default docs;