wsgi.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  4. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  7. import {t, tct} from 'sentry/locale';
  8. // Configuration Start
  9. const performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  10. # of transactions for performance monitoring.
  11. traces_sample_rate=1.0,`;
  12. const profilingConfiguration = ` # Set profiles_sample_rate to 1.0 to profile 100%
  13. # of sampled transactions.
  14. # We recommend adjusting this value in production.
  15. profiles_sample_rate=1.0,`;
  16. export const steps = ({
  17. sentryInitContent,
  18. }: {
  19. sentryInitContent: string;
  20. }): LayoutProps['steps'] => [
  21. {
  22. type: StepType.INSTALL,
  23. description: (
  24. <Fragment>
  25. <p>
  26. {tct(
  27. '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.',
  28. {
  29. link: (
  30. <ExternalLink href="https://docs.sentry.io/platforms/python/#web-frameworks" />
  31. ),
  32. }
  33. )}
  34. </p>
  35. {t(
  36. '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.'
  37. )}
  38. </Fragment>
  39. ),
  40. configurations: [
  41. {
  42. language: 'python',
  43. code: `
  44. import sentry_sdk
  45. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  46. from my_wsgi_app import app
  47. sentry_sdk.init(
  48. ${sentryInitContent}
  49. )
  50. app = SentryWsgiMiddleware(app)
  51. `,
  52. },
  53. ],
  54. },
  55. {
  56. type: StepType.VERIFY,
  57. description: t(
  58. 'You can easily verify your Sentry installation by creating a route that triggers an error:'
  59. ),
  60. configurations: [
  61. {
  62. language: 'python',
  63. code: `import sentry_sdk
  64. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  65. sentry_sdk.init(
  66. ${sentryInitContent}
  67. )
  68. def app(env, start_response):
  69. start_response('200 OK', [('Content-Type', 'text/plain')])
  70. response_body = 'Hello World'
  71. 1/0 # this raises an error
  72. return [response_body.encode()]
  73. app = SentryWsgiMiddleware(app)
  74. # Run the application in a mini WSGI server.
  75. from wsgiref.simple_server import make_server
  76. make_server('', 8000, app).serve_forever()
  77. `,
  78. },
  79. ],
  80. additionalInfo: (
  81. <div>
  82. <p>
  83. {tct(
  84. 'When you point your browser to [link:http://localhost:8000/] a transaction in the Performance section of Sentry will be created.',
  85. {
  86. link: <ExternalLink href="http://localhost:8000/" />,
  87. }
  88. )}
  89. </p>
  90. <p>
  91. {t(
  92. 'Additionally, an error event will be sent to Sentry and will be connected to the transaction.'
  93. )}
  94. </p>
  95. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  96. </div>
  97. ),
  98. },
  99. ];
  100. // Configuration End
  101. export function GettingStartedWithWSGI({
  102. dsn,
  103. activeProductSelection = [],
  104. ...props
  105. }: ModuleProps) {
  106. const otherConfigs: string[] = [];
  107. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  108. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  109. otherConfigs.push(performanceConfiguration);
  110. }
  111. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  112. otherConfigs.push(profilingConfiguration);
  113. }
  114. sentryInitContent = sentryInitContent.concat(otherConfigs);
  115. return (
  116. <Layout
  117. steps={steps({
  118. sentryInitContent: sentryInitContent.join('\n'),
  119. })}
  120. {...props}
  121. />
  122. );
  123. }
  124. export default GettingStartedWithWSGI;