rq.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 {t, tct} from 'sentry/locale';
  11. type Params = DocsParams;
  12. const getInstallSnippet = () => `pip install --upgrade sentry-sdk[rq]`;
  13. const getInitCallSnippet = (params: Params) => `
  14. sentry_sdk.init(
  15. dsn="${params.dsn}",${
  16. params.isPerformanceSelected
  17. ? `
  18. # Set traces_sample_rate to 1.0 to capture 100%
  19. # of transactions for performance monitoring.
  20. traces_sample_rate=1.0,`
  21. : ''
  22. }${
  23. params.isProfilingSelected
  24. ? `
  25. # Set profiles_sample_rate to 1.0 to profile 100%
  26. # of sampled transactions.
  27. # We recommend adjusting this value in production.
  28. profiles_sample_rate=1.0,`
  29. : ''
  30. }
  31. )`;
  32. const getSdkSetupSnippet = (params: Params) => `
  33. # mysettings.py
  34. import sentry_sdk
  35. ${getInitCallSnippet(params)}`;
  36. const getStartWorkerSnippet = () => `
  37. rq worker \
  38. -c mysettings \ # module name of mysettings.py
  39. --sentry-dsn="..." # only necessary for RQ < 1.0`;
  40. const getJobDefinitionSnippet = () => `# jobs.py
  41. def hello(name):
  42. 1/0 # raises an error
  43. return "Hello %s!" % name`;
  44. const getWorkerSetupSnippet = (params: Params) => `
  45. # mysettings.py
  46. import sentry_sdk
  47. # Sentry configuration for RQ worker processes
  48. ${getInitCallSnippet(params)}`;
  49. const getMainPythonScriptSetupSnippet = (params: Params) => `
  50. # main.py
  51. from redis import Redis
  52. from rq import Queue
  53. from jobs import hello
  54. import sentry_sdk
  55. #import { get } from 'lodash';
  56. Sentry configuration for main.py process (same as above)
  57. ${getInitCallSnippet(params)}
  58. q = Queue(connection=Redis())
  59. with sentry_sdk.start_transaction(name="testing_sentry"):
  60. result = q.enqueue(hello, "World")`;
  61. const onboarding: OnboardingConfig = {
  62. introduction: () =>
  63. tct('The RQ integration adds support for the [link:RQ Job Queue System].', {
  64. link: <ExternalLink href="https://python-rq.org/" />,
  65. }),
  66. install: () => [
  67. {
  68. type: StepType.INSTALL,
  69. description: tct(
  70. 'Install [code:sentry-sdk] from PyPI with the [sentryRQCode:rq] extra:',
  71. {
  72. code: <code />,
  73. sentryRQCode: <code />,
  74. }
  75. ),
  76. configurations: [
  77. {
  78. language: 'bash',
  79. code: getInstallSnippet(),
  80. },
  81. ],
  82. },
  83. ],
  84. configure: (params: Params) => [
  85. {
  86. type: StepType.CONFIGURE,
  87. description: (
  88. <Fragment>
  89. <p>
  90. {tct(
  91. 'If you have the [codeRq:rq] package in your dependencies, the RQ integration will be enabled automatically when you initialize the Sentry SDK.',
  92. {
  93. codeRq: <code />,
  94. }
  95. )}
  96. </p>
  97. <p>
  98. {tct(
  99. 'Create a file called [code:mysettings.py] with the following content:',
  100. {
  101. code: <code />,
  102. }
  103. )}
  104. </p>
  105. </Fragment>
  106. ),
  107. configurations: [
  108. {
  109. language: 'python',
  110. code: getSdkSetupSnippet(params),
  111. },
  112. {
  113. description: t('Start your worker with:'),
  114. language: 'shell',
  115. code: getStartWorkerSnippet(),
  116. },
  117. ],
  118. additionalInfo: tct(
  119. '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.',
  120. {code: <code />}
  121. ),
  122. },
  123. ],
  124. verify: params => [
  125. {
  126. type: StepType.VERIFY,
  127. description: tct(
  128. '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:',
  129. {
  130. code: <code />,
  131. }
  132. ),
  133. configurations: [
  134. {
  135. description: <h5>{t('Job definition')}</h5>,
  136. language: 'python',
  137. code: getJobDefinitionSnippet(),
  138. },
  139. {
  140. description: <h5>{t('Settings for worker')}</h5>,
  141. language: 'python',
  142. code: getWorkerSetupSnippet(params),
  143. },
  144. {
  145. description: <h5>{t('Main Python Script')}</h5>,
  146. language: 'python',
  147. code: getMainPythonScriptSetupSnippet(params),
  148. },
  149. ],
  150. additionalInfo: (
  151. <div>
  152. <p>
  153. {tct(
  154. 'When you run [codeMain:python main.py] a transaction named [codeTrxName:testing_sentry] in the Performance section of Sentry will be created.',
  155. {
  156. codeMain: <code />,
  157. codeTrxName: <code />,
  158. }
  159. )}
  160. </p>
  161. <p>
  162. {tct(
  163. '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.',
  164. {
  165. codeWorker: <code />,
  166. codeFunction: <code />,
  167. }
  168. )}
  169. </p>
  170. <p>{t('It takes a couple of moments for the data to appear in Sentry.')}</p>
  171. </div>
  172. ),
  173. },
  174. ],
  175. };
  176. const docs: Docs = {
  177. onboarding,
  178. customMetricsOnboarding: getPythonMetricsOnboarding({
  179. installSnippet: getInstallSnippet(),
  180. }),
  181. };
  182. export default docs;