René's URL Explorer Experiment


Title: Celery integration strips user custom headers from `task.request.headers` · Issue #5566 · getsentry/sentry-python · GitHub

Open Graph Title: Celery integration strips user custom headers from `task.request.headers` · Issue #5566 · getsentry/sentry-python

X Title: Celery integration strips user custom headers from `task.request.headers` · Issue #5566 · getsentry/sentry-python

Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.53.0 Steps to Reproduce Celery integration strips user custom headers from task.request.headers Environment Package Version sentry-sdk 2.53.0 celery 5.6.2 python 3.12 Description T...

Open Graph Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.53.0 Steps to Reproduce Celery integration strips user custom headers from task.request.headers Environment Package Version sentry-sdk 2.53....

X Description: How do you use Sentry? Sentry Saas (sentry.io) Version 2.53.0 Steps to Reproduce Celery integration strips user custom headers from task.request.headers Environment Package Version sentry-sdk 2.53....

Opengraph URL: https://github.com/getsentry/sentry-python/issues/5566

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Celery integration strips user custom headers from `task.request.headers`","articleBody":"### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Version\n\n2.53.0\n\n### Steps to Reproduce\n\n# Celery integration strips user custom headers from `task.request.headers`\n\n## Environment\n\n| Package    | Version |\n|------------|---------|\n| sentry-sdk | 2.53.0  |\n| celery     | 5.6.2   |\n| python     | 3.12    |\n\n## Description\n\nThe Celery integration's `_update_celery_task_headers` function in `sentry_sdk/integrations/celery/__init__.py` strips user-provided custom headers from reaching `task.request.headers` on the worker side.\n\nCelery has a long-standing bug ([celery/celery#4875](https://github.com/celery/celery/issues/4875)) where task headers placed at the top level of the message are not reliably accessible via `task.request.headers`. Instead, only headers nested inside the inner `\"headers\"` dict (i.e., `message[\"headers\"][\"headers\"]`) survive to `task.request.headers` on the worker.\n\nThe Sentry integration is aware of this issue -- there is even a comment referencing `#4875` at line 219. The integration correctly places its own tracing headers (`sentry-trace`, `baggage`, etc.) into the inner `\"headers\"` dict. However, it does **not** copy user-provided custom headers into the inner dict, effectively making them invisible to `task.request.headers` on the worker.\n\n## Root Cause\n\nIn `sentry_sdk/integrations/celery/__init__.py`, the `_update_celery_task_headers` function (lines 162-234):\n\n**Step 1** (line 169): The function copies all original headers (including user custom headers) into `updated_headers`:\n\n```python\nupdated_headers = original_headers.copy()\n```\n\n**Step 2** (line 224): It creates the inner `\"headers\"` dict and populates it with only Sentry's tracing headers (`sentry-trace`, `baggage`, etc.):\n\n```python\nupdated_headers.setdefault(\"headers\", {}).update(headers)\n```\n\nHere, `headers` is the dict returned by `sentry_sdk.get_isolation_scope().iter_trace_propagation_headers()` -- it contains only Sentry tracing headers, not user custom headers.\n\n**Step 3** (lines 230-232): It then copies only keys prefixed with `\"sentry-\"` from the outer dict into the inner dict:\n\n```python\nfor key, value in updated_headers.items():\n    if key.startswith(\"sentry-\"):\n        updated_headers[\"headers\"][key] = value\n```\n\nThis loop explicitly filters to `\"sentry-\"` prefixed keys. Any user-provided custom headers (e.g., `my_custom_key`, `correlation_id`, `tenant_id`) are left stranded at the top level and never copied into the inner `\"headers\"` dict.\n\n**Result:** Due to celery/celery#4875, the worker's `task.request.headers` only reflects the inner dict contents. User custom headers are silently dropped.\n\n## Steps to Reproduce\n\n### Minimal reproduction script\n\n```python\n# repro.py\nimport sentry_sdk\nfrom sentry_sdk.integrations.celery import CeleryIntegration\nfrom celery import Celery\nfrom celery.signals import task_prerun\n\nsentry_sdk.init(\n    dsn=\"https://examplePublicKey@o0.ingest.sentry.io/0\",\n    integrations=[CeleryIntegration()],\n    traces_sample_rate=1.0,\n)\n\napp = Celery(\"repro\", broker=\"redis://localhost:6379/0\")\n\n\n@app.task(bind=True)\ndef my_task(self):\n    # This is what users expect to work:\n    value_from_headers = self.request.headers.get(\"my_custom_key\")\n    print(f\"self.request.headers.get('my_custom_key') = {value_from_headers}\")\n    # ^ Returns None (BUG)\n\n    # Workaround -- the value IS present at the top level of the request:\n    value_from_attr = getattr(self.request, \"my_custom_key\", None)\n    print(f\"getattr(self.request, 'my_custom_key') = {value_from_attr}\")\n    # ^ Returns 'my_value'\n\n    # Show what's actually in headers:\n    print(f\"self.request.headers = {self.request.headers}\")\n    # ^ Only contains sentry-trace, baggage, sentry-task-enqueued-time, etc.\n\n\nif __name__ == \"__main__\":\n    # Send the task with a custom header\n    my_task.apply_async(args=[], headers={\"my_custom_key\": \"my_value\"})\n    print(\"Task sent with headers={'my_custom_key': 'my_value'}\")\n```\n\n### Steps\n\n1. Start a Redis instance (or any broker).\n2. Run the worker: `celery -A repro worker --loglevel=info`\n3. Send the task: `python repro.py`\n4. Observe the worker output:\n   ```\n   self.request.headers.get('my_custom_key') = None\n   getattr(self.request, 'my_custom_key') = my_value\n   self.request.headers = {'sentry-trace': '...', 'baggage': '...', 'sentry-task-enqueued-time': ...}\n   ```\n5. Disable the Sentry integration (remove `sentry_sdk.init(...)`) and repeat.\n6. Observe the worker output now correctly shows:\n   ```\n   self.request.headers.get('my_custom_key') = my_value\n   ```\n\n## Expected Behavior\n\nCustom headers passed via the `headers=` kwarg should be available in `task.request.headers` on the worker, regardless of whether the Sentry integration is enabled. The Sentry integration should not alter the visibility of user-provided headers.\n\n## Actual Behavior\n\nWhen the Sentry integration is enabled, user custom headers are silently dropped from `task.request.headers`. They exist at the top level of the message (accessible via `getattr(self.request, 'key')`) but not in the inner `\"headers\"` dict that Celery exposes as `task.request.headers`.\n\n## Suggested Fix\n\nIn `_update_celery_task_headers`, after copying Sentry's own headers into the inner dict, also copy user-provided custom headers from `original_headers` so they survive the celery/celery#4875 behavior.\n\n### Option A: Copy all non-internal keys from original headers\n\nAfter line 232, add:\n\n```python\n# Preserve user-provided custom headers in the inner \"headers\" dict\n# so they survive to task.request.headers on the worker (celery#4875).\nfor key, value in original_headers.items():\n    if key != \"headers\" and key not in updated_headers.get(\"headers\", {}):\n        updated_headers[\"headers\"][key] = value\n```\n\n### Option B: Remove the `\"sentry-\"` prefix filter\n\nReplace the loop at lines 230-232:\n\n```python\n# Before (only copies sentry-* keys):\nfor key, value in updated_headers.items():\n    if key.startswith(\"sentry-\"):\n        updated_headers[\"headers\"][key] = value\n\n# After (copies all non-internal keys):\nfor key, value in updated_headers.items():\n    if key != \"headers\" and not isinstance(value, dict):\n        updated_headers[\"headers\"][key] = value\n```\n\nOption A is preferred because it uses `original_headers` as the source (avoiding accidentally copying Sentry's top-level additions like `sentry-task-enqueued-time` twice) and respects any keys the inner dict already has via `setdefault`.\n\n## Workaround\n\nUntil this is fixed, users can access custom headers via top-level request attributes instead of `task.request.headers`:\n\n```python\n@app.task(bind=True)\ndef my_task(self):\n    # Instead of:\n    # value = self.request.headers.get(\"my_custom_key\")\n\n    # Use:\n    value = getattr(self.request, \"my_custom_key\", None)\n```\n\nNote: this workaround is fragile and relies on Celery's internal behavior of flattening top-level message headers into request attributes.\n\n## References\n\n- [celery/celery#4875](https://github.com/celery/celery/issues/4875) -- the upstream Celery bug that necessitates the inner `\"headers\"` dict workaround\n- [`sentry_sdk/integrations/celery/__init__.py` lines 162-234](https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/celery/__init__.py) -- the `_update_celery_task_headers` function\n\n\n### Expected Result\n\nThe headers included on the request are preserved.\n\n### Actual Result\n\nThe headers included on the request are stripped out.","author":{"url":"https://github.com/jamesrusso","@type":"Person","name":"jamesrusso"},"datePublished":"2026-03-01T20:24:19Z","url":"https://github.com/getsentry/sentry-python/issues/5566"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:c6239b42-59aa-b999-652c-940c6c43d452
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD50A:28815B:8A45A2:BA6CDC:6B00E39F
html-safe-nonce858ee5fad00c6ef3d52f35f97497a01a2bb83cfe36fff11ea25b801143688183
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTBBOjI4ODE1Qjo4QTQ1QTI6QkE2Q0RDOjZCMDBFMzlGIiwidmlzaXRvcl9pZCI6IjI0MzI5NzkwNTk0OTIzMTYwNjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac2f8a6afae3a45e4e649499279659dc5fa90dca21e79f3f4e3520cebace428ac3
hovercard-subject-tagissue:4008192609
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-python/5566/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8e827c21fd3469911fc1cd525a01872c83c12a30d1bf8212a655dcff33a99b2c/getsentry/sentry-python/issues/5566
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8e827c21fd3469911fc1cd525a01872c83c12a30d1bf8212a655dcff33a99b2c/getsentry/sentry-python/issues/5566
og:image:altHow do you use Sentry? Sentry Saas (sentry.io) Version 2.53.0 Steps to Reproduce Celery integration strips user custom headers from task.request.headers Environment Package Version sentry-sdk 2.53....
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejamesrusso
hostnamegithub.com
expected-hostnamegithub.com
None27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14
turbo-cache-controlno-preview
go-importgithub.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git
octolytics-dimension-user_id1396951
octolytics-dimension-user_logingetsentry
octolytics-dimension-repository_id137814779
octolytics-dimension-repository_nwogetsentry/sentry-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id137814779
octolytics-dimension-repository_network_root_nwogetsentry/sentry-python
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release624179fb8b88d26ba5bba08894eb60a1c5a1d93d
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/getsentry/sentry-python/issues/5566#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fissues%2F5566
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgetsentry%2Fsentry-python%2Fissues%2F5566
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=getsentry%2Fsentry-python
Reloadhttps://github.com/getsentry/sentry-python/issues/5566
Reloadhttps://github.com/getsentry/sentry-python/issues/5566
Reloadhttps://github.com/getsentry/sentry-python/issues/5566
getsentryhttps://github.com/getsentry
sentry-pythonhttps://github.com/getsentry/sentry-python
Codehttps://github.com/getsentry/sentry-python
Issues345 (345)https://github.com/getsentry/sentry-python/issues
Pull requests42 (42)https://github.com/getsentry/sentry-python/pulls
Discussionshttps://github.com/getsentry/sentry-python/discussions
Actionshttps://github.com/getsentry/sentry-python/actions
Security and quality2 (2)https://github.com/getsentry/sentry-python/security
Insightshttps://github.com/getsentry/sentry-python/insights
https://github.com/getsentry/sentry-python
New issuehttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/5566
New issuehttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/5566
#5581https://github.com/getsentry/sentry-python/pull/5581
Celery integration strips user custom headers from task.request.headershttps://github.com/getsentry/sentry-python/issues/5566#top
#5581https://github.com/getsentry/sentry-python/pull/5581
https://github.com/sentrivana
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
Integration: Celeryhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Integration%3A%20Celery%22
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
https://github.com/jamesrusso
jamesrussohttps://github.com/jamesrusso
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566#issue-4008192609
celery/celery#4875https://github.com/celery/celery/issues/4875
celery/celery#4875https://github.com/celery/celery/issues/4875
celery/celery#4875https://github.com/celery/celery/issues/4875
celery/celery#4875https://github.com/celery/celery/issues/4875
sentry_sdk/integrations/celery/__init__.py lines 162-234https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/celery/__init__.py
jamesrussohttps://github.com/jamesrusso
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23175224106
getsantryhttps://github.com/apps/getsantry
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23175224367
https://github.com/apps/linear
linearhttps://github.com/apps/linear
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566#issuecomment-3980958197
Linearhttps://linear.app
PY-2118https://linear.app/getsentry/issue/PY-2118/celery-integration-strips-user-custom-headers-from-taskrequestheaders
getsantryhttps://github.com/apps/getsantry
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23175225001
getsantryhttps://github.com/apps/getsantry
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
on Mar 1, 2026https://github.com/getsentry/sentry-python/issues/5566
https://github.com/ericapisani
ericapisanihttps://github.com/ericapisani
on Mar 2, 2026https://github.com/getsentry/sentry-python/issues/5566#issuecomment-3984367053
@jamesrussohttps://github.com/jamesrusso
getsantryhttps://github.com/apps/getsantry
Waiting for: Product Ownerhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Waiting%20for%3A%20Product%20Owner%22
on Mar 2, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23192970387
getsantryhttps://github.com/apps/getsantry
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
on Mar 2, 2026https://github.com/getsentry/sentry-python/issues/5566
sentrivanahttps://github.com/sentrivana
Integration: Celeryhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Integration%3A%20Celery%22
on Mar 3, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23222240451
sentrivanahttps://github.com/sentrivana
on Mar 3, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23222479552
sentrivanahttps://github.com/sentrivana
completedhttps://github.com/getsentry/sentry-python/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#5581https://github.com/getsentry/sentry-python/pull/5581
on Mar 3, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23228093683
pullhttps://github.com/apps/pull
on Mar 3, 2026https://github.com/getsentry/sentry-python/issues/5566#event-23236632606
fix(celery): Propagate user-set headers (getsentry#5581)https://github.com/AKJUS/sentry-python/commit/35070ce497bd01707d76dd3b6ada9ef4506176c5
getsentry#5581https://github.com/getsentry/sentry-python/pull/5581
35070cehttps://github.com/AKJUS/sentry-python/commit/35070ce497bd01707d76dd3b6ada9ef4506176c5
Sign up for freehttps://github.com/signup?return_to=https://github.com/getsentry/sentry-python/issues/5566
Sign in to commenthttps://github.com/login?return_to=https://github.com/getsentry/sentry-python/issues/5566
sentrivanahttps://github.com/sentrivana
Bughttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Bug%22
Integration: Celeryhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Integration%3A%20Celery%22
PythonSDKhttps://github.com/getsentry/sentry-python/issues?q=state%3Aopen%20label%3A%22Python%22
GitHub Issues with 👀 3https://github.com/orgs/getsentry/projects/227
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.