Skip to content

Commit bcd9085

Browse files
authored
feat(taskworker) Collect task parameter distributions (#91985)
Start collecting distributions on task payloads. We want to validate assumptions about the distribution of task parameter sizes in production. Include this in the other parameter verification sample rate logic we're running on tasks.
1 parent 238ae97 commit bcd9085

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/sentry/celery.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.db import models
1212
from django.utils.safestring import SafeString
1313

14-
from sentry.utils import metrics
14+
from sentry.utils import json, metrics
1515

1616
logger = logging.getLogger("celery.pickle")
1717

@@ -114,6 +114,19 @@ def apply_async(self, *args, **kwargs):
114114
)
115115
should_sample = random.random() <= settings.CELERY_PICKLE_ERROR_REPORT_SAMPLE_RATE
116116
if should_complain or should_sample:
117+
try:
118+
param_size = json.dumps({"args": args, "kwargs": kwargs})
119+
metrics.distribution(
120+
"celery.task.parameter_bytes",
121+
len(param_size.encode("utf8")),
122+
tags={"taskname": self.name},
123+
sample_rate=1.0,
124+
)
125+
except Exception as e:
126+
logger.warning(
127+
"task.payload.measure.failure", extra={"error": str(e), "task": self.name}
128+
)
129+
117130
try:
118131
good_use_of_pickle_or_bad_use_of_pickle(self, args, kwargs)
119132
except TypeError:

tests/sentry/tasks/test_base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sentry.tasks.base import instrumented_task, retry
88
from sentry.taskworker.config import TaskworkerConfig
99
from sentry.taskworker.namespaces import test_tasks
10+
from sentry.testutils.helpers.options import override_options
1011

1112

1213
@instrumented_task(
@@ -112,6 +113,28 @@ def test_exclude_exception_retry(capture_exception):
112113
assert capture_exception.call_count == 0
113114

114115

116+
@override_settings(
117+
CELERY_COMPLAIN_ABOUT_BAD_USE_OF_PICKLE=True,
118+
CELERY_PICKLE_ERROR_REPORT_SAMPLE_RATE=1.0,
119+
)
120+
@override_options(
121+
{
122+
"taskworker.test.rollout": {"*": 0.0},
123+
"taskworker.route.overrides": {},
124+
}
125+
)
126+
@patch("sentry.tasks.base.metrics.distribution")
127+
def test_capture_payload_metrics(mock_distribution):
128+
region_task.apply_async(args=("bruh",))
129+
130+
mock_distribution.assert_called_once_with(
131+
"celery.task.parameter_bytes",
132+
71,
133+
tags={"taskname": "test.tasks.test_base.region_task"},
134+
sample_rate=1.0,
135+
)
136+
137+
115138
@patch("sentry.taskworker.retry.current_task")
116139
@patch("sentry.tasks.base.capture_exception")
117140
def test_retry_on(capture_exception, current_task):

0 commit comments

Comments
 (0)