rq.tsx 6.7 KB

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