Skip to content

Commit 35070ce

Browse files
fix(celery): Propagate user-set headers (#5581)
### Description The SDK interferes with Celery logic that puts custom headers into `request.headers` in the worker. Currently, we only make internal sentry headers available there. Make sure to copy over any user-set headers as well. #### Issues Closes #5566 #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 13157f7 commit 35070ce

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

sentry_sdk/integrations/celery/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ def _update_celery_task_headers(
231231
if key.startswith("sentry-"):
232232
updated_headers["headers"][key] = value
233233

234+
# Preserve user-provided custom headers in the inner "headers" dict
235+
# so they survive to task.request.headers on the worker (celery#4875).
236+
for key, value in original_headers.items():
237+
if key != "headers" and key not in updated_headers["headers"]:
238+
updated_headers["headers"][key] = value
239+
234240
return updated_headers
235241

236242

tests/integrations/celery/test_celery.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,35 @@ def test_send_task_wrapped(
847847
assert span["trace_id"] == kwargs["headers"]["sentry-trace"].split("-")[0]
848848

849849

850+
def test_user_custom_headers_accessible_in_task(init_celery):
851+
"""
852+
Regression test for https://github.com/getsentry/sentry-python/issues/5566
853+
854+
User-provided custom headers passed to apply_async() must be accessible
855+
via task.request.headers on the worker side.
856+
"""
857+
celery = init_celery(traces_sample_rate=1.0)
858+
859+
@celery.task(name="custom_headers_task", bind=True)
860+
def custom_headers_task(self):
861+
return dict(self.request.headers or {})
862+
863+
custom_headers = {
864+
"my_custom_key": "my_value",
865+
"correlation_id": "abc-123",
866+
"tenant_id": "tenant-42",
867+
}
868+
869+
with start_transaction(name="test"):
870+
result = custom_headers_task.apply_async(headers=custom_headers)
871+
872+
received_headers = result.get()
873+
for key, value in custom_headers.items():
874+
assert received_headers.get(key) == value, (
875+
f"Custom header {key!r} not found in task.request.headers"
876+
)
877+
878+
850879
@pytest.mark.skip(reason="placeholder so that forked test does not come last")
851880
def test_placeholder():
852881
"""Forked tests must not come last in the module.

0 commit comments

Comments
 (0)