rq.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  6. import {t, tct} from 'sentry/locale';
  7. // Configuration Start
  8. const performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  9. # of transactions for performance monitoring.
  10. traces_sample_rate=1.0,`;
  11. const profilingConfiguration = ` # Set profiles_sample_rate to 1.0 to profile 100%
  12. # of sampled transactions.
  13. # We recommend adjusting this value in production.
  14. profiles_sample_rate=1.0,`;
  15. const introduction = (
  16. <p>
  17. {tct('The RQ integration adds support for the [link:RQ Job Queue System].', {
  18. link: <ExternalLink href="https://python-rq.org/" />,
  19. })}
  20. </p>
  21. );
  22. export const steps = ({
  23. sentryInitContent,
  24. }: {
  25. sentryInitContent: string;
  26. }): LayoutProps['steps'] => [
  27. {
  28. type: StepType.CONFIGURE,
  29. description: (
  30. <span>
  31. <p>
  32. {tct(
  33. 'If you have the [codeRq:rq] package in your dependencies, the RQ integration will be enabled automatically when you initialize the Sentry SDK.',
  34. {
  35. codeRq: <code />,
  36. }
  37. )}
  38. </p>
  39. <p>
  40. {tct('Create a file called [code:mysettings.py] with the following content:', {
  41. code: <code />,
  42. })}
  43. </p>
  44. </span>
  45. ),
  46. configurations: [
  47. {
  48. language: 'python',
  49. code: `
  50. # mysettings.py
  51. import sentry_sdk
  52. sentry_sdk.init(
  53. ${sentryInitContent}
  54. )
  55. `,
  56. },
  57. {
  58. description: t('Start your worker with:'),
  59. language: 'shell',
  60. code: `
  61. rq worker \
  62. -c mysettings \ # module name of mysettings.py
  63. --sentry-dsn="..." # only necessary for RQ < 1.0
  64. `,
  65. },
  66. ],
  67. additionalInfo: (
  68. <p>
  69. {tct(
  70. 'Generally, make sure that the call to [code:init] is loaded on worker startup, and not only in the module where your jobs are defined. Otherwise, the initialization happens too late and events might end up not being reported.',
  71. {code: <code />}
  72. )}
  73. </p>
  74. ),
  75. },
  76. {
  77. type: StepType.VERIFY,
  78. description: (
  79. <p>
  80. {' '}
  81. {tct(
  82. 'To verify, create a simple job and a [code:main.py] script that enqueues the job in RQ, then start an RQ worker to run the job:',
  83. {
  84. code: <code />,
  85. }
  86. )}
  87. </p>
  88. ),
  89. configurations: [
  90. {
  91. description: <h5>{t('Job definition')}</h5>,
  92. language: 'python',
  93. code: `# jobs.py
  94. def hello(name):
  95. 1/0 # raises an error
  96. return "Hello %s!" % name
  97. `,
  98. },
  99. {
  100. description: <h5>{t('Settings for worker')}</h5>,
  101. language: 'python',
  102. code: `# mysettings.py
  103. import sentry_sdk
  104. # Sentry configuration for RQ worker processes
  105. sentry_sdk.init(
  106. ${sentryInitContent}
  107. )
  108. `,
  109. },
  110. {
  111. description: <h5>{t('Main Python Script')}</h5>,
  112. language: 'python',
  113. code: `# main.py
  114. from redis import Redis
  115. from rq import Queue
  116. from jobs import hello
  117. import sentry_sdk
  118. # Sentry configuration for main.py process (same as above)
  119. sentry_sdk.init(
  120. ${sentryInitContent}
  121. )
  122. q = Queue(connection=Redis())
  123. with sentry_sdk.start_transaction(name="testing_sentry"):
  124. result = q.enqueue(hello, "World")
  125. `,
  126. },
  127. ],
  128. additionalInfo: (
  129. <div>
  130. <p>
  131. {tct(
  132. 'When you run [codeMain:python main.py] a transaction named [codeTrxName:testing_sentry] in the Performance section of Sentry will be created.',
  133. {
  134. codeMain: <code />,
  135. codeTrxName: <code />,
  136. }
  137. )}
  138. </p>
  139. <p>
  140. {tct(
  141. 'If you run the RQ worker with [codeWorker:rq worker -c mysettings] a transaction for the execution of [codeFunction:hello()] will be created. Additionally, an error event will be sent to Sentry and will be connected to the transaction.',
  142. {
  143. codeWorker: <code />,
  144. codeFunction: <code />,
  145. }
  146. )}
  147. </p>
  148. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  149. </div>
  150. ),
  151. },
  152. ];
  153. // Configuration End
  154. export function GettingStartedWithRq({
  155. dsn,
  156. activeProductSelection = [],
  157. ...props
  158. }: ModuleProps) {
  159. const otherConfigs: string[] = [];
  160. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  161. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  162. otherConfigs.push(performanceConfiguration);
  163. }
  164. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  165. otherConfigs.push(profilingConfiguration);
  166. }
  167. sentryInitContent = sentryInitContent.concat(otherConfigs);
  168. return (
  169. <Layout
  170. introduction={introduction}
  171. steps={steps({
  172. sentryInitContent: sentryInitContent.join('\n'),
  173. })}
  174. {...props}
  175. />
  176. );
  177. }
  178. export default GettingStartedWithRq;