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