starlette.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import {
  4. type Docs,
  5. DocsPageLocation,
  6. type DocsParams,
  7. type OnboardingConfig,
  8. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  9. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  10. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  11. import {
  12. AlternativeConfiguration,
  13. crashReportOnboardingPython,
  14. featureFlagOnboarding,
  15. } from 'sentry/gettingStartedDocs/python/python';
  16. import {t, tct} from 'sentry/locale';
  17. type Params = DocsParams;
  18. const getInstallSnippet = () => `pip install --upgrade 'sentry-sdk[starlette]'`;
  19. const getSdkSetupSnippet = (params: Params) => `
  20. from starlette.applications import Starlette
  21. import sentry_sdk
  22. sentry_sdk.init(
  23. dsn="${params.dsn.public}",${
  24. params.isPerformanceSelected
  25. ? `
  26. # Set traces_sample_rate to 1.0 to capture 100%
  27. # of transactions for tracing.
  28. traces_sample_rate=1.0,`
  29. : ''
  30. }${
  31. params.isProfilingSelected &&
  32. params.profilingOptions?.defaultProfilingMode !== 'continuous'
  33. ? `
  34. # Set profiles_sample_rate to 1.0 to profile 100%
  35. # of sampled transactions.
  36. # We recommend adjusting this value in production.
  37. profiles_sample_rate=1.0,`
  38. : params.isProfilingSelected &&
  39. params.profilingOptions?.defaultProfilingMode === 'continuous'
  40. ? `
  41. _experiments={
  42. # Set continuous_profiling_auto_start to True
  43. # to automatically start the profiler on when
  44. # possible.
  45. "continuous_profiling_auto_start": True,
  46. },`
  47. : ''
  48. }
  49. )
  50. `;
  51. const onboarding: OnboardingConfig = {
  52. introduction: () =>
  53. tct('The Starlette integration adds support for the Starlette Framework.', {
  54. link: <ExternalLink href="https://www.starlette.io/" />,
  55. }),
  56. install: (params: Params) => [
  57. {
  58. type: StepType.INSTALL,
  59. description: tct(
  60. 'Install [code:sentry-sdk] from PyPI with the [code:starlette] extra:',
  61. {
  62. code: <code />,
  63. }
  64. ),
  65. configurations: [
  66. {
  67. description:
  68. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  69. ? tct(
  70. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  71. {
  72. code: <code />,
  73. }
  74. )
  75. : undefined,
  76. language: 'bash',
  77. code: getInstallSnippet(),
  78. },
  79. ],
  80. },
  81. ],
  82. configure: (params: Params) => [
  83. {
  84. type: StepType.CONFIGURE,
  85. description: tct(
  86. 'If you have the [codeStarlette:starlette] package in your dependencies, the Starlette integration will be enabled automatically when you initialize the Sentry SDK. Initialize the Sentry SDK before your app has been initialized:',
  87. {
  88. codeStarlette: <code />,
  89. }
  90. ),
  91. configurations: [
  92. {
  93. language: 'python',
  94. code: `
  95. ${getSdkSetupSnippet(params)}
  96. app = Starlette(routes=[...])
  97. `,
  98. },
  99. ],
  100. additionalInfo: <AlternativeConfiguration />,
  101. },
  102. ],
  103. verify: (params: Params) => [
  104. {
  105. type: StepType.VERIFY,
  106. description: t(
  107. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  108. ),
  109. configurations: [
  110. {
  111. language: 'python',
  112. code: `
  113. from starlette.routing import Route
  114. ${getSdkSetupSnippet(params)}
  115. async def trigger_error(request):
  116. division_by_zero = 1 / 0
  117. app = Starlette(routes=[
  118. Route("/sentry-debug", trigger_error),
  119. ])
  120. `,
  121. },
  122. ],
  123. additionalInfo: (
  124. <div>
  125. <p>
  126. {tct(
  127. 'When you point your browser to [link:http://localhost:8000/sentry-debug/] a transaction in the Performance section of Sentry will be created.',
  128. {
  129. link: <ExternalLink href="http://localhost:8000/sentry-debug/" />,
  130. }
  131. )}
  132. </p>
  133. <p>
  134. {t(
  135. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  136. )}
  137. </p>
  138. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  139. </div>
  140. ),
  141. },
  142. ],
  143. nextSteps: () => [],
  144. };
  145. const docs: Docs = {
  146. onboarding,
  147. replayOnboardingJsLoader,
  148. customMetricsOnboarding: getPythonMetricsOnboarding({
  149. installSnippet: getInstallSnippet(),
  150. }),
  151. crashReportOnboarding: crashReportOnboardingPython,
  152. featureFlagOnboarding: featureFlagOnboarding,
  153. };
  154. export default docs;