rq.tsx 6.9 KB

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