asgi.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  9. import {crashReportOnboardingPython} from 'sentry/gettingStartedDocs/python/python';
  10. import {t, tct} from 'sentry/locale';
  11. type Params = DocsParams;
  12. const getInstallSnippet = () => `pip install --upgrade sentry-sdk`;
  13. const getSdkSetupSnippet = (params: Params) => `
  14. import sentry_sdk
  15. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  16. from myapp import asgi_app
  17. sentry_sdk.init(
  18. dsn="${params.dsn}",${
  19. params.isPerformanceSelected
  20. ? `
  21. # Set traces_sample_rate to 1.0 to capture 100%
  22. # of transactions for performance monitoring.
  23. traces_sample_rate=1.0,`
  24. : ''
  25. }${
  26. params.isProfilingSelected
  27. ? `
  28. # Set profiles_sample_rate to 1.0 to profile 100%
  29. # of sampled transactions.
  30. # We recommend adjusting this value in production.
  31. profiles_sample_rate=1.0,`
  32. : ''
  33. }
  34. )
  35. asgi_app = SentryAsgiMiddleware(asgi_app)`;
  36. const getVerifySnippet = () => `
  37. import sentry_sdk
  38. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  39. from myapp import asgi_app
  40. sentry_sdk.init(...) # same as above
  41. def app(scope):
  42. async def get_body():
  43. return f"The number is: {1/0}" # raises an error!
  44. async def asgi(receive, send):
  45. await send(
  46. {
  47. "type": "http.response.start",
  48. "status": 200,
  49. "headers": [[b"content-type", b"text/plain"]],
  50. }
  51. )
  52. await send({"type": "http.response.body", "body": await get_body()})
  53. return asgi
  54. app = SentryAsgiMiddleware(app)`;
  55. const onboarding: OnboardingConfig = {
  56. introduction: () =>
  57. tct(
  58. '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.',
  59. {
  60. link: <ExternalLink href="https://asgi.readthedocs.io/en/latest/" />,
  61. }
  62. ),
  63. install: (params: Params) => [
  64. {
  65. type: StepType.INSTALL,
  66. description: tct('Install [code:sentry-sdk] from PyPI:', {
  67. code: <code />,
  68. }),
  69. configurations: [
  70. {
  71. description: params.isProfilingSelected
  72. ? tct(
  73. 'You need a minimum version [codeVersion:1.18.0] of the [codePackage:sentry-python] SDK for the profiling feature.',
  74. {
  75. codeVersion: <code />,
  76. codePackage: <code />,
  77. }
  78. )
  79. : undefined,
  80. language: 'bash',
  81. code: getInstallSnippet(),
  82. },
  83. ],
  84. },
  85. ],
  86. configure: (params: Params) => [
  87. {
  88. type: StepType.CONFIGURE,
  89. description: tct('Wrap your ASGI application with [code: SentryAsgiMiddleware]:', {
  90. code: <code />,
  91. }),
  92. configurations: [
  93. {
  94. language: 'python',
  95. code: getSdkSetupSnippet(params),
  96. },
  97. ],
  98. additionalInfo: t('The middleware supports both ASGI 2 and ASGI 3 transparently.'),
  99. },
  100. ],
  101. verify: () => [
  102. {
  103. type: StepType.VERIFY,
  104. description: t('To verify that everything is working trigger an error on purpose:'),
  105. configurations: [
  106. {
  107. language: 'python',
  108. code: getVerifySnippet(),
  109. },
  110. ],
  111. additionalInfo: (
  112. <span>
  113. <p>
  114. {tct(
  115. '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.',
  116. {
  117. code: <code />,
  118. link: <ExternalLink href="http://localhost:8000" />,
  119. }
  120. )}
  121. </p>
  122. <p>
  123. {t(
  124. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  125. )}
  126. </p>
  127. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  128. </span>
  129. ),
  130. },
  131. ],
  132. };
  133. const docs: Docs = {
  134. onboarding,
  135. customMetricsOnboarding: getPythonMetricsOnboarding({
  136. installSnippet: getInstallSnippet(),
  137. }),
  138. crashReportOnboarding: crashReportOnboardingPython,
  139. };
  140. export default docs;