Skip to content

Commit

Permalink
ref(normalize_stacktraces_for_grouping): Minor refactoring (#53061)
Browse files Browse the repository at this point in the history
No perf improvements as originally intend it. Let's still take it.

---------

Co-authored-by: Armen Zambrano G <[email protected]>
  • Loading branch information
armenzg and armenzg authored Jul 18, 2023
1 parent 536fa53 commit 9ef3ba5
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/sentry/stacktraces/processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from datetime import datetime
from typing import Any, Callable, Dict, List, NamedTuple, Set
from typing import Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Set

import sentry_sdk
from django.utils import timezone
Expand Down Expand Up @@ -230,7 +230,7 @@ def _has_system_frames(frames):
return bool(system_frames) and len(frames) != system_frames


def _normalize_in_app(stacktrace):
def _normalize_in_app(stacktrace: List[Dict[str, str]]) -> None:
"""
Ensures consistent values of in_app across a stacktrace.
"""
Expand All @@ -241,7 +241,9 @@ def _normalize_in_app(stacktrace):
set_in_app(frame, False)


def normalize_stacktraces_for_grouping(data, grouping_config=None):
def normalize_stacktraces_for_grouping(
data: Mapping[str, Any], grouping_config: Any = None
) -> None:
"""
Applies grouping enhancement rules and ensure in_app is set on all frames.
This also trims functions if necessary.
Expand Down Expand Up @@ -272,41 +274,42 @@ def normalize_stacktraces_for_grouping(data, grouping_config=None):
with sentry_sdk.start_span(op=op, description="iterate_frames"):
for frames in stacktraces:
for frame in frames:
# Restore the original in_app value before the first grouping
# enhancers have been run. This allows to re-apply grouping
# enhancers on the original frame data.
orig_in_app = get_path(frame, "data", "orig_in_app")
if orig_in_app is not None:
frame["in_app"] = None if orig_in_app == -1 else bool(orig_in_app)

if frame.get("raw_function") is not None:
continue
raw_func = frame.get("function")
if not raw_func:
continue
function_name = trim_function_name(raw_func, frame.get("platform") or platform)
if function_name != raw_func:
frame["raw_function"] = raw_func
frame["function"] = function_name
_update_frame(frame, platform)

# If a grouping config is available, run grouping enhancers
if grouping_config is not None:
with sentry_sdk.start_span(op=op, description="apply_modifications_to_frame"):
counter = 0
for frames, exception_data in zip(stacktraces, stacktrace_exceptions):
grouping_config.enhancements.apply_modifications_to_frame(
frames, platform, exception_data
)
counter += 1
sentry_sdk.set_tag("processing.mods_to_frame", counter)
sentry_sdk.set_tag("processing.num_frames", len(frames))

# normalize in-app
with sentry_sdk.start_span(op=op, description="normalize_in_app_stacktraces"):
for stacktrace in stacktraces:
_normalize_in_app(stacktrace)


def _update_frame(frame: Dict[str, Any], platform: Optional[str]) -> None:
"""Restore the original in_app value before the first grouping
enhancers have been run. This allows to re-apply grouping
enhancers on the original frame data.
"""
orig_in_app = get_path(frame, "data", "orig_in_app")
if orig_in_app is not None:
frame["in_app"] = None if orig_in_app == -1 else bool(orig_in_app)

if frame.get("raw_function") is not None:
return
raw_func = frame.get("function")
if not raw_func:
return
function_name = trim_function_name(raw_func, frame.get("platform") or platform)
if function_name != raw_func:
frame["raw_function"] = raw_func
frame["function"] = function_name


def should_process_for_stacktraces(data):
from sentry.plugins.base import plugins

Expand Down

0 comments on commit 9ef3ba5

Please sign in to comment.