1818)
1919from sentry_protos .snuba .v1 .request_common_pb2 import RequestMeta , TraceItemType
2020from sentry_protos .snuba .v1 .trace_item_attribute_pb2 import AttributeKey , AttributeValue
21- from sentry_protos .snuba .v1 .trace_item_filter_pb2 import ComparisonFilter , TraceItemFilter
21+ from sentry_protos .snuba .v1 .trace_item_filter_pb2 import (
22+ ComparisonFilter ,
23+ TraceItemFilter ,
24+ )
2225from snuba_sdk import Column as SnubaColumn
2326from snuba_sdk import Function
2427
@@ -111,7 +114,9 @@ class TraceOccurrenceEvent(TypedDict):
111114 issue_data : TraceIssueOccurrenceData
112115
113116
114- def _serialize_rpc_issue (event : dict [str , Any ], group_cache : dict [int , Group ]) -> SerializedIssue :
117+ def _serialize_rpc_issue (
118+ event : dict [str , Any ], group_cache : dict [int , Group ]
119+ ) -> SerializedIssue | None :
115120 def _qualify_short_id (project : str , short_id : int | None ) -> str | None :
116121 """Logic for qualified_short_id is copied from property on the Group model
117122 to prevent an N+1 query from accessing project.slug everytime"""
@@ -127,7 +132,11 @@ def _qualify_short_id(project: str, short_id: int | None) -> str | None:
127132 if issue_id in group_cache :
128133 issue = group_cache [issue_id ]
129134 else :
130- issue = Group .objects .get (id = issue_id , project__id = occurrence .project_id )
135+ try :
136+ issue = Group .objects .get (id = issue_id , project__id = occurrence .project_id )
137+ except Group .DoesNotExist as e :
138+ logger .error (e )
139+ return None
131140 group_cache [issue_id ] = issue
132141 return SerializedIssue (
133142 event_id = occurrence .event_id ,
@@ -154,7 +163,11 @@ def _qualify_short_id(project: str, short_id: int | None) -> str | None:
154163 if issue_id in group_cache :
155164 issue = group_cache [issue_id ]
156165 else :
157- issue = Group .objects .get (id = issue_id , project__id = event ["project.id" ])
166+ try :
167+ issue = Group .objects .get (id = issue_id , project__id = event ["project.id" ])
168+ except Group .DoesNotExist as e :
169+ logger .error (e )
170+ return None
158171 group_cache [issue_id ] = issue
159172
160173 return SerializedIssue (
@@ -179,7 +192,7 @@ def _serialize_rpc_event(
179192 event : dict [str , Any ],
180193 group_cache : dict [int , Group ],
181194 additional_attributes : list [str ] | None = None ,
182- ) -> SerializedEvent | SerializedIssue | SerializedUptimeCheck :
195+ ) -> SerializedEvent | SerializedIssue | SerializedUptimeCheck | None :
183196 if event .get ("event_type" ) not in ("span" , "uptime_check" ):
184197 return _serialize_rpc_issue (event , group_cache )
185198
@@ -189,11 +202,25 @@ def _serialize_rpc_event(
189202 if attribute in event
190203 }
191204 children = [
192- _serialize_rpc_event (child , group_cache , additional_attributes )
193- for child in event ["children" ]
205+ child
206+ for child in [
207+ _serialize_rpc_event (child , group_cache , additional_attributes )
208+ for child in event ["children" ]
209+ ]
210+ if child is not None
211+ ]
212+ errors = [
213+ error
214+ for error in [_serialize_rpc_issue (error , group_cache ) for error in event ["errors" ]]
215+ if error is not None
216+ ]
217+ occurrences = [
218+ occurrence
219+ for occurrence in [
220+ _serialize_rpc_issue (error , group_cache ) for error in event ["occurrences" ]
221+ ]
222+ if occurrence is not None
194223 ]
195- errors = [_serialize_rpc_issue (error , group_cache ) for error in event ["errors" ]]
196- occurrences = [_serialize_rpc_issue (error , group_cache ) for error in event ["occurrences" ]]
197224
198225 if event .get ("event_type" ) == "uptime_check" :
199226 return SerializedUptimeCheck (
@@ -389,7 +416,9 @@ def _uptime_results_query(
389416 )
390417
391418
392- def _run_uptime_results_query (uptime_query : TraceItemTableRequest ) -> list [TraceItemTableResponse ]:
419+ def _run_uptime_results_query (
420+ uptime_query : TraceItemTableRequest ,
421+ ) -> list [TraceItemTableResponse ]:
393422 return table_rpc ([uptime_query ])
394423
395424
@@ -651,4 +680,10 @@ def query_trace_data(
651680 result .extend (errors )
652681 group_cache : dict [int , Group ] = {}
653682 with sentry_sdk .start_span (op = "serializing_data" ):
654- return [_serialize_rpc_event (root , group_cache , additional_attributes ) for root in result ]
683+ return [
684+ event
685+ for event in [
686+ _serialize_rpc_event (root , group_cache , additional_attributes ) for root in result
687+ ]
688+ if event is not None
689+ ]
0 commit comments