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