Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stacktrace-link): Only code mappings that match should affect the API response #41562

Merged

Conversation

armenzg
Copy link
Member

@armenzg armenzg commented Nov 18, 2022

Changes:

  • Fix that the last code mapping would set the error information even if it didn't match
  • Make the code readable
  • Improve debuggability by returning all code mappings that match and associated results
  • Add organization.slug and project.slug tags

@armenzg armenzg self-assigned this Nov 18, 2022
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 18, 2022
@@ -15,8 +15,9 @@


def get_link(
config: RepositoryProjectPathConfig, filepath: str, default: str, version: Optional[str] = None
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
config: RepositoryProjectPathConfig, filepath: str, version: Optional[str] = None
Copy link
Member Author

@armenzg armenzg Nov 18, 2022

Choose a reason for hiding this comment

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

Using Any instead of dict[str, str] because some weird issue I was not able to figure out.
I can try again in the future.


# This is to support mobile languages with non-fully-qualified file pathing.
# We attempt to 'munge' the proper source-relative filepath based on the stackframe data.
def generate_mobile_frame(parameters: Any) -> Any:
Copy link
Member Author

Choose a reason for hiding this comment

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

I just moved the code from here into a function:

abs_path = request.GET.get("absPath")
module = request.GET.get("module")
package = request.GET.get("package")
frame = {}
if abs_path:
frame["abs_path"] = abs_path
if module:
frame["module"] = module
if package:
frame["package"] = package

@@ -56,24 +109,21 @@ class ProjectStacktraceLinkEndpoint(ProjectEndpoint):
"""

def get(self, request: Request, project) -> Response:
# import pprint

# pprint.pp(request)
Copy link
Member Author

Choose a reason for hiding this comment

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

I will remove this in the following push.

@@ -85,69 +135,63 @@ def get(self, request: Request, project) -> Response:
if i.has_feature(IntegrationFeatures.STACKTRACE_LINK)
]

last_error = None
Copy link
Member Author

Choose a reason for hiding this comment

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

Not needed anymore.


link, attempted_url, error = get_link(
config, munged_filename, config.default_branch, commit_id
)
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 whole block is now done above like this:

                # For mobile we try a second time by munging the file path
                if not result["sourceUrl"] and mobile_frame:
                    result = try_path_munging(filepath, mobile_frame, ctx)

last_error = get_link_error
result["attemptedUrl"] = attempted_url
if not result["sourceUrl"]:
matched_code_mappings.append(current_config)
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 will create a list of all code mappings that were attempted and its result.

@@ -15,27 +15,77 @@


def get_link(
config: RepositoryProjectPathConfig, filepath: str, default: str, version: Optional[str] = None
Copy link
Member Author

Choose a reason for hiding this comment

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

config contains the default branch. I'm reducing the signature.

return frame


def try_path_munging(
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 function comes from the main loop.

frame["filename"] = filepath
munged_frames = munged_filename_and_frames(
platform, [frame], "munged_filename", sdk_name=sdk_name
)
if munged_frames:
munged_frame: Mapping[str, Any] = munged_frames[1][0]
munged_filename = str(munged_frame.get("munged_filename"))
if munged_filename:
if not filepath.startswith(
config.stack_root
) and not munged_filename.startswith(config.stack_root):
last_error = "stack_root_mismatch"
continue
link, attempted_url, error = get_link(
config, munged_filename, config.default_branch, commit_id
)

"platform": request.GET.get("platform"),
"sdk_name": request.GET.get("sdkName"),
}
mobile_frame = generate_mobile_frame(request.GET)
Copy link
Member Author

Choose a reason for hiding this comment

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

Using mobile_frame later in the code to decide if this is a mobile platform does not make sense. Either way, here we are.

with configure_scope() as scope:
scope.set_tag("project.slug", project.slug)
scope.set_tag("organization.slug", project.organization.slug)
Copy link
Member Author

Choose a reason for hiding this comment

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

These are two new tags.

@@ -116,10 +116,8 @@ def test_file_not_found_error(self):
)
assert response.data["config"] == self.expected_configurations(self.code_mapping1)
assert not response.data["sourceUrl"]
# XXX: This depends on what was the last attempted code mapping
assert response.data["error"] == "stack_root_mismatch"
assert response.data["error"] == "file_not_found"
Copy link
Member Author

Choose a reason for hiding this comment

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

In #41409 I added more than one code mapping and it showed that the error value would depend on the last attempted code mapping rather than the last one that matched.

This PR fixes the issue since the first code mapping matches the stack trace root and the source file is not found (as the original test was):

@armenzg armenzg changed the title ref(stacktrace-link): Clean up the code and improvements fix(stacktrace-link): Only code mappings that match should affect the API response Nov 18, 2022
@armenzg armenzg marked this pull request as ready for review November 18, 2022 18:24
@armenzg armenzg requested a review from a team as a code owner November 18, 2022 18:24
@armenzg armenzg requested review from a team November 18, 2022 18:24
@armenzg armenzg merged commit cb37060 into master Nov 21, 2022
@armenzg armenzg deleted the armenzg/derive-code-mappings/track-invalid-code-mappings branch November 21, 2022 13:24
@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants