Skip to content

wip: update queries to fetch the new metric #95923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/sentry/release_health/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"crash_free_rate(user)",
"anr_rate()",
"foreground_anr_rate()",
"unhandled_rate(session)",
"unhandled_rate(user)",
]

GroupByFieldName = Literal[
Expand Down Expand Up @@ -182,6 +184,8 @@ class ReleaseHealthOverview(TypedDict, total=False):
duration_p50: float | None
duration_p90: float | None
stats: Mapping[StatsPeriod, ReleaseHealthStats]
sessions_unhandled: int
handled_sessions: float | None


class CrashFreeBreakdown(TypedDict):
Expand All @@ -202,6 +206,7 @@ class UserCounts(TypedDict):
users_healthy: int
users_crashed: int
users_abnormal: int
users_unhandled: int
users_errored: int


Expand All @@ -214,6 +219,7 @@ class SessionCounts(TypedDict):
sessions_healthy: int
sessions_crashed: int
sessions_abnormal: int
sessions_unhandled: int
sessions_errored: int


Expand Down
30 changes: 27 additions & 3 deletions src/sentry/release_health/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def _get_errored_sessions_for_overview(
end: datetime,
) -> Mapping[tuple[int, str], int]:
"""
Count of errored sessions, incl fatal (abnormal, crashed) sessions,
Count of errored sessions, incl fatal (abnormal, unhandled, crashed) sessions,
excl errored *preaggregated* sessions
"""
project_ids = [p.id for p in projects]
Expand Down Expand Up @@ -774,12 +774,13 @@ def _get_session_by_status_for_overview(
end: datetime,
) -> Mapping[tuple[int, str, str], int]:
"""
Counts of init, abnormal and crashed sessions, purpose-built for overview
Counts of init, abnormal, unhandled and crashed sessions, purpose-built for overview
"""
project_ids = [p.id for p in projects]

select = [
MetricField(metric_mri=SessionMRI.ABNORMAL.value, alias="abnormal", op=None),
MetricField(metric_mri=SessionMRI.UNHANDLED.value, alias="unhandled", op=None),
MetricField(metric_mri=SessionMRI.CRASHED.value, alias="crashed", op=None),
MetricField(metric_mri=SessionMRI.ALL.value, alias="init", op=None),
MetricField(
Expand Down Expand Up @@ -820,7 +821,13 @@ def _get_session_by_status_for_overview(
release = by.get("release")

totals = group.get("totals", {})
for status in ["abnormal", "crashed", "init", "errored_preaggr"]:
for status in [
"abnormal",
"unhandled",
"crashed",
"init",
"errored_preaggr",
]:
value = totals.get(status)
if value is not None and value != 0.0:
ret_val[(proj_id, release, status)] = value
Expand Down Expand Up @@ -1037,10 +1044,13 @@ def get_release_health_data_overview(
if not has_health_data and summary_stats_period != "90d":
fetch_has_health_data_releases.add((project_id, release))

sessions_unhandled = rv_sessions.get((project_id, release, "unhandled"), 0)
sessions_crashed = rv_sessions.get((project_id, release, "crashed"), 0)

users_crashed = rv_users.get((project_id, release, "crashed_users"), 0)

sum_unhandled = sessions_unhandled + sessions_crashed

rv_row = rv[project_id, release] = {
"adoption": adoption_info.get("adoption"),
"sessions_adoption": adoption_info.get("sessions_adoption"),
Expand All @@ -1051,10 +1061,14 @@ def get_release_health_data_overview(
"total_sessions": total_sessions,
"total_users": total_users,
"has_health_data": has_health_data,
"sessions_unhandled": sessions_unhandled,
"sessions_crashed": sessions_crashed,
"crash_free_users": (
100 - users_crashed / total_users * 100 if total_users else None
),
"handled_sessions": (
100 - sum_unhandled / float(total_sessions) * 100 if total_sessions else None
),
"crash_free_sessions": (
100 - sessions_crashed / float(total_sessions) * 100 if total_sessions else None
),
Expand All @@ -1063,6 +1077,7 @@ def get_release_health_data_overview(
rv_errored_sessions.get((project_id, release), 0)
+ rv_sessions.get((project_id, release, "errored_preaggr"), 0)
- sessions_crashed
- sessions_unhandled
- rv_sessions.get((project_id, release, "abnormal"), 0),
),
"duration_p50": None,
Expand Down Expand Up @@ -1427,6 +1442,9 @@ def get_project_release_stats(
MetricField(
metric_mri=SessionMRI.CRASHED_USER.value, alias="users_crashed", op=None
),
MetricField(
metric_mri=SessionMRI.UNHANDLED_USER.value, alias="users_unhandled", op=None
),
MetricField(
metric_mri=SessionMRI.ERRORED_USER.value, alias="users_errored", op=None
),
Expand All @@ -1441,6 +1459,11 @@ def get_project_release_stats(
metric_mri=SessionMRI.ABNORMAL.value, alias="sessions_abnormal", op=None
),
MetricField(metric_mri=SessionMRI.CRASHED.value, alias="sessions_crashed", op=None),
MetricField(
metric_mri=SessionMRI.UNHANDLED.value,
alias="sessions_unhandled",
op=None,
),
MetricField(metric_mri=SessionMRI.ERRORED.value, alias="sessions_errored", op=None),
MetricField(metric_mri=SessionMRI.HEALTHY.value, alias="sessions_healthy", op=None),
]
Expand Down Expand Up @@ -1500,6 +1523,7 @@ def get_project_release_stats(
f"{stat}": 0,
f"{stat}_abnormal": 0,
f"{stat}_crashed": 0,
f"{stat}_unhandled": 0,
f"{stat}_errored": 0,
f"{stat}_healthy": 0,
}
Expand Down
12 changes: 10 additions & 2 deletions src/sentry/release_health/metrics_sessions_v2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" This module offers the same functionality as sessions_v2, but pulls its data
"""This module offers the same functionality as sessions_v2, but pulls its data
from the `metrics` dataset instead of `sessions`.

Do not call this module directly. Use the `release_health` service instead. """
Do not call this module directly. Use the `release_health` service instead."""

import logging
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -73,6 +73,7 @@ class SessionStatus(Enum):
CRASHED = "crashed"
ERRORED = "errored"
HEALTHY = "healthy"
UNHANDLED = "unhandled"


ALL_STATUSES = frozenset(iter(SessionStatus))
Expand Down Expand Up @@ -242,6 +243,7 @@ def _get_metric_fields(
self.status_to_metric_field[SessionStatus.ABNORMAL],
self.status_to_metric_field[SessionStatus.CRASHED],
self.status_to_metric_field[SessionStatus.ERRORED],
self.status_to_metric_field[SessionStatus.UNHANDLED],
]
return [self.get_all_field()]

Expand All @@ -265,6 +267,7 @@ class SumSessionField(CountField):
SessionStatus.ABNORMAL: MetricField(None, SessionMRI.ABNORMAL.value),
SessionStatus.CRASHED: MetricField(None, SessionMRI.CRASHED.value),
SessionStatus.ERRORED: MetricField(None, SessionMRI.ERRORED.value),
SessionStatus.UNHANDLED: MetricField(None, SessionMRI.UNHANDLED.value),
None: MetricField(None, SessionMRI.ALL.value),
}

Expand Down Expand Up @@ -298,6 +301,7 @@ def __init__(
SessionStatus.ABNORMAL: MetricField(None, SessionMRI.ABNORMAL_USER.value),
SessionStatus.CRASHED: MetricField(None, SessionMRI.CRASHED_USER.value),
SessionStatus.ERRORED: MetricField(None, SessionMRI.ERRORED_USER.value),
SessionStatus.UNHANDLED: MetricField(None, SessionMRI.UNHANDLED_USER.value),
None: MetricField(None, SessionMRI.ALL_USER.value),
}

Expand Down Expand Up @@ -335,6 +339,8 @@ class SimpleForwardingField(Field):
"""

field_name_to_metric_name = {
"unhandled_rate(session)": SessionMRI.UNHANDLED_RATE,
"unhandled_rate(user)": SessionMRI.UNHANDLED_USER_RATE,
"crash_rate(session)": SessionMRI.CRASH_RATE,
"crash_rate(user)": SessionMRI.CRASH_USER_RATE,
"crash_free_rate(session)": SessionMRI.CRASH_FREE_RATE,
Expand Down Expand Up @@ -373,6 +379,8 @@ def _get_metric_fields(
"p95(session.duration)": DurationField,
"p99(session.duration)": DurationField,
"max(session.duration)": DurationField,
"unhandled_rate(session)": SimpleForwardingField,
"unhandled_rate(user)": SimpleForwardingField,
"crash_rate(session)": SimpleForwardingField,
"crash_rate(user)": SimpleForwardingField,
"crash_free_rate(session)": SimpleForwardingField,
Expand Down
14 changes: 14 additions & 0 deletions src/sentry/snuba/metrics/fields/snql.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ def all_users(org_id: int, metric_ids: Sequence[int], alias: str | None = None)
return uniq_aggregation_on_metric(metric_ids, alias)


def unhandled_sessions(
org_id: int, metric_ids: Sequence[int], alias: str | None = None
) -> Function:
return _counter_sum_aggregation_on_session_status_factory(
org_id, session_status="unhandled", metric_ids=metric_ids, alias=alias
)


def unhandled_users(org_id: int, metric_ids: Sequence[int], alias: str | None = None) -> Function:
return _set_uniq_aggregation_on_session_status_factory(
org_id, session_status="unhandled", metric_ids=metric_ids, alias=alias
)


def crashed_sessions(org_id: int, metric_ids: Sequence[int], alias: str | None = None) -> Function:
return _counter_sum_aggregation_on_session_status_factory(
org_id, session_status="crashed", metric_ids=metric_ids, alias=alias
Expand Down
14 changes: 13 additions & 1 deletion src/sentry/snuba/metrics/naming_layer/mri.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,32 @@ class SessionMRI(Enum):
ERRORED_PREAGGREGATED = "e:sessions/error.preaggr@none"
ERRORED_SET = "e:sessions/error.unique@none"
ERRORED_ALL = "e:sessions/all_errored@none"
HANDLED = "e:sessions/handled.unique@none" # all sessions excluding handled and crashed
UNHANDLED = "e:sessions/unhandled@none" # unhandled, does not include crashed
CRASHED_AND_ABNORMAL = "e:sessions/crashed_abnormal@none"
CRASHED = "e:sessions/crashed@none"
CRASH_FREE = "e:sessions/crash_free@none"
ABNORMAL = "e:sessions/abnormal@none"
HANDLED_RATE = "e:sessions/handled_rate@ratio" # all sessions excluding handled and crashed
UNHANDLED_RATE = "e:sessions/unhandled_rate@ratio" # unhandled, does not include crashed
CRASH_RATE = "e:sessions/crash_rate@ratio"
CRASH_FREE_RATE = "e:sessions/crash_free_rate@ratio"
CRASH_FREE_RATE = "e:sessions/crash_free_rate@ratio" # includes handled and unhandled
ALL_USER = "e:sessions/user.all@none"
HEALTHY_USER = "e:sessions/user.healthy@none"
ERRORED_USER = "e:sessions/user.errored@none"
ERRORED_USER_ALL = "e:sessions/user.all_errored@none"
HANDLED_USER = "e:sessions/user.handled@none" # all sessions excluding handled and crashed
UNHANDLED_USER = "e:sessions/user.unhandled@none" # unhandled, does not include crashed
CRASHED_AND_ABNORMAL_USER = "e:sessions/user.crashed_abnormal@none"
CRASHED_USER = "e:sessions/user.crashed@none"
CRASH_FREE_USER = "e:sessions/user.crash_free@none"
ABNORMAL_USER = "e:sessions/user.abnormal@none"
HANDLED_USER_RATE = (
"e:sessions/user.handled_rate@ratio" # all sessions excluding handled and crashed
)
UNHANDLED_USER_RATE = (
"e:sessions/user.unhandled_rate@ratio" # unhandled, does not include crashed
)
CRASH_USER_RATE = "e:sessions/user.crash_rate@ratio"
CRASH_FREE_USER_RATE = "e:sessions/user.crash_free_rate@ratio"
ANR_USER = "e:sessions/user.anr@none"
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/snuba/metrics/naming_layer/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SessionMetricKey(Enum):
DURATION = "session.duration"
ALL = "session.all"
ABNORMAL = "session.abnormal"
UNHANDLED = "session.unhandled"
CRASHED = "session.crashed"
CRASH_FREE = "session.crash_free"
ERRORED = "session.errored"
Expand All @@ -45,6 +46,7 @@ class SessionMetricKey(Enum):
CRASH_FREE_RATE = "session.crash_free_rate"
ALL_USER = "session.all_user"
ABNORMAL_USER = "session.abnormal_user"
UNHANDLED_USER = "session.unhandled_user"
CRASHED_USER = "session.crashed_user"
CRASH_FREE_USER = "session.crash_free_user"
ERRORED_USER = "session.errored_user"
Expand Down
18 changes: 15 additions & 3 deletions src/sentry/snuba/sessions_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,16 @@ def extract_from_row(self, row, group):
return max(healthy_sessions, 0)
if status == "abnormal":
return row["sessions_abnormal"]
if status == "unhandled":
return row["sessions_unhandled"]
if status == "crashed":
return row["sessions_crashed"]
if status == "errored":
errored_sessions = (
row["sessions_errored"] - row["sessions_crashed"] - row["sessions_abnormal"]
row["sessions_errored"]
- row["sessions_unhandled"]
- row["sessions_crashed"]
- row["sessions_abnormal"]
)
return max(errored_sessions, 0)
return 0
Expand All @@ -133,7 +138,7 @@ def extract_from_row(self, row, group):
class UsersField:
def get_snuba_columns(self, raw_groupby):
if "session.status" in raw_groupby:
return ["users", "users_abnormal", "users_crashed", "users_errored"]
return ["users", "users_abnormal", "users_crashed", "users_errored", "users_unhandled"]
return ["users"]

def extract_from_row(self, row, group):
Expand All @@ -147,10 +152,17 @@ def extract_from_row(self, row, group):
return max(healthy_users, 0)
if status == "abnormal":
return row["users_abnormal"]
if status == "unhandled":
return row["users_unhandled"]
if status == "crashed":
return row["users_crashed"]
if status == "errored":
errored_users = row["users_errored"] - row["users_crashed"] - row["users_abnormal"]
errored_users = (
row["users_errored"]
- row["users_crashed"]
- row["users_abnormal"]
- row["users_unhandled"]
)
return max(errored_users, 0)
return 0

Expand Down
2 changes: 1 addition & 1 deletion src/sentry/testutils/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ def push(mri: str, tags, value):
elif not user_is_nil:
push(SessionMRI.RAW_USER.value, {}, user)

if status in ("abnormal", "crashed"): # fatal
if status in ("abnormal", "unhandled", "crashed"): # fatal
push(SessionMRI.RAW_SESSION.value, {"session.status": status}, +1)
if not user_is_nil:
push(SessionMRI.RAW_USER.value, {"session.status": status}, user)
Expand Down
5 changes: 5 additions & 0 deletions src/sentry/web/frontend/debug/debug_chart_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@
"totals": {"sum(session)": 963},
"series": {"sum(session)": [185, 170, 147, 170, 105, 133, 53]},
},
{
"by": {"session.status": "unhandled"},
"totals": {"sum(session)": 0},
"series": {"sum(session)": [0, 0, 0, 0, 0, 0, 0]},
},
{
"by": {"session.status": "crashed"},
"totals": {"sum(session)": 401},
Expand Down
3 changes: 3 additions & 0 deletions static/app/types/organization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,16 @@ export enum SessionFieldWithOperation {
SESSIONS = 'sum(session)',
USERS = 'count_unique(user)',
DURATION = 'p50(session.duration)',
UNHANDLED = 'sum(session.unhandled)',
UNHANDLED_USER = 'count_unique(session.unhandled_user)',
CRASH_FREE_RATE_USERS = 'crash_free_rate(user)',
CRASH_FREE_RATE_SESSIONS = 'crash_free_rate(session)',
}

export enum SessionStatus {
HEALTHY = 'healthy',
ABNORMAL = 'abnormal',
UNHANDLED = 'unhandled',
ERRORED = 'errored',
CRASHED = 'crashed',
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,7 @@ def test_errored_sessions(self):
for tag_value, value in (
("errored_preaggr", 10),
("crashed", 2),
("unhandled", 1),
("abnormal", 4),
("init", 15),
):
Expand All @@ -1787,7 +1788,7 @@ def test_errored_sessions(self):
interval="1m",
)
group = response.data["groups"][0]
assert group["totals"]["session.errored"] == 7
assert group["totals"]["session.errored"] == 8
assert group["series"]["session.errored"] == [0, 4, 0, 0, 0, 3]

def test_orderby_composite_entity_derived_metric(self):
Expand Down Expand Up @@ -1839,6 +1840,10 @@ def test_abnormal_sessions(self):
assert bar_group["totals"] == {"session.abnormal": 3}
assert bar_group["series"] == {"session.abnormal": [0, 0, 0, 3, 0, 0]}

def test_unhandled_sessions(self):
# TODO: ryan953
pass

def test_crashed_user_sessions(self):
for tag_value, values in (
("foo", [1, 2, 4]),
Expand Down
Loading
Loading