gatsby.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 * as Sentry from "@sentry/gatsby";
  89. Sentry.init({
  90. ${config}
  91. });
  92. const container = document.getElementById(“app”);
  93. const root = createRoot(container);
  94. root.render(<App />);
  95. `;
  96. };
  97. const getVerifySnippet = () => `
  98. myUndefinedFunction();`;
  99. const getConfigureStep = (params: Params) => {
  100. return {
  101. type: StepType.CONFIGURE,
  102. configurations: [
  103. {
  104. description: tct(
  105. 'Register the [code:Sentry@sentry/gatsby] plugin in your Gatsby configuration file (typically [code:gatsby-config.js]).',
  106. {code: <code />}
  107. ),
  108. code: [
  109. {
  110. label: 'JavaScript',
  111. value: 'javascript',
  112. language: 'javascript',
  113. code: `module.exports = {
  114. plugins: [{
  115. resolve: "@sentry/gatsby",
  116. }],
  117. };`,
  118. },
  119. ],
  120. },
  121. {
  122. description: tct(
  123. 'Then, configure your [codeSentry:Sentry.init:]. For this, create a new file called [codeSentry:sentry.config.js] in the root of your project and add the following code:',
  124. {codeSentry: <code />}
  125. ),
  126. code: [
  127. {
  128. label: 'JavaScript',
  129. value: 'javascript',
  130. language: 'javascript',
  131. filename: 'sentry.config.js',
  132. code: getSdkSetupSnippet(params),
  133. },
  134. ],
  135. },
  136. ...(params.isProfilingSelected
  137. ? [getProfilingDocumentHeaderConfigurationStep()]
  138. : []),
  139. ],
  140. };
  141. };
  142. const getInstallConfig = () => [
  143. {
  144. language: 'bash',
  145. code: [
  146. {
  147. label: 'npm',
  148. value: 'npm',
  149. language: 'bash',
  150. code: 'npm install --save @sentry/gatsby',
  151. },
  152. {label: 'yarn', value: 'yarn', language: 'bash', code: 'yarn add @sentry/gatsby'},
  153. ],
  154. },
  155. ];
  156. const onboarding: OnboardingConfig = {
  157. introduction: params => (
  158. <Fragment>
  159. <MaybeBrowserProfilingBetaWarning {...params} />
  160. <p>
  161. {tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  162. strong: <strong />,
  163. })}
  164. </p>
  165. </Fragment>
  166. ),
  167. install: () => [
  168. {
  169. type: StepType.INSTALL,
  170. description: tct(
  171. 'Add the Sentry SDK as a dependency using [code:npm] or [code:yarn]:',
  172. {code: <code />}
  173. ),
  174. configurations: getInstallConfig(),
  175. },
  176. ],
  177. configure: (params: Params) => [
  178. getConfigureStep(params),
  179. getUploadSourceMapsStep({
  180. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/gatsby/sourcemaps/',
  181. ...params,
  182. }),
  183. ],
  184. verify: () => [
  185. {
  186. type: StepType.VERIFY,
  187. description: t(
  188. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  189. ),
  190. configurations: [
  191. {
  192. code: [
  193. {
  194. label: 'JavaScript',
  195. value: 'javascript',
  196. language: 'javascript',
  197. code: getVerifySnippet(),
  198. },
  199. ],
  200. },
  201. ],
  202. },
  203. ],
  204. nextSteps: () => [],
  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/gatsby] in order to use Session Replay. You do not need to install any additional packages.',
  212. {code: <code />}
  213. ),
  214. configurations: getInstallConfig(),
  215. },
  216. ],
  217. configure: (params: Params) => [
  218. {
  219. type: StepType.CONFIGURE,
  220. description: getReplayConfigureDescription({
  221. link: 'https://docs.sentry.io/platforms/javascript/guides/gatsby/session-replay/',
  222. }),
  223. configurations: [getConfigureStep(params)],
  224. additionalInfo: (
  225. <Fragment>
  226. <TracePropagationMessage />
  227. {tct(
  228. 'Note: If [code:gatsby-config.js] has any settings for the [code:@sentry/gatsby] plugin, they need to be moved into [code:sentry.config.js]. The [code:gatsby-config.js] file does not support non-serializable options, like [code:new Replay()].',
  229. {code: <code />}
  230. )}
  231. </Fragment>
  232. ),
  233. },
  234. ],
  235. verify: getReplayVerifyStep(),
  236. nextSteps: () => [],
  237. };
  238. const feedbackOnboarding: OnboardingConfig = {
  239. install: () => [
  240. {
  241. type: StepType.INSTALL,
  242. description: tct(
  243. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/gatsby]) installed, minimum version 7.85.0.',
  244. {code: <code />}
  245. ),
  246. configurations: getInstallConfig(),
  247. },
  248. ],
  249. configure: (params: Params) => [
  250. {
  251. type: StepType.CONFIGURE,
  252. description: getFeedbackConfigureDescription({
  253. linkConfig:
  254. 'https://docs.sentry.io/platforms/javascript/guides/gatsby/user-feedback/configuration/',
  255. linkButton:
  256. 'https://docs.sentry.io/platforms/javascript/guides/gatsby/user-feedback/configuration/#bring-your-own-button',
  257. }),
  258. configurations: [
  259. {
  260. code: [
  261. {
  262. label: 'JavaScript',
  263. value: 'javascript',
  264. language: 'javascript',
  265. code: getSdkSetupSnippet(params),
  266. },
  267. ],
  268. },
  269. ],
  270. additionalInfo: crashReportCallout({
  271. link: 'https://docs.sentry.io/platforms/javascript/guides/gatsby/user-feedback/#crash-report-modal',
  272. }),
  273. },
  274. ],
  275. verify: () => [],
  276. nextSteps: () => [],
  277. };
  278. const crashReportOnboarding: OnboardingConfig = {
  279. introduction: () => getCrashReportModalIntroduction(),
  280. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  281. configure: () => [
  282. {
  283. type: StepType.CONFIGURE,
  284. description: getCrashReportModalConfigDescription({
  285. link: 'https://docs.sentry.io/platforms/javascript/guides/gatsby/user-feedback/configuration/#crash-report-modal',
  286. }),
  287. additionalInfo: widgetCallout({
  288. link: 'https://docs.sentry.io/platforms/javascript/guides/gatsby/user-feedback/#user-feedback-widget',
  289. }),
  290. },
  291. ],
  292. verify: () => [],
  293. nextSteps: () => [],
  294. };
  295. const profilingOnboarding: OnboardingConfig = {
  296. ...onboarding,
  297. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  298. };
  299. const docs: Docs = {
  300. onboarding,
  301. feedbackOnboardingNpm: feedbackOnboarding,
  302. replayOnboarding,
  303. crashReportOnboarding,
  304. profilingOnboarding,
  305. featureFlagOnboarding,
  306. };
  307. export default docs;