asgi.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  6. import {t, tct} from 'sentry/locale';
  7. // Configuration Start
  8. const performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  9. # of transactions for performance monitoring.
  10. traces_sample_rate=1.0,`;
  11. const introduction = (
  12. <p>
  13. {tct(
  14. '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.',
  15. {
  16. link: <ExternalLink href="https://asgi.readthedocs.io/en/latest/" />,
  17. }
  18. )}
  19. </p>
  20. );
  21. export const steps = ({
  22. sentryInitContent,
  23. }: {
  24. sentryInitContent: string;
  25. }): LayoutProps['steps'] => [
  26. {
  27. type: StepType.INSTALL,
  28. description: (
  29. <p>
  30. {tct('Install [code:sentry-sdk] from PyPI:', {
  31. code: <code />,
  32. })}
  33. </p>
  34. ),
  35. configurations: [
  36. {
  37. language: 'bash',
  38. code: '$ pip install --upgrade sentry-sdk',
  39. },
  40. ],
  41. },
  42. {
  43. type: StepType.CONFIGURE,
  44. description: (
  45. <p>
  46. {tct('Wrap your ASGI application with [code: SentryAsgiMiddleware]:', {
  47. code: <code />,
  48. })}
  49. </p>
  50. ),
  51. configurations: [
  52. {
  53. language: 'python',
  54. code: `
  55. import sentry_sdk
  56. from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
  57. from myapp import asgi_app
  58. sentry_sdk.init(
  59. ${sentryInitContent}
  60. )
  61. asgi_app = SentryAsgiMiddleware(asgi_app)
  62. `,
  63. },
  64. ],
  65. additionalInfo: t('The middleware supports both ASGI 2 and ASGI 3 transparently.'),
  66. },
  67. {
  68. type: StepType.VERIFY,
  69. description: (
  70. <p>{t('To verify that everything is working trigger an error on purpose:')}</p>
  71. ),
  72. configurations: [
  73. {
  74. language: 'python',
  75. code: `sentry_sdk.init(
  76. ${sentryInitContent}
  77. )
  78. def app(scope):
  79. async def get_body():
  80. return f"The number is: {1/0}" # raises an error!
  81. async def asgi(receive, send):
  82. await send(
  83. {
  84. "type": "http.response.start",
  85. "status": 200,
  86. "headers": [[b"content-type", b"text/plain"]],
  87. }
  88. )
  89. await send({"type": "http.response.body", "body": await get_body()})
  90. return asgi
  91. app = SentryAsgiMiddleware(app)
  92. `,
  93. },
  94. ],
  95. additionalInfo: (
  96. <span>
  97. <p>
  98. {tct(
  99. '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.',
  100. {
  101. code: <code />,
  102. link: <ExternalLink href="http://localhost:8000" />,
  103. }
  104. )}
  105. </p>
  106. <p>
  107. {t(
  108. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  109. )}
  110. </p>
  111. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  112. </span>
  113. ),
  114. },
  115. ];
  116. // Configuration End
  117. export function GettingStartedWithASGI({
  118. dsn,
  119. activeProductSelection = [],
  120. ...props
  121. }: ModuleProps) {
  122. const otherConfigs: string[] = [];
  123. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  124. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  125. otherConfigs.push(performanceConfiguration);
  126. }
  127. sentryInitContent = sentryInitContent.concat(otherConfigs);
  128. return (
  129. <Layout
  130. introduction={introduction}
  131. steps={steps({
  132. sentryInitContent: sentryInitContent.join('\n'),
  133. })}
  134. {...props}
  135. />
  136. );
  137. }
  138. export default GettingStartedWithASGI;