fastapi.tsx 4.0 KB

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