rq.tsx 6.2 KB

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