File tree Expand file tree Collapse file tree
sentry_sdk/integrations/celery
tests/integrations/celery Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -237,6 +237,12 @@ def _update_celery_task_headers(
237237 if key .startswith ("sentry-" ):
238238 updated_headers ["headers" ][key ] = value
239239
240+ # Preserve user-provided custom headers in the inner "headers" dict
241+ # so they survive to task.request.headers on the worker (celery#4875).
242+ for key , value in original_headers .items ():
243+ if key != "headers" and key not in updated_headers ["headers" ]:
244+ updated_headers ["headers" ][key ] = value
245+
240246 return updated_headers
241247
242248
Original file line number Diff line number Diff 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" )
851880def test_placeholder ():
852881 """Forked tests must not come last in the module.
You can’t perform that action at this time.
0 commit comments