Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/sentry/tasks/post_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from sentry.utils.sdk_crashes.sdk_crash_detection_config import build_sdk_crash_detection_configs
from sentry.utils.services import build_instance_from_options_of_type
from sentry.utils.tracing import start_span, trace
from sentry.viewer_context import ActorType, ViewerContext, viewer_context_scope

if TYPE_CHECKING:
from sentry.eventstream.base import GroupState
Expand Down Expand Up @@ -610,41 +611,48 @@ def get_event_raise_exception() -> Event:
Organization.objects.get_from_cache(id=event.project.organization_id),
)

is_reprocessed = is_reprocessed_event(event.data)
sentry_sdk.set_tag("is_reprocessed", is_reprocessed)
sentry_sdk.set_attribute("is_reprocessed", is_reprocessed)

metric_tags = {}
if group_id:
group_state: GroupState = {
"id": group_id,
"is_new": is_new,
"is_regression": bool(is_regression),
"is_new_group_environment": is_new_group_environment,
}
with viewer_context_scope(

@cvxluo cvxluo Aug 7, 2026

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.

can we wrap only the post process jobs that need viewer context?

@sehr-m sehr-m Aug 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The thought process was that setting it at the entrypoint means any Seer call added to the post-process pipeline in the future automatically has ViewerContext. The current issue is that when the addition is needed at the call site it gets very fragmented and poorly maintained because people forget to add it, whereas setting a contextvar for the other pipeline steps is pretty much free. (Also this is mainly for seer though it has uses for security etc within sentry as well)

@cvxluo cvxluo Aug 7, 2026

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.

there was some discussion here about removing some of these Seer calls from the hot path here: #121462 (comment)

change looks ok as is. i'm a little concerned since this means that any changes to how ViewerContext works or any performance / latency problem there has a chance of breaking post process, so i'd like to see if we can avoid it.

ViewerContext(
organization_id=event.project.organization_id,
project_id=event.project_id,
actor_type=ActorType.SYSTEM,
)
):
is_reprocessed = is_reprocessed_event(event.data)
sentry_sdk.set_tag("is_reprocessed", is_reprocessed)
sentry_sdk.set_attribute("is_reprocessed", is_reprocessed)

metric_tags = {}
if group_id:
group_state: GroupState = {
"id": group_id,
"is_new": is_new,
"is_regression": bool(is_regression),
"is_new_group_environment": is_new_group_environment,
}

group_event = update_event_group(event, group_state)
bind_organization_context(event.project.organization)
_capture_event_stats(event)
group_event = update_event_group(event, group_state)
bind_organization_context(event.project.organization)
_capture_event_stats(event)

group_event.occurrence = occurrence
group_event.occurrence = occurrence

run_post_process_job(
{
"event": group_event,
"group_state": group_state,
"is_reprocessed": is_reprocessed,
"has_reappeared": bool(not group_state["is_new"]),
"has_escalated": kwargs.get("has_escalated", False),
}
)
metric_tags["occurrence_type"] = group_event.group.issue_type.slug
run_post_process_job(
{
"event": group_event,
"group_state": group_state,
"is_reprocessed": is_reprocessed,
"has_reappeared": bool(not group_state["is_new"]),
"has_escalated": kwargs.get("has_escalated", False),
}
)
metric_tags["occurrence_type"] = group_event.group.issue_type.slug

track_event_since_received(
step="end_post_process",
event_data=event.data,
tags=metric_tags,
)
track_event_since_received(
step="end_post_process",
event_data=event.data,
tags=metric_tags,
)


def run_post_process_job(job: PostProcessJob) -> None:
Expand Down
Loading