|
8 | 8 | _get_headers,
|
9 | 9 | _get_humanized_interval,
|
10 | 10 | _get_monitor_config,
|
| 11 | + _patch_beat_apply_entry, |
11 | 12 | crons_task_success,
|
12 | 13 | crons_task_failure,
|
13 | 14 | crons_task_retry,
|
@@ -243,3 +244,56 @@ def test_get_monitor_config_default_timezone():
|
243 | 244 | monitor_config = _get_monitor_config(celery_schedule, app)
|
244 | 245 |
|
245 | 246 | assert monitor_config["timezone"] == "UTC"
|
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.parametrize( |
| 250 | + "task_name,exclude_beat_tasks,task_in_excluded_beat_tasks", |
| 251 | + [ |
| 252 | + ["some_task_name", ["xxx", "some_task.*"], True], |
| 253 | + ["some_task_name", ["xxx", "some_other_task.*"], False], |
| 254 | + ], |
| 255 | +) |
| 256 | +def test_exclude_beat_tasks_option( |
| 257 | + task_name, exclude_beat_tasks, task_in_excluded_beat_tasks |
| 258 | +): |
| 259 | + """ |
| 260 | + Test excluding Celery Beat tasks from automatic instrumentation. |
| 261 | + """ |
| 262 | + fake_apply_entry = mock.MagicMock() |
| 263 | + |
| 264 | + fake_scheduler = mock.MagicMock() |
| 265 | + fake_scheduler.apply_entry = fake_apply_entry |
| 266 | + |
| 267 | + fake_integration = mock.MagicMock() |
| 268 | + fake_integration.exclude_beat_tasks = exclude_beat_tasks |
| 269 | + |
| 270 | + fake_schedule_entry = mock.MagicMock() |
| 271 | + fake_schedule_entry.name = task_name |
| 272 | + |
| 273 | + fake_get_monitor_config = mock.MagicMock() |
| 274 | + |
| 275 | + with mock.patch( |
| 276 | + "sentry_sdk.integrations.celery.Scheduler", fake_scheduler |
| 277 | + ) as Scheduler: # noqa: N806 |
| 278 | + with mock.patch( |
| 279 | + "sentry_sdk.integrations.celery.Hub.current.get_integration", |
| 280 | + return_value=fake_integration, |
| 281 | + ): |
| 282 | + with mock.patch( |
| 283 | + "sentry_sdk.integrations.celery._get_monitor_config", |
| 284 | + fake_get_monitor_config, |
| 285 | + ) as _get_monitor_config: |
| 286 | + # Mimic CeleryIntegration patching of Scheduler.apply_entry() |
| 287 | + _patch_beat_apply_entry() |
| 288 | + # Mimic Celery Beat calling a task from the Beat schedule |
| 289 | + Scheduler.apply_entry(fake_scheduler, fake_schedule_entry) |
| 290 | + |
| 291 | + if task_in_excluded_beat_tasks: |
| 292 | + # Only the original Scheduler.apply_entry() is called, _get_monitor_config is NOT called. |
| 293 | + fake_apply_entry.assert_called_once() |
| 294 | + _get_monitor_config.assert_not_called() |
| 295 | + |
| 296 | + else: |
| 297 | + # The original Scheduler.apply_entry() is called, AND _get_monitor_config is called. |
| 298 | + fake_apply_entry.assert_called_once() |
| 299 | + _get_monitor_config.assert_called_once() |
0 commit comments