|
5 | 5 | pytest.importorskip("celery") |
6 | 6 |
|
7 | 7 | from sentry_sdk import Hub, configure_scope, start_transaction |
8 | | -from sentry_sdk.integrations.celery import CeleryIntegration |
| 8 | +from sentry_sdk.integrations.celery import CeleryIntegration, _get_headers |
| 9 | + |
9 | 10 | from sentry_sdk._compat import text_type |
10 | 11 |
|
11 | 12 | from celery import Celery, VERSION |
12 | 13 | from celery.bin import worker |
| 14 | +from celery.signals import task_success |
13 | 15 |
|
14 | 16 | try: |
15 | 17 | from unittest import mock # python 3.3 and above |
@@ -437,3 +439,29 @@ def dummy_task(x, y): |
437 | 439 | celery_invocation(dummy_task, 1, 0) |
438 | 440 |
|
439 | 441 | assert not events |
| 442 | + |
| 443 | + |
| 444 | +def test_task_headers(celery): |
| 445 | + """ |
| 446 | + Test that the headers set in the Celery Beat auto-instrumentation are passed to the celery signal handlers |
| 447 | + """ |
| 448 | + sentry_crons_setup = { |
| 449 | + "sentry-monitor-slug": "some-slug", |
| 450 | + "sentry-monitor-config": {"some": "config"}, |
| 451 | + "sentry-monitor-check-in-id": "123abc", |
| 452 | + } |
| 453 | + |
| 454 | + @celery.task(name="dummy_task") |
| 455 | + def dummy_task(x, y): |
| 456 | + return x + y |
| 457 | + |
| 458 | + def crons_task_success(sender, **kwargs): |
| 459 | + headers = _get_headers(sender) |
| 460 | + assert headers == sentry_crons_setup |
| 461 | + |
| 462 | + task_success.connect(crons_task_success) |
| 463 | + |
| 464 | + # This is how the Celery Beat auto-instrumentation starts a task |
| 465 | + # in the monkey patched version of `apply_async` |
| 466 | + # in `sentry_sdk/integrations/celery.py::_wrap_apply_async()` |
| 467 | + dummy_task.apply_async(args=(1, 0), headers=sentry_crons_setup) |
0 commit comments