Skip to content

Commit 48d82cc

Browse files
authored
feat(derive_code_mapping): Pass platform around the code base (#86026)
This is in preparation for the Java in-app work.
1 parent efd7381 commit 48d82cc

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

Diff for: src/sentry/issues/auto_source_code_config/code_mapping.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ class NeedsExtension(Exception):
5454
def derive_code_mappings(
5555
organization: Organization,
5656
frame: Mapping[str, Any],
57+
platform: str | None = None,
5758
) -> list[dict[str, str]]:
5859
installation = get_installation(organization)
5960
if not isinstance(installation, RepoTreesIntegration):
6061
return []
6162
trees = installation.get_trees_for_org()
6263
trees_helper = CodeMappingTreesHelper(trees)
6364
try:
64-
frame_filename = FrameInfo(frame)
65+
frame_filename = FrameInfo(frame, platform)
6566
return trees_helper.list_file_matches(frame_filename)
6667
except NeedsExtension:
6768
logger.warning("Needs extension: %s", frame.get("filename"))
@@ -71,8 +72,8 @@ def derive_code_mappings(
7172

7273
# XXX: Look at sentry.interfaces.stacktrace and maybe use that
7374
class FrameInfo:
74-
def __init__(self, frame: Mapping[str, Any]) -> None:
75-
# XXX: In the next PR, we will use more than just the filename
75+
def __init__(self, frame: Mapping[str, Any], platform: str | None = None) -> None:
76+
# XXX: platform will be used in a following PR
7677
frame_file_path = frame["filename"]
7778
frame_file_path = self.transformations(frame_file_path)
7879

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

103104
def transformations(self, frame_file_path: str) -> str:
104105
self.raw_path = frame_file_path
106+
105107
is_windows_path = False
106108
if "\\" in frame_file_path:
107109
is_windows_path = True
108110
frame_file_path = frame_file_path.replace("\\", "/")
109111

112+
# Remove leading slash if it exists
110113
if frame_file_path[0] == "/" or frame_file_path[0] == "\\":
111114
frame_file_path = frame_file_path[1:]
112115

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

132135
# call generate_code_mappings() after you initialize CodeMappingTreesHelper
133136
class CodeMappingTreesHelper:
137+
platform: str | None = None
138+
134139
def __init__(self, trees: Mapping[str, RepoTree]):
135140
self.trees = trees
136141
self.code_mappings: dict[str, CodeMapping] = {}
137142

138-
def generate_code_mappings(self, frames: Sequence[Mapping[str, Any]]) -> list[CodeMapping]:
143+
def generate_code_mappings(
144+
self, frames: Sequence[Mapping[str, Any]], platform: str | None = None
145+
) -> list[CodeMapping]:
139146
"""Generate code mappings based on the initial trees object and the list of stack traces"""
140147
# We need to make sure that calling this method with a new list of stack traces
141148
# should always start with a clean slate
142149
self.code_mappings = {}
150+
self.platform = platform
151+
143152
buckets: dict[str, list[FrameInfo]] = self._stacktrace_buckets(frames)
144153

145154
# We reprocess stackframes until we are told that no code mappings were produced
@@ -200,7 +209,7 @@ def _stacktrace_buckets(
200209
buckets: defaultdict[str, list[FrameInfo]] = defaultdict(list)
201210
for frame in frames:
202211
try:
203-
frame_filename = FrameInfo(frame)
212+
frame_filename = FrameInfo(frame, self.platform)
204213
# Any files without a top directory will be grouped together
205214
buckets[frame_filename.stack_root].append(frame_filename)
206215
except UnsupportedFrameInfo:

Diff for: src/sentry/issues/auto_source_code_config/stacktraces.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ def get_stacktraces(data: NodeData | dict[str, Any]) -> list[dict[str, Any]]:
3737
if exceptions:
3838
return [e["stacktrace"] for e in exceptions if get_path(e, "stacktrace", "frames")]
3939

40-
stacktrace = data.get("stacktrace")
41-
if stacktrace and stacktrace.get("frames"):
42-
logger.warning("Investigate if we use this code path in production.")
43-
return [stacktrace]
44-
45-
return []
40+
# This section is only used as part of the tests
41+
# Ideally, we should fix the tests to pass the correct data
42+
return [data["stacktrace"]]

Diff for: src/sentry/issues/auto_source_code_config/task.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ def process_event(project_id: int, group_id: int, event_id: str) -> list[CodeMap
6565
logger.error("Event not found.", extra=extra)
6666
return []
6767

68-
platform = event.data["platform"]
68+
platform = event.platform
69+
assert platform is not None
6970
if not supported_platform(platform):
7071
return []
7172

72-
frames_to_process = get_frames_to_process(event.data, event.platform)
73+
frames_to_process = get_frames_to_process(event.data, platform)
7374
if not frames_to_process:
7475
return []
7576

@@ -78,7 +79,7 @@ def process_event(project_id: int, group_id: int, event_id: str) -> list[CodeMap
7879
installation = get_installation(org)
7980
trees = get_trees_for_org(installation, org, extra)
8081
trees_helper = CodeMappingTreesHelper(trees)
81-
code_mappings = trees_helper.generate_code_mappings(frames_to_process)
82+
code_mappings = trees_helper.generate_code_mappings(frames_to_process, platform)
8283
if platform not in DRY_RUN_PLATFORMS:
8384
set_project_codemappings(code_mappings, installation, project, platform)
8485
except (InstallationNotFoundError, InstallationCannotGetTreesError):

Diff for: src/sentry/issues/endpoints/organization_derive_code_mappings.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ def get(self, request: Request, organization: Organization) -> Response:
4545
4646
:param organization:
4747
:param string stacktraceFilename:
48+
:param string platform:
4849
:auth: required
4950
"""
50-
# XXX: Evaluate what else does the UI send us
5151
stacktrace_filename = request.GET.get("stacktraceFilename")
52+
# XXX: The UI will need to pass the platform
53+
platform = request.GET.get("platform")
5254

5355
try:
5456
possible_code_mappings = []
5557
resp_status: Literal[200, 204, 400] = status.HTTP_400_BAD_REQUEST
5658

5759
if stacktrace_filename:
5860
possible_code_mappings = derive_code_mappings(
59-
organization, {"filename": stacktrace_filename}
61+
organization, {"filename": stacktrace_filename}, platform
6062
)
6163
if possible_code_mappings:
6264
resp_status = status.HTTP_200_OK

0 commit comments

Comments
 (0)