rq.tsx 5.7 KB

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