Skip to content

Commit c92cdeb

Browse files
Zylphrexclaude
andauthored
fix(snuba): Handle direct project.id groupby in top event conditions (#109535)
Handle direct `project.id` groupby in `build_top_event_conditions` When grouping by `project.id` directly in the timeseries spans endpoint, the top events query returns data keyed by `"project.id"` with the integer project ID as value. The existing code only handled the case where `project.id` appeared as a derived groupby key (from grouping by `project` or `project.name`), where the data contains the slug under a `"project"` or `"project.slug"` key. This caused a `KeyError: 'project.slug'`. The fix checks if the event data already has a `"project.id"` key and uses the value directly, falling back to the slug-based lookup for the derived case. Fixes SENTRY-5KF8 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6b2b1db commit c92cdeb

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/sentry/snuba/rpc_dataset_common.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,12 @@ def build_top_event_conditions(
825825
other_row_conditions = []
826826
for key in groupby_columns:
827827
if key == "project.id":
828-
value = resolver.params.project_slug_map[
829-
event.get("project") or event["project.slug"]
830-
]
828+
if "project.id" in event:
829+
value = event["project.id"]
830+
else:
831+
value = resolver.params.project_slug_map[
832+
event.get("project") or event["project.slug"]
833+
]
831834
else:
832835
value = event[key]
833836
resolved_term, context = resolver.resolve_term(

tests/snuba/api/endpoints/test_organization_events_timeseries_spans.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,3 +2615,27 @@ def test_sort_by_equation_and_top_events(self) -> None:
26152615
time_series_by_transaction["foo"]["meta"]["order"]
26162616
< time_series_by_transaction["bar"]["meta"]["order"]
26172617
)
2618+
2619+
def test_group_by_project_id(self) -> None:
2620+
self.store_spans([self.create_span({}, project=self.project)])
2621+
response = self._do_request(
2622+
data={
2623+
"yAxis": "count()",
2624+
"dataset": "spans",
2625+
"groupBy": ["project.id", "project.name"],
2626+
"query": "",
2627+
"topEvents": 5,
2628+
"project": self.project.id,
2629+
},
2630+
)
2631+
assert response.status_code == 200, response.content
2632+
2633+
time_series_by_project_id = {
2634+
ts["groupBy"][0]["value"]: ts
2635+
for ts in response.data["timeSeries"]
2636+
if ts["groupBy"] is not None
2637+
}
2638+
assert time_series_by_project_id[str(self.project.id)]["groupBy"] == [
2639+
{"key": "project.id", "value": str(self.project.id)},
2640+
{"key": "project.name", "value": self.project.slug},
2641+
]

0 commit comments

Comments
 (0)