wsgi.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {
  5. type Docs,
  6. DocsPageLocation,
  7. type DocsParams,
  8. type OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {
  11. AlternativeConfiguration,
  12. crashReportOnboardingPython,
  13. } from 'sentry/gettingStartedDocs/python/python';
  14. import {t, tct} from 'sentry/locale';
  15. type Params = DocsParams;
  16. const getInstallSnippet = () => `pip install --upgrade sentry-sdk`;
  17. const getSdkSetupSnippet = (params: Params) => `
  18. import sentry_sdk
  19. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  20. from my_wsgi_app import app
  21. sentry_sdk.init(
  22. dsn="${params.dsn.public}",
  23. # Add data like request headers and IP for users,
  24. # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
  25. send_default_pii=True,${
  26. params.isPerformanceSelected
  27. ? `
  28. # Set traces_sample_rate to 1.0 to capture 100%
  29. # of transactions for tracing.
  30. traces_sample_rate=1.0,`
  31. : ''
  32. }${
  33. params.isProfilingSelected &&
  34. params.profilingOptions?.defaultProfilingMode !== 'continuous'
  35. ? `
  36. # Set profiles_sample_rate to 1.0 to profile 100%
  37. # of sampled transactions.
  38. # We recommend adjusting this value in production.
  39. profiles_sample_rate=1.0,`
  40. : params.isProfilingSelected &&
  41. params.profilingOptions?.defaultProfilingMode === 'continuous'
  42. ? `
  43. _experiments={
  44. # Set continuous_profiling_auto_start to True
  45. # to automatically start the profiler on when
  46. # possible.
  47. "continuous_profiling_auto_start": True,
  48. },`
  49. : ''
  50. }
  51. )
  52. app = SentryWsgiMiddleware(app)`;
  53. const getVerifySnippet = () => `
  54. import sentry_sdk
  55. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  56. sentry_sdk.init(...) # same as above
  57. def app(env, start_response):
  58. start_response('200 OK', [('Content-Type', 'text/plain')])
  59. response_body = 'Hello World'
  60. 1/0 # this raises an error
  61. return [response_body.encode()]
  62. app = SentryWsgiMiddleware(app)
  63. # Run the application in a mini WSGI server.
  64. from wsgiref.simple_server import make_server
  65. make_server('', 8000, app).serve_forever()`;
  66. const onboarding: OnboardingConfig = {
  67. introduction: () => (
  68. <Fragment>
  69. <p>
  70. {tct(
  71. '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.',
  72. {
  73. link: (
  74. <ExternalLink href="https://docs.sentry.io/platforms/python/#web-frameworks" />
  75. ),
  76. }
  77. )}
  78. </p>
  79. <p>
  80. {t(
  81. '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.'
  82. )}
  83. </p>
  84. </Fragment>
  85. ),
  86. install: (params: Params) => [
  87. {
  88. type: StepType.INSTALL,
  89. description: tct('Install our Python SDK using [code:pip]:', {
  90. code: <code />,
  91. }),
  92. configurations: [
  93. {
  94. description:
  95. params.docsLocation === DocsPageLocation.PROFILING_PAGE
  96. ? tct(
  97. 'You need a minimum version [code:1.18.0] of the [code:sentry-python] SDK for the profiling feature.',
  98. {
  99. code: <code />,
  100. }
  101. )
  102. : undefined,
  103. language: 'bash',
  104. code: getInstallSnippet(),
  105. },
  106. ],
  107. },
  108. ],
  109. configure: params => [
  110. {
  111. type: StepType.CONFIGURE,
  112. description: t(
  113. 'Then you can use this generic WSGI middleware. It captures errors and attaches a basic amount of information for incoming requests.'
  114. ),
  115. configurations: [
  116. {
  117. language: 'python',
  118. code: getSdkSetupSnippet(params),
  119. },
  120. ],
  121. additionalInfo: params.isProfilingSelected &&
  122. params.profilingOptions?.defaultProfilingMode === 'continuous' && (
  123. <AlternativeConfiguration />
  124. ),
  125. },
  126. ],
  127. verify: () => [
  128. {
  129. type: StepType.VERIFY,
  130. description: t(
  131. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  132. ),
  133. configurations: [
  134. {
  135. language: 'python',
  136. code: getVerifySnippet(),
  137. },
  138. ],
  139. additionalInfo: (
  140. <Fragment>
  141. <p>
  142. {tct(
  143. 'When you point your browser to [link:http://localhost:8000/] a transaction in the Performance section of Sentry will be created.',
  144. {
  145. link: <ExternalLink href="http://localhost:8000/" />,
  146. }
  147. )}
  148. </p>
  149. <p>
  150. {t(
  151. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  152. )}
  153. </p>
  154. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  155. </Fragment>
  156. ),
  157. },
  158. ],
  159. };
  160. const docs: Docs = {
  161. onboarding,
  162. crashReportOnboarding: crashReportOnboardingPython,
  163. };
  164. export default docs;