wsgi.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import type {
  5. Docs,
  6. DocsParams,
  7. OnboardingConfig,
  8. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  9. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  10. import {crashReportOnboardingPython} from 'sentry/gettingStartedDocs/python/python';
  11. import {t, tct} from 'sentry/locale';
  12. type Params = DocsParams;
  13. const getInstallSnippet = () => `pip install --upgrade sentry-sdk`;
  14. const getSdkSetupSnippet = (params: Params) => `
  15. import sentry_sdk
  16. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  17. from my_wsgi_app import app
  18. sentry_sdk.init(
  19. dsn="${params.dsn}",${
  20. params.isPerformanceSelected
  21. ? `
  22. # Set traces_sample_rate to 1.0 to capture 100%
  23. # of transactions for performance monitoring.
  24. traces_sample_rate=1.0,`
  25. : ''
  26. }${
  27. params.isProfilingSelected
  28. ? `
  29. # Set profiles_sample_rate to 1.0 to profile 100%
  30. # of sampled transactions.
  31. # We recommend adjusting this value in production.
  32. profiles_sample_rate=1.0,`
  33. : ''
  34. }
  35. )
  36. app = SentryWsgiMiddleware(app)`;
  37. const getVerifySnippet = () => `
  38. import sentry_sdk
  39. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  40. sentry_sdk.init(...) # same as above
  41. def app(env, start_response):
  42. start_response('200 OK', [('Content-Type', 'text/plain')])
  43. response_body = 'Hello World'
  44. 1/0 # this raises an error
  45. return [response_body.encode()]
  46. app = SentryWsgiMiddleware(app)
  47. # Run the application in a mini WSGI server.
  48. from wsgiref.simple_server import make_server
  49. make_server('', 8000, app).serve_forever()`;
  50. const onboarding: OnboardingConfig = {
  51. introduction: () => (
  52. <Fragment>
  53. <p>
  54. {tct(
  55. 'It is recommended to use an [link:integration for your particular WSGI framework if available], as those are easier to use and capture more useful information.',
  56. {
  57. link: (
  58. <ExternalLink href="https://docs.sentry.io/platforms/python/#web-frameworks" />
  59. ),
  60. }
  61. )}
  62. </p>
  63. <p>
  64. {t(
  65. 'If you use a WSGI framework not directly supported by the SDK, or wrote a raw WSGI app, you can use this generic WSGI middleware. It captures errors and attaches a basic amount of information for incoming requests.'
  66. )}
  67. </p>
  68. </Fragment>
  69. ),
  70. install: () => [
  71. {
  72. type: StepType.INSTALL,
  73. description: tct('Install our Python SDK using [code:pip]:', {
  74. code: <code />,
  75. }),
  76. configurations: [
  77. {
  78. language: 'bash',
  79. code: getInstallSnippet(),
  80. },
  81. ],
  82. },
  83. ],
  84. configure: params => [
  85. {
  86. type: StepType.CONFIGURE,
  87. description: t(
  88. 'Then you can use this generic WSGI middleware. It captures errors and attaches a basic amount of information for incoming requests.'
  89. ),
  90. configurations: [
  91. {
  92. language: 'python',
  93. code: getSdkSetupSnippet(params),
  94. },
  95. ],
  96. },
  97. ],
  98. verify: () => [
  99. {
  100. type: StepType.VERIFY,
  101. description: t(
  102. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  103. ),
  104. configurations: [
  105. {
  106. language: 'python',
  107. code: getVerifySnippet(),
  108. },
  109. ],
  110. additionalInfo: (
  111. <Fragment>
  112. <p>
  113. {tct(
  114. 'When you point your browser to [link:http://localhost:8000/] a transaction in the Performance section of Sentry will be created.',
  115. {
  116. link: <ExternalLink href="http://localhost:8000/" />,
  117. }
  118. )}
  119. </p>
  120. <p>
  121. {t(
  122. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  123. )}
  124. </p>
  125. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  126. </Fragment>
  127. ),
  128. },
  129. ],
  130. };
  131. const docs: Docs = {
  132. onboarding,
  133. customMetricsOnboarding: getPythonMetricsOnboarding({
  134. installSnippet: getInstallSnippet(),
  135. }),
  136. crashReportOnboarding: crashReportOnboardingPython,
  137. };
  138. export default docs;