fastapi.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  9. import {t, tct} from 'sentry/locale';
  10. type Params = DocsParams;
  11. const getSdkSetupSnippet = (params: Params) => `
  12. from fastapi import FastAPI
  13. import sentry_sdk
  14. sentry_sdk.init(
  15. dsn="${params.dsn}",${
  16. params.isPerformanceSelected
  17. ? `
  18. # Set traces_sample_rate to 1.0 to capture 100%
  19. # of transactions for performance monitoring.
  20. traces_sample_rate=1.0,`
  21. : ''
  22. }${
  23. params.isProfilingSelected
  24. ? `
  25. # Set profiles_sample_rate to 1.0 to profile 100%
  26. # of sampled transactions.
  27. # We recommend adjusting this value in production.
  28. profiles_sample_rate=1.0,`
  29. : ''
  30. }
  31. )
  32. `;
  33. const onboarding: OnboardingConfig = {
  34. introduction: () =>
  35. tct('The FastAPI integration adds support for the [link:FastAPI Framework].', {
  36. link: <ExternalLink href="https://fastapi.tiangolo.com/" />,
  37. }),
  38. install: () => [
  39. {
  40. type: StepType.INSTALL,
  41. description: tct(
  42. 'Install [sentrySdkCode:sentry-sdk] from PyPI with the [sentryFastApiCode:fastapi] extra:',
  43. {
  44. sentrySdkCode: <code />,
  45. sentryFastApiCode: <code />,
  46. }
  47. ),
  48. configurations: [
  49. {
  50. language: 'bash',
  51. code: "pip install --upgrade 'sentry-sdk[fastapi]'",
  52. },
  53. ],
  54. },
  55. ],
  56. configure: (params: Params) => [
  57. {
  58. type: StepType.CONFIGURE,
  59. description: tct(
  60. '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:',
  61. {
  62. codeFastAPI: <code />,
  63. }
  64. ),
  65. configurations: [
  66. {
  67. language: 'python',
  68. code: `
  69. ${getSdkSetupSnippet(params)}
  70. app = FastAPI()
  71. `,
  72. },
  73. ],
  74. additionalInfo: tct(
  75. '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.',
  76. {
  77. code: <code />,
  78. }
  79. ),
  80. },
  81. ],
  82. verify: (params: Params) => [
  83. {
  84. type: StepType.VERIFY,
  85. description: t(
  86. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  87. ),
  88. configurations: [
  89. {
  90. language: 'python',
  91. code: `
  92. ${getSdkSetupSnippet(params)}
  93. app = FastAPI()
  94. @app.get("/sentry-debug")
  95. async def trigger_error():
  96. division_by_zero = 1 / 0
  97. `,
  98. },
  99. ],
  100. additionalInfo: (
  101. <div>
  102. <p>
  103. {tct(
  104. 'When you point your browser to [link:http://localhost:8000/sentry-debug/] a transaction in the Performance section of Sentry will be created.',
  105. {
  106. link: <ExternalLink href="http://localhost:8000/sentry-debug/" />,
  107. }
  108. )}
  109. </p>
  110. <p>
  111. {t(
  112. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  113. )}
  114. </p>
  115. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  116. </div>
  117. ),
  118. },
  119. ],
  120. nextSteps: () => [],
  121. };
  122. const docs: Docs = {
  123. onboarding,
  124. replayOnboardingJsLoader,
  125. };
  126. export default docs;