celery.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import Alert from 'sentry/components/alert';
  4. import {CodeSnippet} from 'sentry/components/codeSnippet';
  5. import ExternalLink from 'sentry/components/links/externalLink';
  6. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  7. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  8. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  9. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  10. import {t, tct} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. // Configuration Start
  13. const performanceConfiguration = ` # Set traces_sample_rate to 1.0 to capture 100%
  14. # of transactions for performance monitoring.
  15. traces_sample_rate=1.0,`;
  16. const profilingConfiguration = ` # Set profiles_sample_rate to 1.0 to profile 100%
  17. # of sampled transactions.
  18. # We recommend adjusting this value in production.
  19. profiles_sample_rate=1.0,`;
  20. const introduction = (
  21. <p>
  22. {tct('The celery integration adds support for the [link:Celery Task Queue System].', {
  23. link: <ExternalLink href="https://docs.celeryq.dev/en/stable/" />,
  24. })}
  25. </p>
  26. );
  27. export const steps = ({
  28. sentryInitContent,
  29. }: {
  30. sentryInitContent: string;
  31. }): LayoutProps['steps'] => [
  32. {
  33. type: StepType.INSTALL,
  34. description: (
  35. <p>
  36. {tct('Install [code:sentry-sdk] from PyPI with the [code:celery] extra:', {
  37. code: <code />,
  38. })}
  39. </p>
  40. ),
  41. configurations: [
  42. {
  43. language: 'bash',
  44. code: 'pip install --upgrade sentry-sdk[celery]',
  45. },
  46. ],
  47. },
  48. {
  49. type: StepType.CONFIGURE,
  50. description: (
  51. <div>
  52. <p>
  53. {tct(
  54. 'If you have the [code:celery] package in your dependencies, the Celery integration will be enabled automatically when you initialize the Sentry SDK.',
  55. {
  56. code: <code />,
  57. }
  58. )}
  59. </p>
  60. <p>
  61. {tct(
  62. 'Make sure that the call to [code:init] is loaded on worker startup, and not only in the module where your tasks are defined. Otherwise, the initialization happens too late and events might end up not being reported.',
  63. {
  64. code: <code />,
  65. }
  66. )}
  67. </p>
  68. </div>
  69. ),
  70. configurations: [
  71. {
  72. language: 'python',
  73. code: `
  74. import sentry_sdk
  75. sentry_sdk.init(
  76. ${sentryInitContent}
  77. )
  78. `,
  79. },
  80. ],
  81. additionalInfo: (
  82. <Fragment>
  83. <h5>{t('Standalone Setup')}</h5>
  84. {t("If you're using Celery standalone, there are two ways to set this up:")}
  85. <ul>
  86. <li>
  87. {tct(
  88. "Initializing the SDK in the configuration file loaded with Celery's [code:--config] parameter",
  89. {
  90. code: <code />,
  91. }
  92. )}
  93. </li>
  94. <li>
  95. {tct(
  96. 'Initializing the SDK by hooking it to either the [celerydInit: celeryd_init] or [workerInit: worker_init] signals:',
  97. {
  98. celerydInit: (
  99. <ExternalLink href="https://docs.celeryq.dev/en/stable/userguide/signals.html?#celeryd-init" />
  100. ),
  101. workerInit: (
  102. <ExternalLink href="https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init" />
  103. ),
  104. }
  105. )}
  106. <CodeSnippet dark language="python">
  107. {`import sentry_sdk
  108. from celery import Celery, signals
  109. app = Celery("myapp")
  110. #@signals.worker_init.connect
  111. @signals.celeryd_init.connect
  112. def init_sentry(**_kwargs):
  113. sentry_sdk.init(...) # same as above
  114. `}
  115. </CodeSnippet>
  116. </li>
  117. </ul>
  118. <h5>{t('Setup With Django')}</h5>
  119. <p>
  120. {tct(
  121. "If you're using Celery with Django in a conventional setup, have already initialized the SDK in [settingsLink:your settings.py], and have Celery using the same settings with [celeryDocsLinks:config_from_object], you don't need to initialize the SDK separately for Celery.",
  122. {
  123. settingsLink: (
  124. <ExternalLink href="https://docs.sentry.io/platforms/python/guides/django/#configure" />
  125. ),
  126. celeryDocsLinks: (
  127. <ExternalLink href="https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html" />
  128. ),
  129. }
  130. )}
  131. </p>
  132. </Fragment>
  133. ),
  134. },
  135. {
  136. type: StepType.VERIFY,
  137. description: (
  138. <div>
  139. <p>
  140. {t(
  141. "To verify if your SDK is initialized on worker start, you can pass `debug=True` to `sentry_sdk.init()` to see extra output when the SDK is initialized. If the output appears during worker startup and not only after a task has started, then it's working properly."
  142. )}
  143. </p>
  144. <AlertWithMarginBottom type="info">
  145. {tct(
  146. `Sentry uses custom message headers for distributed tracing. For Celery versions 4.x, with [celeryDocLink: message protocol of version 1], this functionality is broken, and Celery fails to propagate custom headers to the worker. Protocol version 2, which is the default since Celery version 4.0, is not affected.
  147. The fix for the custom headers propagation issue was introduced to Celery project ([celeryPRLink: PR]) starting with version 5.0.1. However, the fix was not backported to versions 4.x.
  148. `,
  149. {
  150. celeryDocLink: (
  151. <ExternalLink href="https://docs.celeryq.dev/en/stable/internals/protocol.html#version-1" />
  152. ),
  153. celeryPRLink: (
  154. <ExternalLink href="https://github.com/celery/celery/pull/6374" />
  155. ),
  156. }
  157. )}
  158. </AlertWithMarginBottom>
  159. </div>
  160. ),
  161. },
  162. ];
  163. // Configuration End
  164. export function GettingStartedWithCelery({
  165. dsn,
  166. activeProductSelection = [],
  167. ...props
  168. }: ModuleProps) {
  169. const otherConfigs: string[] = [];
  170. let sentryInitContent: string[] = [` dsn="${dsn}",`];
  171. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  172. otherConfigs.push(performanceConfiguration);
  173. }
  174. if (activeProductSelection.includes(ProductSolution.PROFILING)) {
  175. otherConfigs.push(profilingConfiguration);
  176. }
  177. sentryInitContent = sentryInitContent.concat(otherConfigs);
  178. return (
  179. <Layout
  180. introduction={introduction}
  181. steps={steps({
  182. sentryInitContent: sentryInitContent.join('\n'),
  183. })}
  184. {...props}
  185. />
  186. );
  187. }
  188. export default GettingStartedWithCelery;
  189. const AlertWithMarginBottom = styled(Alert)`
  190. margin-top: ${space(2)};
  191. margin-bottom: 0;
  192. `;