fastapi.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  9. # of transactions for performance monitoring.
  10. traces_sample_rate=1.0,`;
  11. const profilingConfiguration = ` # Set profiles_sample_rate to 1.0 to profile 100%
  12. # of sampled transactions.
  13. # We recommend adjusting this value in production.
  14. profiles_sample_rate=1.0,`;
  15. const introduction = (
  16. <p>
  17. {tct('The FastAPI integration adds support for the [link:FastAPI Framework].', {
  18. link: <ExternalLink href="https://fastapi.tiangolo.com/" />,
  19. })}
  20. </p>
  21. );
  22. export const steps = ({
  23. sentryInitContent,
  24. }: {
  25. sentryInitContent: string;
  26. }): LayoutProps['steps'] => [
  27. {
  28. type: StepType.INSTALL,
  29. description: (
  30. <p>
  31. {tct(
  32. 'Install [sentrySdkCode:sentry-sdk] from PyPI with the [sentryFastApiCode:fastapi] extra:',
  33. {
  34. sentrySdkCode: <code />,
  35. sentryFastApiCode: <code />,
  36. }
  37. )}
  38. </p>
  39. ),
  40. configurations: [
  41. {
  42. language: 'bash',
  43. code: "pip install --upgrade 'sentry-sdk[fastapi]'",
  44. },
  45. ],
  46. },
  47. {
  48. type: StepType.CONFIGURE,
  49. description: (
  50. <p>
  51. {tct(
  52. '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:',
  53. {
  54. codeFastAPI: <code />,
  55. }
  56. )}
  57. </p>
  58. ),
  59. configurations: [
  60. {
  61. language: 'python',
  62. code: `
  63. from fastapi import FastAPI
  64. import sentry_sdk
  65. sentry_sdk.init(
  66. ${sentryInitContent}
  67. )
  68. app = FastAPI()
  69. `,
  70. },
  71. ],
  72. additionalInfo: (
  73. <p>
  74. {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. </p>
  81. ),
  82. },
  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. from fastapi import FastAPI
  93. import sentry_sdk
  94. sentry_sdk.init(
  95. ${sentryInitContent}
  96. )
  97. app = FastAPI()
  98. @app.get("/sentry-debug")
  99. async def trigger_error():
  100. division_by_zero = 1 / 0
  101. `,
  102. },
  103. ],
  104. additionalInfo: (
  105. <div>
  106. <p>
  107. {tct(
  108. 'When you point your browser to [link:http://localhost:8000/sentry-debug/] a transaction in the Performance section of Sentry will be created.',
  109. {
  110. link: <ExternalLink href="http://localhost:8000/sentry-debug/" />,
  111. }
  112. )}
  113. </p>
  114. <p>
  115. {t(
  116. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  117. )}
  118. </p>
  119. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  120. </div>
  121. ),
  122. },
  123. ];
  124. // Configuration End
  125. export function GettingStartedWithFastApi({
  126. dsn,
  127. activeProductSelection = [],
  128. ...props
  129. }: ModuleProps) {
  130. const otherConfigs: string[] = [];
  131. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  132. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  133. otherConfigs.push(performanceConfiguration);
  134. }
  135. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  136. otherConfigs.push(profilingConfiguration);
  137. }
  138. sentryInitContent = sentryInitContent.concat(otherConfigs);
  139. return (
  140. <Layout
  141. introduction={introduction}
  142. steps={steps({
  143. sentryInitContent: sentryInitContent.join('\n'),
  144. })}
  145. {...props}
  146. />
  147. );
  148. }
  149. export default GettingStartedWithFastApi;