Skip to content
Merged
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
19 changes: 14 additions & 5 deletions src/sentry/issues/auto_source_code_config/code_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ class NeedsExtension(Exception):
def derive_code_mappings(
organization: Organization,
frame: Mapping[str, Any],
platform: str | None = None,
) -> list[dict[str, str]]:
installation = get_installation(organization)
if not isinstance(installation, RepoTreesIntegration):
return []
trees = installation.get_trees_for_org()
trees_helper = CodeMappingTreesHelper(trees)
try:
frame_filename = FrameInfo(frame)
frame_filename = FrameInfo(frame, platform)
return trees_helper.list_file_matches(frame_filename)
except NeedsExtension:
logger.warning("Needs extension: %s", frame.get("filename"))
Expand All @@ -71,8 +72,8 @@ def derive_code_mappings(

# XXX: Look at sentry.interfaces.stacktrace and maybe use that
class FrameInfo:
def __init__(self, frame: Mapping[str, Any]) -> None:
# XXX: In the next PR, we will use more than just the filename
def __init__(self, frame: Mapping[str, Any], platform: str | None = None) -> None:
# XXX: platform will be used in a following PR
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soon 😄

frame_file_path = frame["filename"]
frame_file_path = self.transformations(frame_file_path)

Expand Down Expand Up @@ -102,11 +103,13 @@ def __init__(self, frame: Mapping[str, Any]) -> None:

def transformations(self, frame_file_path: str) -> str:
self.raw_path = frame_file_path

is_windows_path = False
if "\\" in frame_file_path:
is_windows_path = True
frame_file_path = frame_file_path.replace("\\", "/")

# Remove leading slash if it exists
if frame_file_path[0] == "/" or frame_file_path[0] == "\\":
frame_file_path = frame_file_path[1:]

Expand All @@ -131,15 +134,21 @@ def __eq__(self, other: object) -> bool:

# call generate_code_mappings() after you initialize CodeMappingTreesHelper
class CodeMappingTreesHelper:
platform: str | None = None

def __init__(self, trees: Mapping[str, RepoTree]):
self.trees = trees
self.code_mappings: dict[str, CodeMapping] = {}

def generate_code_mappings(self, frames: Sequence[Mapping[str, Any]]) -> list[CodeMapping]:
def generate_code_mappings(
self, frames: Sequence[Mapping[str, Any]], platform: str | None = None
) -> list[CodeMapping]:
"""Generate code mappings based on the initial trees object and the list of stack traces"""
# We need to make sure that calling this method with a new list of stack traces
# should always start with a clean slate
self.code_mappings = {}
self.platform = platform

buckets: dict[str, list[FrameInfo]] = self._stacktrace_buckets(frames)

# We reprocess stackframes until we are told that no code mappings were produced
Expand Down Expand Up @@ -200,7 +209,7 @@ def _stacktrace_buckets(
buckets: defaultdict[str, list[FrameInfo]] = defaultdict(list)
for frame in frames:
try:
frame_filename = FrameInfo(frame)
frame_filename = FrameInfo(frame, self.platform)
# Any files without a top directory will be grouped together
buckets[frame_filename.stack_root].append(frame_filename)
except UnsupportedFrameInfo:
Expand Down
9 changes: 3 additions & 6 deletions src/sentry/issues/auto_source_code_config/stacktraces.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ def get_stacktraces(data: NodeData | dict[str, Any]) -> list[dict[str, Any]]:
if exceptions:
return [e["stacktrace"] for e in exceptions if get_path(e, "stacktrace", "frames")]

stacktrace = data.get("stacktrace")
if stacktrace and stacktrace.get("frames"):
logger.warning("Investigate if we use this code path in production.")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is nowhere to be found in Sentry, thus, simplifying this.

return [stacktrace]

return []
# This section is only used as part of the tests
# Ideally, we should fix the tests to pass the correct data
return [data["stacktrace"]]
7 changes: 4 additions & 3 deletions src/sentry/issues/auto_source_code_config/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ def process_event(project_id: int, group_id: int, event_id: str) -> list[CodeMap
logger.error("Event not found.", extra=extra)
return []

platform = event.data["platform"]
platform = event.platform
assert platform is not None
if not supported_platform(platform):
return []

frames_to_process = get_frames_to_process(event.data, event.platform)
frames_to_process = get_frames_to_process(event.data, platform)
if not frames_to_process:
return []

Expand All @@ -78,7 +79,7 @@ def process_event(project_id: int, group_id: int, event_id: str) -> list[CodeMap
installation = get_installation(org)
trees = get_trees_for_org(installation, org, extra)
trees_helper = CodeMappingTreesHelper(trees)
code_mappings = trees_helper.generate_code_mappings(frames_to_process)
code_mappings = trees_helper.generate_code_mappings(frames_to_process, platform)
if platform not in DRY_RUN_PLATFORMS:
set_project_codemappings(code_mappings, installation, project, platform)
except (InstallationNotFoundError, InstallationCannotGetTreesError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ def get(self, request: Request, organization: Organization) -> Response:

:param organization:
:param string stacktraceFilename:
:param string platform:
:auth: required
"""
# XXX: Evaluate what else does the UI send us
stacktrace_filename = request.GET.get("stacktraceFilename")
# XXX: The UI will need to pass the platform
platform = request.GET.get("platform")

try:
possible_code_mappings = []
resp_status: Literal[200, 204, 400] = status.HTTP_400_BAD_REQUEST

if stacktrace_filename:
possible_code_mappings = derive_code_mappings(
organization, {"filename": stacktrace_filename}
organization, {"filename": stacktrace_filename}, platform
)
if possible_code_mappings:
resp_status = status.HTTP_200_OK
Expand Down
Loading