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