From ed73c010e8b2af64e6767e93154a8b37e7005f55 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 13 Jul 2023 11:40:18 +0200 Subject: [PATCH 1/2] Make sure each task that is started by Celery Beat has its own trace. --- sentry_sdk/integrations/celery.py | 46 +++++++++++++++++-------------- sentry_sdk/scope.py | 21 ++++++++++---- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py index 443fcdad45..ae2635a45d 100644 --- a/sentry_sdk/integrations/celery.py +++ b/sentry_sdk/integrations/celery.py @@ -462,30 +462,34 @@ def sentry_apply_entry(*args, **kwargs): if match_regex_list(monitor_name, integration.exclude_beat_tasks): return original_apply_entry(*args, **kwargs) - monitor_config = _get_monitor_config(celery_schedule, app) - - is_supported_schedule = bool(monitor_config) - if is_supported_schedule: - headers = schedule_entry.options.pop("headers", {}) - headers.update( - { - "sentry-monitor-slug": monitor_name, - "sentry-monitor-config": monitor_config, - } - ) + with hub.configure_scope() as scope: + # When tasks are started from Celery Beat, make sure each task has its own trace. + scope.set_new_propagation_context() + + monitor_config = _get_monitor_config(celery_schedule, app) + + is_supported_schedule = bool(monitor_config) + if is_supported_schedule: + headers = schedule_entry.options.pop("headers", {}) + headers.update( + { + "sentry-monitor-slug": monitor_name, + "sentry-monitor-config": monitor_config, + } + ) - check_in_id = capture_checkin( - monitor_slug=monitor_name, - monitor_config=monitor_config, - status=MonitorStatus.IN_PROGRESS, - ) - headers.update({"sentry-monitor-check-in-id": check_in_id}) + check_in_id = capture_checkin( + monitor_slug=monitor_name, + monitor_config=monitor_config, + status=MonitorStatus.IN_PROGRESS, + ) + headers.update({"sentry-monitor-check-in-id": check_in_id}) - # Set the Sentry configuration in the options of the ScheduleEntry. - # Those will be picked up in `apply_async` and added to the headers. - schedule_entry.options["headers"] = headers + # Set the Sentry configuration in the options of the ScheduleEntry. + # Those will be picked up in `apply_async` and added to the headers. + schedule_entry.options["headers"] = headers - return original_apply_entry(*args, **kwargs) + return original_apply_entry(*args, **kwargs) Scheduler.apply_entry = sentry_apply_entry diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 317d14c6b1..5a6816bde6 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -196,10 +196,23 @@ def _create_new_propagation_context(self): "dynamic_sampling_context": None, } + def set_new_propagation_context(self): + # type: () -> None + """ + Creates a new propagation context and sets it as `_propagation_context`. Overwriting existing one. + """ + logger.debug( + "[Tracing] Create new propagation context: %s", + self._propagation_context, + ) + self._propagation_context = self._create_new_propagation_context() + def generate_propagation_context(self, incoming_data=None): # type: (Optional[Dict[str, str]]) -> None """ - Populates `_propagation_context`. Either from `incoming_data` or with a new propagation context. + Makes sure `_propagation_context` is set. + If there is `incoming_data` overwrite existing `_propagation_context`. + if there is no `incoming_data` create new `_propagation_context`, but do NOT overwrite if already existing. """ if incoming_data: context = self._extract_propagation_context(incoming_data) @@ -212,11 +225,7 @@ def generate_propagation_context(self, incoming_data=None): ) if self._propagation_context is None: - self._propagation_context = self._create_new_propagation_context() - logger.debug( - "[Tracing] Create new propagation context: %s", - self._propagation_context, - ) + self.set_new_propagation_context() def get_dynamic_sampling_context(self): # type: () -> Optional[Dict[str, str]] From 993519b988d158c976bb128109482b67c3e04df5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 13 Jul 2023 11:59:40 +0200 Subject: [PATCH 2/2] Moved log statement --- sentry_sdk/scope.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 5a6816bde6..b83cd5f464 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -201,11 +201,11 @@ def set_new_propagation_context(self): """ Creates a new propagation context and sets it as `_propagation_context`. Overwriting existing one. """ + self._propagation_context = self._create_new_propagation_context() logger.debug( "[Tracing] Create new propagation context: %s", self._propagation_context, ) - self._propagation_context = self._create_new_propagation_context() def generate_propagation_context(self, incoming_data=None): # type: (Optional[Dict[str, str]]) -> None