asgi.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {
  5. type Docs,
  6. DocsPageLocation,
  7. type DocsParams,
  8. type OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  11. import {
  12. AlternativeConfiguration,
  13. crashReportOnboardingPython,
  14. } from 'sentry/gettingStartedDocs/python/python';
  15. import {t, tct} from 'sentry/locale';
  16. type Params = DocsParams;
  17. const getInstallSnippet = () => `pip install --upgrade sentry-sdk`;
  18. const getSdkSetupSnippet = (params: Params) => `
  19. import sentry_sdk
  20. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  21. from myapp import asgi_app
  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. asgi_app = SentryAsgiMiddleware(asgi_app)`;
  51. const getVerifySnippet = () => `
  52. import sentry_sdk
  53. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  54. from myapp import asgi_app
  55. sentry_sdk.init(...) # same as above
  56. def app(scope):
  57. async def get_body():
  58. return f"The number is: {1/0}" # raises an error!
  59. async def asgi(receive, send):
  60. await send(
  61. {
  62. "type": "http.response.start",
  63. "status": 200,
  64. "headers": [[b"content-type", b"text/plain"]],
  65. }
  66. )
  67. await send({"type": "http.response.body", "body": await get_body()})
  68. return asgi
  69. app = SentryAsgiMiddleware(app)`;
  70. const onboarding: OnboardingConfig = {
  71. introduction: () =>
  72. tct(
  73. 'The ASGI middleware can be used to instrument any bare bones ASGI application. If you have a ASGI based web framework (like FastAPI, Starlette, or others), please use the specific integration for the framework.',
  74. {
  75. link: <ExternalLink href="https://asgi.readthedocs.io/en/latest/" />,
  76. }
  77. ),
  78. install: (params: Params) => [
  79. {
  80. type: StepType.INSTALL,
  81. description: tct('Install [code:sentry-sdk] from PyPI:', {
  82. code: <code />,
  83. }),
  84. configurations: [
  85. {
  86. description:
  87. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  88. ? tct(
  89. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  90. {
  91. code: <code />,
  92. }
  93. )
  94. : undefined,
  95. language: 'bash',
  96. code: getInstallSnippet(),
  97. },
  98. ],
  99. },
  100. ],
  101. configure: (params: Params) => [
  102. {
  103. type: StepType.CONFIGURE,
  104. description: tct('Wrap your ASGI application with [code: SentryAsgiMiddleware]:', {
  105. code: <code />,
  106. }),
  107. configurations: [
  108. {
  109. language: 'python',
  110. code: getSdkSetupSnippet(params),
  111. },
  112. ],
  113. additionalInfo: (
  114. <Fragment>
  115. {params.isProfilingSelected &&
  116. params.profilingOptions?.defaultProfilingMode === 'continuous' && (
  117. <Fragment>
  118. <AlternativeConfiguration />
  119. <br />
  120. </Fragment>
  121. )}
  122. {t('The middleware supports both ASGI 2 and ASGI 3 transparently.')}
  123. </Fragment>
  124. ),
  125. },
  126. ],
  127. verify: () => [
  128. {
  129. type: StepType.VERIFY,
  130. description: t('To verify that everything is working trigger an error on purpose:'),
  131. configurations: [
  132. {
  133. language: 'python',
  134. code: getVerifySnippet(),
  135. },
  136. ],
  137. additionalInfo: (
  138. <span>
  139. <p>
  140. {tct(
  141. 'Run your ASGI app with uvicorn ([code:uvicorn main:app --port 8000]) and point your browser to [link:http://localhost:8000]. A transaction in the Performance section of Sentry will be created.',
  142. {
  143. code: <code />,
  144. link: <ExternalLink href="http://localhost:8000" />,
  145. }
  146. )}
  147. </p>
  148. <p>
  149. {t(
  150. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  151. )}
  152. </p>
  153. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  154. </span>
  155. ),
  156. },
  157. ],
  158. };
  159. const docs: Docs = {
  160. onboarding,
  161. customMetricsOnboarding: getPythonMetricsOnboarding({
  162. installSnippet: getInstallSnippet(),
  163. }),
  164. crashReportOnboarding: crashReportOnboardingPython,
  165. };
  166. export default docs;