wsgi.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.public}",${
  20. params.isPerformanceSelected
  21. ? `
  22. # Set traces_sample_rate to 1.0 to capture 100%
  23. # of transactions for tracing.
  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: (params: Params) => [
  71. {
  72. type: StepType.INSTALL,
  73. description: tct('Install our Python SDK using [code:pip]:', {
  74. code: <code />,
  75. }),
  76. configurations: [
  77. {
  78. description: params.isProfilingSelected
  79. ? tct(
  80. 'You need a minimum version [codeVersion:1.18.0] of the [codePackage:sentry-python] SDK for the profiling feature.',
  81. {
  82. codeVersion: <code />,
  83. codePackage: <code />,
  84. }
  85. )
  86. : undefined,
  87. language: 'bash',
  88. code: getInstallSnippet(),
  89. },
  90. ],
  91. },
  92. ],
  93. configure: params => [
  94. {
  95. type: StepType.CONFIGURE,
  96. description: t(
  97. 'Then you can use this generic WSGI middleware. It captures errors and attaches a basic amount of information for incoming requests.'
  98. ),
  99. configurations: [
  100. {
  101. language: 'python',
  102. code: getSdkSetupSnippet(params),
  103. },
  104. ],
  105. },
  106. ],
  107. verify: () => [
  108. {
  109. type: StepType.VERIFY,
  110. description: t(
  111. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  112. ),
  113. configurations: [
  114. {
  115. language: 'python',
  116. code: getVerifySnippet(),
  117. },
  118. ],
  119. additionalInfo: (
  120. <Fragment>
  121. <p>
  122. {tct(
  123. 'When you point your browser to [link:http://localhost:8000/] a transaction in the Performance section of Sentry will be created.',
  124. {
  125. link: <ExternalLink href="http://localhost:8000/" />,
  126. }
  127. )}
  128. </p>
  129. <p>
  130. {t(
  131. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  132. )}
  133. </p>
  134. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  135. </Fragment>
  136. ),
  137. },
  138. ],
  139. };
  140. const docs: Docs = {
  141. onboarding,
  142. customMetricsOnboarding: getPythonMetricsOnboarding({
  143. installSnippet: getInstallSnippet(),
  144. }),
  145. crashReportOnboarding: crashReportOnboardingPython,
  146. };
  147. export default docs;