fastapi.tsx 5.1 KB

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