wsgi.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 {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.wsgi import SentryWsgiMiddleware
  16. from my_wsgi_app import 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. app = SentryWsgiMiddleware(app)`;
  36. const getVerifySnippet = () => `
  37. import sentry_sdk
  38. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  39. sentry_sdk.init(...) # same as above
  40. def app(env, start_response):
  41. start_response('200 OK', [('Content-Type', 'text/plain')])
  42. response_body = 'Hello World'
  43. 1/0 # this raises an error
  44. return [response_body.encode()]
  45. app = SentryWsgiMiddleware(app)
  46. # Run the application in a mini WSGI server.
  47. from wsgiref.simple_server import make_server
  48. make_server('', 8000, app).serve_forever()`;
  49. const onboarding: OnboardingConfig = {
  50. introduction: () => (
  51. <Fragment>
  52. <p>
  53. {tct(
  54. '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.',
  55. {
  56. link: (
  57. <ExternalLink href="https://docs.sentry.io/platforms/python/#web-frameworks" />
  58. ),
  59. }
  60. )}
  61. </p>
  62. <p>
  63. {t(
  64. '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.'
  65. )}
  66. </p>
  67. </Fragment>
  68. ),
  69. install: () => [
  70. {
  71. type: StepType.INSTALL,
  72. description: tct('Install our Python SDK using [code:pip]:', {
  73. code: <code />,
  74. }),
  75. configurations: [
  76. {
  77. language: 'bash',
  78. code: getInstallSnippet(),
  79. },
  80. ],
  81. },
  82. ],
  83. configure: params => [
  84. {
  85. type: StepType.CONFIGURE,
  86. description: t(
  87. 'Then you can use this generic WSGI middleware. It captures errors and attaches a basic amount of information for incoming requests.'
  88. ),
  89. configurations: [
  90. {
  91. language: 'python',
  92. code: getSdkSetupSnippet(params),
  93. },
  94. ],
  95. },
  96. ],
  97. verify: () => [
  98. {
  99. type: StepType.VERIFY,
  100. description: t(
  101. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  102. ),
  103. configurations: [
  104. {
  105. language: 'python',
  106. code: getVerifySnippet(),
  107. },
  108. ],
  109. additionalInfo: (
  110. <Fragment>
  111. <p>
  112. {tct(
  113. 'When you point your browser to [link:http://localhost:8000/] a transaction in the Performance section of Sentry will be created.',
  114. {
  115. link: <ExternalLink href="http://localhost:8000/" />,
  116. }
  117. )}
  118. </p>
  119. <p>
  120. {t(
  121. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  122. )}
  123. </p>
  124. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  125. </Fragment>
  126. ),
  127. },
  128. ],
  129. };
  130. const docs: Docs = {
  131. onboarding,
  132. customMetricsOnboarding: getPythonMetricsOnboarding({
  133. installSnippet: getInstallSnippet(),
  134. }),
  135. };
  136. export default docs;