Skip to content

feat(taskworker): Add ViewerContext propagation via context hooks#112217

Draft
gricha wants to merge 1 commit intomasterfrom
gricha/feat/viewer-context-taskbroker-hook
Draft

feat(taskworker): Add ViewerContext propagation via context hooks#112217
gricha wants to merge 1 commit intomasterfrom
gricha/feat/viewer-context-taskbroker-hook

Conversation

@gricha
Copy link
Copy Markdown
Member

@gricha gricha commented Apr 3, 2026

Add ViewerContextHook that implicitly propagates ViewerContext through TaskBroker task headers.

On dispatch: the hook reads from the get_viewer_context() contextvar and injects sentry-viewer-org, sentry-viewer-user, and sentry-viewer-actor into task activation headers. This happens automatically in create_activation() — no callsite changes needed.

On execution: the hook reads those headers back and wraps the task callable in viewer_context_scope(), making get_viewer_context() available inside every task.

This is the Sentry-side implementation. The taskbroker-client ContextHook protocol that powers this lives in getsentry/taskbroker#587 — this PR depends on that being merged and the taskbroker-client dependency bumped.

Part of the ViewerContext RFC.

Implement ViewerContextHook that propagates ViewerContext through
TaskBroker task headers. On dispatch, the hook reads from the
contextvar and injects org_id, user_id, and actor_type into task
headers. On execution, it restores the ViewerContext from headers
into a viewer_context_scope.

This enables implicit identity propagation through async tasks
without requiring callsites to manually pass user context.

Depends on getsentry/taskbroker#587 for the ContextHook protocol.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 3, 2026
metrics_class=SentryMetricsBackend(),
router_class=SentryRouter(),
at_most_once_store=DjangoCacheAtMostOnceStore(cache),
context_hooks=[ViewerContextHook()],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActorType enum conversion can raise ValueError during version skew

ActorType(actor) on line 156 of adapters.py converts the header value to an enum without catching ValueError. During rolling deployments, if a producer with a newer ActorType value sends tasks to a consumer with an older enum definition, the task execution will crash. This matches Check 4 (Value Validation Errors) pattern: "Integer enum lookups without catching ValueError".

Verification

Read src/sentry/taskworker/adapters.py to trace ViewerContextHook implementation. Verified ActorType is a StrEnum (src/sentry/viewer_context.py:28) with 4 values. The on_execute method (line 156) calls ActorType(actor) without try/except. Cross-process version skew during deployments could cause ValueError if new enum values are added.

Identified by Warden sentry-backend-bugs · 5Z2-2MC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant