asgi.tsx 4.0 KB

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