Skip to content

Commit ec1ea6b

Browse files
authored
fix(tasks) Provide custom sampling context for taskworker (#96463)
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.
1 parent 7c38df2 commit ec1ea6b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/sentry/taskworker/workerchild.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,18 @@ def _execute_activation(task_func: Task[Any, Any], activation: TaskActivation) -
344344
name=activation.taskname,
345345
origin="taskworker",
346346
)
347+
sampling_context = {
348+
"taskworker": {
349+
"task": activation.taskname,
350+
}
351+
}
347352
with (
348353
track_memory_usage(
349354
"taskworker.worker.memory_change",
350355
tags={"namespace": activation.namespace, "taskname": activation.taskname},
351356
),
352357
sentry_sdk.isolation_scope(),
353-
sentry_sdk.start_transaction(transaction),
358+
sentry_sdk.start_transaction(transaction, custom_sampling_context=sampling_context),
354359
):
355360
transaction.set_data(
356361
"taskworker-task", {"args": args, "kwargs": kwargs, "id": activation.id}

src/sentry/utils/sdk.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ def traces_sampler(sampling_context):
198198
if task_name in SAMPLED_TASKS:
199199
return SAMPLED_TASKS[task_name]
200200

201+
if "taskworker" in sampling_context:
202+
task_name = sampling_context["taskworker"].get("task")
203+
204+
if task_name in SAMPLED_TASKS:
205+
return SAMPLED_TASKS[task_name]
206+
201207
# Default to the sampling rate in settings
202208
return float(settings.SENTRY_BACKEND_APM_SAMPLING or 0)
203209

0 commit comments

Comments
 (0)