fastapi.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {
  5. type Docs,
  6. DocsPageLocation,
  7. type DocsParams,
  8. type OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  11. import {
  12. feedbackOnboardingJsLoader,
  13. replayOnboardingJsLoader,
  14. } from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  15. import {
  16. AlternativeConfiguration,
  17. crashReportOnboardingPython,
  18. featureFlagOnboarding,
  19. } from 'sentry/gettingStartedDocs/python/python';
  20. import {t, tct} from 'sentry/locale';
  21. type Params = DocsParams;
  22. const getInstallSnippet = () => `pip install --upgrade 'sentry-sdk[fastapi]'`;
  23. const getSdkSetupSnippet = (params: Params) => `
  24. from fastapi import FastAPI
  25. import sentry_sdk
  26. sentry_sdk.init(
  27. dsn="${params.dsn.public}",${
  28. params.isPerformanceSelected
  29. ? `
  30. # Set traces_sample_rate to 1.0 to capture 100%
  31. # of transactions for tracing.
  32. traces_sample_rate=1.0,`
  33. : ''
  34. }${
  35. params.isProfilingSelected &&
  36. params.profilingOptions?.defaultProfilingMode !== 'continuous'
  37. ? `
  38. # Set profiles_sample_rate to 1.0 to profile 100%
  39. # of sampled transactions.
  40. # We recommend adjusting this value in production.
  41. profiles_sample_rate=1.0,`
  42. : params.isProfilingSelected &&
  43. params.profilingOptions?.defaultProfilingMode === 'continuous'
  44. ? `
  45. _experiments={
  46. # Set continuous_profiling_auto_start to True
  47. # to automatically start the profiler on when
  48. # possible.
  49. "continuous_profiling_auto_start": True,
  50. },`
  51. : ''
  52. }
  53. )
  54. `;
  55. const onboarding: OnboardingConfig = {
  56. introduction: () =>
  57. tct('The FastAPI integration adds support for the [link:FastAPI Framework].', {
  58. link: <ExternalLink href="https://fastapi.tiangolo.com/" />,
  59. }),
  60. install: (params: Params) => [
  61. {
  62. type: StepType.INSTALL,
  63. description: tct(
  64. 'Install [code:sentry-sdk] from PyPI with the [code:fastapi] extra:',
  65. {
  66. code: <code />,
  67. }
  68. ),
  69. configurations: [
  70. {
  71. description:
  72. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  73. ? tct(
  74. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  75. {
  76. code: <code />,
  77. }
  78. )
  79. : undefined,
  80. language: 'bash',
  81. code: getInstallSnippet(),
  82. },
  83. ],
  84. },
  85. ],
  86. configure: (params: Params) => [
  87. {
  88. type: StepType.CONFIGURE,
  89. description: tct(
  90. 'If you have the [codeFastAPI:fastapi] package in your dependencies, the FastAPI integration will be enabled automatically when you initialize the Sentry SDK. Initialize the Sentry SDK before your app has been initialized:',
  91. {
  92. codeFastAPI: <code />,
  93. }
  94. ),
  95. configurations: [
  96. {
  97. language: 'python',
  98. code: `
  99. ${getSdkSetupSnippet(params)}
  100. app = FastAPI()
  101. `,
  102. },
  103. ],
  104. additionalInfo: (
  105. <Fragment>
  106. {params.isProfilingSelected &&
  107. params.profilingOptions?.defaultProfilingMode === 'continuous' && (
  108. <Fragment>
  109. <AlternativeConfiguration />
  110. <br />
  111. </Fragment>
  112. )}
  113. {tct(
  114. 'The above configuration captures both error and performance data. To reduce the volume of performance data captured, change [code:traces_sample_rate] to a value between 0 and 1.',
  115. {
  116. code: <code />,
  117. }
  118. )}
  119. ,
  120. </Fragment>
  121. ),
  122. },
  123. ],
  124. verify: () => [
  125. {
  126. type: StepType.VERIFY,
  127. description: t(
  128. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  129. ),
  130. configurations: [
  131. {
  132. language: 'python',
  133. code: `
  134. @app.get("/sentry-debug")
  135. async def trigger_error():
  136. division_by_zero = 1 / 0
  137. `,
  138. },
  139. ],
  140. additionalInfo: (
  141. <div>
  142. <p>
  143. {tct(
  144. 'When you open [link:http://localhost:8000/sentry-debug/] with your browser, a transaction in the Performance section of Sentry will be created.',
  145. {
  146. link: <ExternalLink href="http://localhost:8000/sentry-debug/" />,
  147. }
  148. )}
  149. </p>
  150. <p>
  151. {t(
  152. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  153. )}
  154. </p>
  155. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  156. </div>
  157. ),
  158. },
  159. ],
  160. nextSteps: () => [],
  161. };
  162. const docs: Docs = {
  163. onboarding,
  164. replayOnboardingJsLoader,
  165. customMetricsOnboarding: getPythonMetricsOnboarding({
  166. installSnippet: getInstallSnippet(),
  167. }),
  168. crashReportOnboarding: crashReportOnboardingPython,
  169. featureFlagOnboarding,
  170. feedbackOnboardingJsLoader,
  171. };
  172. export default docs;