From c899ffafc11162625a0b0e4e2cf2cbcf767996cd Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 25 Jul 2025 14:49:15 -0400 Subject: [PATCH] fix(tasks) Provide custom sampling context for taskworker When reading through our SDK configuration code, I noticed that we had implemented custom sampling rates for tasks. This change should make the same sampling rates apply to taskworker tasks. --- src/sentry/taskworker/workerchild.py | 7 ++++++- src/sentry/utils/sdk.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/sentry/taskworker/workerchild.py b/src/sentry/taskworker/workerchild.py index 0534a1d5afde0f..4428f1c55f13db 100644 --- a/src/sentry/taskworker/workerchild.py +++ b/src/sentry/taskworker/workerchild.py @@ -344,13 +344,18 @@ def _execute_activation(task_func: Task[Any, Any], activation: TaskActivation) - name=activation.taskname, origin="taskworker", ) + sampling_context = { + "taskworker": { + "task": activation.taskname, + } + } with ( track_memory_usage( "taskworker.worker.memory_change", tags={"namespace": activation.namespace, "taskname": activation.taskname}, ), sentry_sdk.isolation_scope(), - sentry_sdk.start_transaction(transaction), + sentry_sdk.start_transaction(transaction, custom_sampling_context=sampling_context), ): transaction.set_data( "taskworker-task", {"args": args, "kwargs": kwargs, "id": activation.id} diff --git a/src/sentry/utils/sdk.py b/src/sentry/utils/sdk.py index d43d8b5078ea1a..f5f1947436e08b 100644 --- a/src/sentry/utils/sdk.py +++ b/src/sentry/utils/sdk.py @@ -198,6 +198,12 @@ def traces_sampler(sampling_context): if task_name in SAMPLED_TASKS: return SAMPLED_TASKS[task_name] + if "taskworker" in sampling_context: + task_name = sampling_context["taskworker"].get("task") + + if task_name in SAMPLED_TASKS: + return SAMPLED_TASKS[task_name] + # Default to the sampling rate in settings return float(settings.SENTRY_BACKEND_APM_SAMPLING or 0)