fastapi.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  6. import {t, tct} from 'sentry/locale';
  7. // Configuration Start
  8. const profilingConfiguration = ` # Set profiles_sample_rate to 1.0 to profile 100%
  9. # of sampled transactions.
  10. # We recommend adjusting this value in production.
  11. profiles_sample_rate=1.0,`;
  12. const performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  13. # of transactions for performance monitoring.
  14. # We recommend adjusting this value in production.
  15. traces_sample_rate=1.0,`;
  16. const introduction = (
  17. <p>
  18. {tct('The FastAPI integration adds support for the [link:FastAPI Framework].', {
  19. link: <ExternalLink href="https://fastapi.tiangolo.com/" />,
  20. })}
  21. </p>
  22. );
  23. export const steps = ({
  24. sentryInitContent,
  25. }: {
  26. sentryInitContent: string;
  27. }): LayoutProps['steps'] => [
  28. {
  29. type: StepType.INSTALL,
  30. description: (
  31. <p>
  32. {tct(
  33. 'Install [sentrySdkCode:sentry-sdk] from PyPI with the [sentryFastApiCode:fastapi] extra:',
  34. {
  35. sentrySdkCode: <code />,
  36. sentryFastApiCode: <code />,
  37. }
  38. )}
  39. </p>
  40. ),
  41. configurations: [
  42. {
  43. language: 'bash',
  44. code: "pip install --upgrade 'sentry-sdk[fastapi]'",
  45. },
  46. ],
  47. },
  48. {
  49. type: StepType.CONFIGURE,
  50. description: t(
  51. 'To configure the Sentry SDK, initialize it before your app has been initialized:'
  52. ),
  53. configurations: [
  54. {
  55. language: 'python',
  56. code: `
  57. from fastapi import FastAPI
  58. import sentry_sdk
  59. sentry_sdk.init(
  60. ${sentryInitContent}
  61. )
  62. app = FastAPI()
  63. `,
  64. },
  65. ],
  66. additionalInfo: (
  67. <p>
  68. {tct(
  69. '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.',
  70. {
  71. code: <code />,
  72. }
  73. )}
  74. </p>
  75. ),
  76. },
  77. {
  78. type: StepType.VERIFY,
  79. description: t(
  80. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  81. ),
  82. configurations: [
  83. {
  84. language: 'python',
  85. code: `
  86. from fastapi import FastAPI
  87. app = FastAPI()
  88. @app.get("/sentry-debug")
  89. async def trigger_error():
  90. division_by_zero = 1 / 0
  91. `,
  92. },
  93. ],
  94. additionalInfo: t(
  95. 'Visiting this route will trigger an error that will be captured by Sentry.'
  96. ),
  97. },
  98. ];
  99. // Configuration End
  100. export function GettingStartedWithFastApi({
  101. dsn,
  102. activeProductSelection = [],
  103. ...props
  104. }: ModuleProps) {
  105. const otherConfigs: string[] = [];
  106. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  107. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  108. otherConfigs.push(performanceConfiguration);
  109. }
  110. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  111. otherConfigs.push(profilingConfiguration);
  112. }
  113. sentryInitContent = sentryInitContent.concat(otherConfigs);
  114. return (
  115. <Layout
  116. introduction={introduction}
  117. steps={steps({
  118. sentryInitContent: sentryInitContent.join('\n'),
  119. })}
  120. {...props}
  121. />
  122. );
  123. }
  124. export default GettingStartedWithFastApi;