Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(error-tracking): keep the severity filter on the optimized query shape (#129230)

James Gill118096873853
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index 02a05f471968..e007a08821ce 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -24636,6 +24636,17 @@
"description": "Free-text search across exception type, message, and stack frames.",
"type": "string"
},
+ "severity": {
+ "anyOf": [
+ {
+ "$ref": "#/definitions/ErrorTrackingQueryIssueSeverity"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Filter by issue severity."
+ },
"status": {
"$ref": "#/definitions/ErrorTrackingQueryStatus",
"description": "Filter by issue status."
diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.ts
index 44bd08e61b41..4011cbc1e980 100644
--- a/frontend/src/queries/schema/schema-general.ts
+++ b/frontend/src/queries/schema/schema-general.ts
@@ -4558,6 +4558,8 @@ export interface ErrorTrackingQuery extends DataNode<ErrorTrackingQueryResponse>
dateRange: DateRange
/** Filter by issue status. */
status?: ErrorTrackingQueryStatus
+ /** Filter by issue severity. */
+ severity?: ErrorTrackingQueryIssueSeverity | null
assignee?: ErrorTrackingIssueAssignee | null
filterGroup?: PropertyGroupFilter
/** Whether to filter out test accounts. */
diff --git a/posthog/schema.py b/posthog/schema.py
index 14ec800ee3fb..55edf415fa72 100644
--- a/posthog/schema.py
+++ b/posthog/schema.py
@@ -32597,6 +32597,7 @@ class ErrorTrackingQuery(BaseModel):
default=None,
description=("Free-text search across exception type, message, and stack frames."),
)
+ severity: ErrorTrackingQueryIssueSeverity | None = Field(default=None, description="Filter by issue severity.")
status: ErrorTrackingIssueStatus | str | None = Field(
default=None,
description="Filter by issue status.",
diff --git a/products/dashboards/frontend/generated/api.schemas.ts b/products/dashboards/frontend/generated/api.schemas.ts
index 9c50420ae698..61b5e99ee68b 100644
--- a/products/dashboards/frontend/generated/api.schemas.ts
+++ b/products/dashboards/frontend/generated/api.schemas.ts
@@ -8476,6 +8476,8 @@ export interface ErrorTrackingQueryApi {
response?: ErrorTrackingQueryResponseApi | null
/** Free-text search across exception type, message, and stack frames. */
searchQuery?: string | null
+ /** Filter by issue severity. */
+ severity?: ErrorTrackingQueryIssueSeverityApi | null
/** Filter by issue status. */
status?: ErrorTrackingIssueStatusApi | string | null
tags?: QueryLogTagsApi | null
diff --git a/products/desktop/packages/api-client/src/generated.ts b/products/desktop/packages/api-client/src/generated.ts
index bcc13b1a6f0a..f235a73e211a 100644
--- a/products/desktop/packages/api-client/src/generated.ts
+++ b/products/desktop/packages/api-client/src/generated.ts
@@ -8593,6 +8593,7 @@ export namespace Schemas {
personId?: (string | null) | undefined;
response?: (ErrorTrackingQueryResponse | null) | undefined;
searchQuery?: (string | null) | undefined;
+ severity?: (ErrorTrackingQueryIssueSeverity | null) | undefined;
status?: (ErrorTrackingIssueStatus | string | null) | undefined;
tags?: (QueryLogTags | null) | undefined;
useQueryV2?: (boolean | null) | undefined;
diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py
index a52afa317227..ecd8c335b1d9 100644
--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py
+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_builder.py
@@ -417,6 +417,46 @@ class ErrorTrackingQueryBuilder:
],
)
+ def _issue_state_filter_exprs(self, state_expr: Callable[[str], ast.Expr]) -> list[ast.Expr]:
+ exprs: list[ast.Expr] = []
+ if self.query.status and self.query.status != "all":
+ exprs.append(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=state_expr("issue_status"),
+ right=ast.Constant(value=self.query.status),
+ )
+ )
+
+ if self.query.severity:
+ exprs.append(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=state_expr("issue_severity"),
+ right=ast.Constant(value=self.query.severity.value),
+ )
+ )
+
+ if self.query.assignee:
+ if self.query.assignee.type == "user":
+ exprs.append(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=state_expr("assigned_user_id"),
+ right=ast.Constant(value=self.query.assignee.id),
+ )
+ )
+ else:
+ exprs.append(
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.Eq,
+ left=state_expr("assigned_role_id"),
+ right=ast.Constant(value=str(self.query.assignee.id)),
+ )
+ )
+
+ return exprs
+
def _build_query_optimized(self) -> ast.SelectQuery:
inner = self._build_inner_query()
outer_where_exprs = self._outer_where_exprs()
@@ -695,32 +735,7 @@ class ErrorTrackingQueryBuilder:
)
)
- if self.query.status and self.query.status != "all":
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._issue_state_expr("issue_status"),
- right=ast.Constant(value=self.query.status),
- )
- )
-
- if self.query.assignee:
- if self.query.assignee.type == "user":
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._issue_state_expr("assigned_user_id"),
- right=ast.Constant(value=self.query.assignee.id),
- )
- )
- else:
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._issue_state_expr("assigned_role_id"),
- right=ast.Constant(value=str(self.query.assignee.id)),
- )
- )
+ exprs.extend(self._issue_state_filter_exprs(self._issue_state_expr))
return exprs
@@ -911,32 +926,7 @@ class ErrorTrackingQueryBuilder:
if self.query.searchQuery:
exprs.append(self._search_expr())
- if self.query.status and self.query.status != "all":
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._legacy_issue_state_expr("issue_status"),
- right=ast.Constant(value=self.query.status),
- )
- )
-
- if self.query.assignee:
- if self.query.assignee.type == "user":
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._legacy_issue_state_expr("assigned_user_id"),
- right=ast.Constant(value=self.query.assignee.id),
- )
- )
- else:
- exprs.append(
- ast.CompareOperation(
- op=ast.CompareOperationOp.Eq,
- left=self._legacy_issue_state_expr("assigned_role_id"),
- right=ast.Constant(value=str(self.query.assignee.id)),
- )
- )
+ exprs.extend(self._issue_state_filter_exprs(self._legacy_issue_state_expr))
user_expr = self._user_filter_expr()
if user_expr is not None:
diff --git a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
index 960565ffddd8..ab6133a48e59 100644
--- a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
+++ b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py
@@ -237,6 +237,7 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIde
orderBy="last_seen",
orderDirection=None,
status=None,
+ severity=None,
volumeResolution=1,
withAggregations=False,
withFirstEvent=False,
@@ -261,6 +262,7 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIde
orderBy=orderBy, # pyright: ignore[reportArgumentType]
orderDirection=orderDirection,
status=status,
+ severity=severity,
volumeResolution=volumeResolution,
withFirstEvent=withFirstEvent,
withAggregations=withAggregations,
@@ -1203,6 +1205,57 @@ class TestErrorTrackingQueryRunner(ClickhouseTestMixin, NonAtomicBaseTestKeepIde
expected_severity = ErrorTrackingIssue.Severity.HIGH if result["id"] == self.issue_id_one else None
self.assertEqual(result["severity"], expected_severity)
+ @parameterized.expand(
+ [
+ ("optimized_shape", False),
+ ("legacy_shape", True),
+ ]
+ )
+ @time_machine.travel("2022-01-10T12:11:00", tick=False)
+ def test_severity_field(self, _name: str, legacy_shape: bool):
+ self._assign_severities()
+ shape_filters: list[ErrorTrackingIssueFilter] = []
+ if legacy_shape:
+ # Matches every issue, so it only forces the legacy shape.
+ shape_filters = [ErrorTrackingIssueFilter(key="status", value=True, operator=PropertyOperator.IS_SET)]
+
+ filter_group = PropertyGroupFilter(
+ type=FilterLogicalOperator.AND_,
+ values=[PropertyGroupFilterValue(type=FilterLogicalOperator.AND_, values=shape_filters)],
+ )
+
+ builder = ErrorTrackingQueryBuilder(
+ query=ErrorTrackingQuery(
+ kind="ErrorTrackingQuery",
+ dateRange=DateRange(date_from="all"),
+ filterGroup=filter_group,
+ orderBy="last_seen",
+ severity=ErrorTrackingQueryIssueSeverity.HIGH,
+ volumeResolution=1,
+ ),
+ team=self.team,
+ date_from=datetime(2020, 1, 1, tzinfo=UTC),
+ date_to=datetime(2022, 1, 10, tzinfo=UTC),
+ )
+ self.assertEqual(builder._needs_legacy_shape(), legacy_shape)
+
+ by_field = {
+ severity.value: {
+ result["id"] for result in self._calculate(filterGroup=filter_group, severity=severity)["results"]
+ }
+ for severity in ErrorTrackingQueryIssueSeverity
+ }
+
+ self.assertEqual(
+ by_field,
+ {
+ "low": {self.issue_id_two},
+ "medium": {self.issue_id_four},
+ "high": {self.issue_id_three},
+ "critical": {self.issue_id_one},
+ },
+ )
+
@parameterized.expand(
[
("exact_single", "name", PropertyOperator.EXACT, ["TypeError"], "equals(e.issue_name, 'TypeError')"),
diff --git a/products/error_tracking/frontend/queries.test.ts b/products/error_tracking/frontend/queries.test.ts
index 00ae58251adb..3235ab7ba274 100644
--- a/products/error_tracking/frontend/queries.test.ts
+++ b/products/error_tracking/frontend/queries.test.ts
@@ -34,7 +34,7 @@ describe('queries', () => {
})
})
- it('adds severity without changing an OR property filter', () => {
+ it('sends severity as its own field and leaves the filter group unchanged', () => {
const browserFilter: EventPropertyFilter = {
type: PropertyFilterType.Event,
key: '$browser',
@@ -59,22 +59,11 @@ describe('queries', () => {
columns: ['error'],
})
- expect((actual.source as ErrorTrackingQuery).filterGroup).toEqual({
+ const source = actual.source as ErrorTrackingQuery
+ expect(source.severity).toEqual('high')
+ expect(source.filterGroup).toEqual({
type: FilterLogicalOperator.And,
- values: [
- {
- type: FilterLogicalOperator.And,
- values: [
- { type: FilterLogicalOperator.Or, values: [browserFilter, osFilter] },
- {
- type: PropertyFilterType.ErrorTrackingIssue,
- key: 'severity',
- operator: PropertyOperator.Exact,
- value: ['high'],
- },
- ],
- },
- ],
+ values: [{ type: FilterLogicalOperator.Or, values: [browserFilter, osFilter] }],
})
})
})
diff --git a/products/error_tracking/frontend/queries.ts b/products/error_tracking/frontend/queries.ts
index 821a1ca606f2..c3ee70eb97f6 100644
--- a/products/error_tracking/frontend/queries.ts
+++ b/products/error_tracking/frontend/queries.ts
@@ -1,4 +1,3 @@
-import { isUniversalGroupFilterLike } from 'lib/components/UniversalFilters/utils'
import { HogQLQueryString, escapeHogQLString, hogql } from 'lib/utils/hogql'
import { setLatestVersionsOnQuery } from '~/queries/schema/query-versions'
@@ -11,7 +10,6 @@ import {
ErrorTrackingIssueEventsQuery,
ErrorTrackingPendingFingerprintIssueStateUpdate,
ErrorTrackingQuery,
- ErrorTrackingQueryIssueSeverity,
ErrorTrackingReleasesOrderBy,
ErrorTrackingReleasesQuery,
EventsQuery,
@@ -23,11 +21,8 @@ import {
AnyPropertyFilter,
BaseMathType,
ChartDisplayType,
- ErrorTrackingIssueFilter,
- FilterLogicalOperator,
PropertyFilterType,
PropertyGroupFilter,
- PropertyOperator,
UniversalFiltersGroup,
} from '~/types'
@@ -39,44 +34,6 @@ import {
SEARCHABLE_EXCEPTION_PROPERTIES,
} from './utils'
-function withIssueSeverityFilter(
- filterGroup: UniversalFiltersGroup,
- severity: ErrorTrackingQueryIssueSeverity | null | undefined
-): UniversalFiltersGroup {
- if (!severity) {
- return filterGroup
- }
-
- const severityFilter: ErrorTrackingIssueFilter = {
- key: 'severity',
- type: PropertyFilterType.ErrorTrackingIssue,
- operator: PropertyOperator.Exact,
- value: [severity],
- }
- const firstValue = filterGroup.values[0]
- const firstGroup: UniversalFiltersGroup = isUniversalGroupFilterLike(firstValue)
- ? firstValue
- : {
- type: FilterLogicalOperator.And,
- values: firstValue ? [firstValue] : [],
- }
- const nextValues =
- firstGroup.type === FilterLogicalOperator.Or && firstGroup.values.length > 0
- ? [firstGroup, severityFilter]
- : [...firstGroup.values, severityFilter]
-
- return {
- ...filterGroup,
- values: [
- {
- type: FilterLogicalOperator.And,
- values: nextValues,
- },
- ...filterGroup.values.slice(1),
- ],
- }
-}
-
export const errorTrackingQuery = ({
orderBy,
status,
@@ -98,6 +55,7 @@ export const errorTrackingQuery = ({
ErrorTrackingQuery,
| 'orderBy'
| 'status'
+ | 'severity'
| 'dateRange'
| 'assignee'
| 'filterTestAccounts'
@@ -109,7 +67,6 @@ export const errorTrackingQuery = ({
| 'groupTypeIndex'
> & {
filterGroup: UniversalFiltersGroup
- severity?: ErrorTrackingQueryIssueSeverity | null
columns: string[]
volumeResolution?: number
pendingFingerprintIssueStateUpdates?: ErrorTrackingPendingFingerprintIssueStateUpdate[]
@@ -120,10 +77,11 @@ export const errorTrackingQuery = ({
kind: NodeKind.ErrorTrackingQuery,
orderBy,
status,
+ ...(severity ? { severity } : {}),
dateRange,
assignee,
volumeResolution,
- filterGroup: withIssueSeverityFilter(filterGroup, severity) as PropertyGroupFilter,
+ filterGroup: filterGroup as PropertyGroupFilter,
filterTestAccounts: filterTestAccounts,
searchQuery: searchQuery,
limit: limit,
diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.ts
index bc4c3f3c1276..ff99b0caf2b8 100644
--- a/products/product_analytics/frontend/generated/api.schemas.ts
+++ b/products/product_analytics/frontend/generated/api.schemas.ts
@@ -7282,6 +7282,8 @@ export interface ErrorTrackingQueryApi {
response?: ErrorTrackingQueryResponseApi | null
/** Free-text search across exception type, message, and stack frames. */
searchQuery?: string | null
+ /** Filter by issue severity. */
+ severity?: ErrorTrackingQueryIssueSeverityApi | null
/** Filter by issue status. */
status?: ErrorTrackingIssueStatusApi | string | null
tags?: QueryLogTagsApi | null
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index da7e6898ef5f..595144ac92af 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -10666,6 +10666,8 @@ export namespace Schemas {
response?: ErrorTrackingQueryResponse | null;
/** Free-text search across exception type, message, and stack frames. */
searchQuery?: string | null;
+ /** Filter by issue severity. */
+ severity?: ErrorTrackingQueryIssueSeverity | null;
/** Filter by issue status. */
status?: ErrorTrackingIssueStatus | string | null;
tags?: QueryLogTags | null;