starlette.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = tct(
  17. 'The Starlette integration adds support for the Starlette Framework.',
  18. {
  19. link: <ExternalLink href="https://www.starlette.io/" />,
  20. }
  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 [sentryStarletteCode:starlette] extra:',
  33. {
  34. sentrySdkCode: <code />,
  35. sentryStarletteCode: <code />,
  36. }
  37. )}
  38. </p>
  39. ),
  40. configurations: [
  41. {
  42. language: 'bash',
  43. code: "pip install --upgrade 'sentry-sdk[starlette]'",
  44. },
  45. ],
  46. },
  47. {
  48. type: StepType.CONFIGURE,
  49. description: (
  50. <p>
  51. {tct(
  52. 'To configure the SDK, initialize it before your app has been initialized. The Sentry SDK automatically enables support for Starlette if you have the [code:starlette] Python package installed in your project. There are no configuration options you need to add when initializing the Sentry SDK as everything works out of the box:',
  53. {code: <code />}
  54. )}
  55. </p>
  56. ),
  57. configurations: [
  58. {
  59. language: 'python',
  60. code: `
  61. from starlette.applications import Starlette
  62. import sentry_sdk
  63. sentry_sdk.init(
  64. ${sentryInitContent}
  65. )
  66. app = Starlette(routes=[...])
  67. `,
  68. },
  69. ],
  70. additionalInfo: (
  71. <p>
  72. {tct(
  73. '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.',
  74. {
  75. code: <code />,
  76. }
  77. )}
  78. </p>
  79. ),
  80. },
  81. {
  82. type: StepType.VERIFY,
  83. description: t(
  84. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  85. ),
  86. configurations: [
  87. {
  88. language: 'python',
  89. code: `
  90. from starlette.applications import Starlette
  91. from starlette.routing import Route
  92. async def trigger_error(request):
  93. division_by_zero = 1 / 0
  94. app = Starlette(routes=[
  95. Route("/sentry-debug", trigger_error),
  96. ])
  97. `,
  98. additionalInfo: t(
  99. 'Visiting this route will trigger an error that will be captured by Sentry.'
  100. ),
  101. },
  102. ],
  103. },
  104. ];
  105. // Configuration End
  106. export function GettingStartedWithStarlette({
  107. dsn,
  108. activeProductSelection = [],
  109. ...props
  110. }: ModuleProps) {
  111. const otherConfigs: string[] = [];
  112. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  113. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  114. otherConfigs.push(performanceConfiguration);
  115. }
  116. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  117. otherConfigs.push(profilingConfiguration);
  118. }
  119. sentryInitContent = sentryInitContent.concat(otherConfigs);
  120. return (
  121. <Layout
  122. introduction={introduction}
  123. steps={steps({
  124. sentryInitContent: sentryInitContent.join('\n'),
  125. })}
  126. {...props}
  127. />
  128. );
  129. }
  130. export default GettingStartedWithStarlette;