celery.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  7. import type {
  8. Docs,
  9. DocsParams,
  10. OnboardingConfig,
  11. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  12. import {getPythonMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  13. import {crashReportOnboardingPython} from 'sentry/gettingStartedDocs/python/python';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. type Params = DocsParams;
  17. const getInstallSnippet = () => `pip install --upgrade 'sentry-sdk[celery]'`;
  18. const getSdkSetupSnippet = (params: Params) => `
  19. import sentry_sdk
  20. sentry_sdk.init(
  21. dsn="${params.dsn}",${
  22. params.isPerformanceSelected
  23. ? `
  24. # Set traces_sample_rate to 1.0 to capture 100%
  25. # of transactions for performance monitoring.
  26. traces_sample_rate=1.0,`
  27. : ''
  28. }${
  29. params.isProfilingSelected
  30. ? `
  31. # Set profiles_sample_rate to 1.0 to profile 100%
  32. # of sampled transactions.
  33. # We recommend adjusting this value in production.
  34. profiles_sample_rate=1.0,`
  35. : ''
  36. }
  37. )`;
  38. const onboarding: OnboardingConfig = {
  39. introduction: () =>
  40. tct('The celery integration adds support for the [link:Celery Task Queue System].', {
  41. link: <ExternalLink href="https://docs.celeryq.dev/en/stable/" />,
  42. }),
  43. install: (params: Params) => [
  44. {
  45. type: StepType.INSTALL,
  46. description: tct(
  47. 'Install [code:sentry-sdk] from PyPI with the [code:celery] extra:',
  48. {
  49. code: <code />,
  50. }
  51. ),
  52. configurations: [
  53. {
  54. description: params.isProfilingSelected
  55. ? tct(
  56. 'You need a minimum version [codeVersion:1.18.0] of the [codePackage:sentry-python] SDK for the profiling feature.',
  57. {
  58. codeVersion: <code />,
  59. codePackage: <code />,
  60. }
  61. )
  62. : undefined,
  63. language: 'bash',
  64. code: getInstallSnippet(),
  65. },
  66. ],
  67. },
  68. ],
  69. configure: (params: Params) => [
  70. {
  71. type: StepType.CONFIGURE,
  72. description: (
  73. <div>
  74. <p>
  75. {tct(
  76. 'If you have the [code:celery] package in your dependencies, the Celery integration will be enabled automatically when you initialize the Sentry SDK.',
  77. {
  78. code: <code />,
  79. }
  80. )}
  81. </p>
  82. <p>
  83. {tct(
  84. '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.',
  85. {
  86. code: <code />,
  87. }
  88. )}
  89. </p>
  90. </div>
  91. ),
  92. configurations: [
  93. {
  94. language: 'python',
  95. code: getSdkSetupSnippet(params),
  96. },
  97. ],
  98. additionalInfo: (
  99. <Fragment>
  100. <h5>{t('Standalone Setup')}</h5>
  101. {t("If you're using Celery standalone, there are two ways to set this up:")}
  102. <ul>
  103. <li>
  104. {tct(
  105. "Initializing the SDK in the configuration file loaded with Celery's [code:--config] parameter",
  106. {
  107. code: <code />,
  108. }
  109. )}
  110. </li>
  111. <li>
  112. {tct(
  113. 'Initializing the SDK by hooking it to either the [celerydInit: celeryd_init] or [workerInit: worker_init] signals:',
  114. {
  115. celerydInit: (
  116. <ExternalLink href="https://docs.celeryq.dev/en/stable/userguide/signals.html?#celeryd-init" />
  117. ),
  118. workerInit: (
  119. <ExternalLink href="https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init" />
  120. ),
  121. }
  122. )}
  123. <CodeSnippet dark language="python">
  124. {`import sentry_sdk
  125. from celery import Celery, signals
  126. app = Celery("myapp")
  127. #@signals.worker_init.connect
  128. @signals.celeryd_init.connect
  129. def init_sentry(**_kwargs):
  130. sentry_sdk.init(...) # same as above
  131. `}
  132. </CodeSnippet>
  133. </li>
  134. </ul>
  135. <h5>{t('Setup With Django')}</h5>
  136. <p>
  137. {tct(
  138. "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.",
  139. {
  140. settingsLink: (
  141. <ExternalLink href="https://docs.sentry.io/platforms/python/guides/django/#configure" />
  142. ),
  143. celeryDocsLinks: (
  144. <ExternalLink href="https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html" />
  145. ),
  146. }
  147. )}
  148. </p>
  149. </Fragment>
  150. ),
  151. },
  152. ],
  153. verify: () => [
  154. {
  155. type: StepType.VERIFY,
  156. description: (
  157. <Fragment>
  158. <p>
  159. {t(
  160. "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."
  161. )}
  162. </p>
  163. <AlertWithMarginBottom type="info">
  164. {tct(
  165. `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.
  166. 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.
  167. `,
  168. {
  169. celeryDocLink: (
  170. <ExternalLink href="https://docs.celeryq.dev/en/stable/internals/protocol.html#version-1" />
  171. ),
  172. celeryPRLink: (
  173. <ExternalLink href="https://github.com/celery/celery/pull/6374" />
  174. ),
  175. }
  176. )}
  177. </AlertWithMarginBottom>
  178. </Fragment>
  179. ),
  180. },
  181. ],
  182. };
  183. const docs: Docs = {
  184. onboarding,
  185. customMetricsOnboarding: getPythonMetricsOnboarding({
  186. installSnippet: getInstallSnippet(),
  187. }),
  188. crashReportOnboarding: crashReportOnboardingPython,
  189. };
  190. export default docs;
  191. const AlertWithMarginBottom = styled(Alert)`
  192. margin-top: ${space(2)};
  193. margin-bottom: 0;
  194. `;