asgi.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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: () => [
  64. {
  65. type: StepType.INSTALL,
  66. description: tct('Install [code:sentry-sdk] from PyPI:', {
  67. code: <code />,
  68. }),
  69. configurations: [
  70. {
  71. language: 'bash',
  72. code: getInstallSnippet(),
  73. },
  74. ],
  75. },
  76. ],
  77. configure: (params: Params) => [
  78. {
  79. type: StepType.CONFIGURE,
  80. description: tct('Wrap your ASGI application with [code: SentryAsgiMiddleware]:', {
  81. code: <code />,
  82. }),
  83. configurations: [
  84. {
  85. language: 'python',
  86. code: getSdkSetupSnippet(params),
  87. },
  88. ],
  89. additionalInfo: t('The middleware supports both ASGI 2 and ASGI 3 transparently.'),
  90. },
  91. ],
  92. verify: () => [
  93. {
  94. type: StepType.VERIFY,
  95. description: t('To verify that everything is working trigger an error on purpose:'),
  96. configurations: [
  97. {
  98. language: 'python',
  99. code: getVerifySnippet(),
  100. },
  101. ],
  102. additionalInfo: (
  103. <span>
  104. <p>
  105. {tct(
  106. '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.',
  107. {
  108. code: <code />,
  109. link: <ExternalLink href="http://localhost:8000" />,
  110. }
  111. )}
  112. </p>
  113. <p>
  114. {t(
  115. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  116. )}
  117. </p>
  118. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  119. </span>
  120. ),
  121. },
  122. ],
  123. };
  124. const docs: Docs = {
  125. onboarding,
  126. customMetricsOnboarding: getPythonMetricsOnboarding({
  127. installSnippet: getInstallSnippet(),
  128. }),
  129. crashReportOnboarding: crashReportOnboardingPython,
  130. };
  131. export default docs;