starlette.tsx 4.8 KB

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