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
feat(code-mappings): Add new task to find projects with missing code mappings #40271
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c25d0c8
Add new task to find missing codemappings
bce98dd
Apply suggestions from code review
snigdhas 86190e8
style(lint): Auto commit lint changes
getsantry[bot] 708d261
Add typing and fix variable names
e3b6684
Add docstring comments
82bd51c
Define new task
d065a56
Update tests/sentry/tasks/test_find_missing_codemappings.py
snigdhas 0e065be
Apply suggestions from code review
snigdhas e3e86d5
Fix celery queue
4694a38
Reduce code duplication in tests
8d1a0e1
Rename files
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import logging | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from datetime import timedelta | ||
from typing import Any, List, Mapping, Optional, Set, Tuple | ||
|
||
from django.utils import timezone | ||
|
||
from sentry.db.models.fields.node import NodeData | ||
from sentry.models import Project | ||
from sentry.models.group import Group | ||
from sentry.models.organization import Organization, OrganizationStatus | ||
from sentry.tasks.base import instrumented_task | ||
from sentry.utils.safe import get_path | ||
|
||
ACTIVE_PROJECT_THRESHOLD = timedelta(days=7) | ||
GROUP_ANALYSIS_RANGE = timedelta(days=14) | ||
|
||
logger = logging.getLogger("sentry.tasks.find_missing_codemappings") | ||
|
||
|
||
@instrumented_task( # type: ignore | ||
name="sentry.tasks.find_missing_codemappings", | ||
queue="find_missing_codemappings", | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
max_retries=0, # if we don't backfill it this time, we'll get it the next time | ||
) | ||
def find_missing_codemappings( | ||
organizations: Optional[List[Organization]] = None, | ||
) -> Mapping[str, Mapping[str, List[str]]]: | ||
""" | ||
Generate a map of projects to stacktrace paths for specified organizations, | ||
or all active organizations if unspecified. | ||
|
||
This filters out projects have not had an event in the last 7 days or have | ||
non-python files in the stacktrace. | ||
""" | ||
if organizations is None: | ||
organizations = Organization.objects.filter(status=OrganizationStatus.ACTIVE) | ||
|
||
filename_maps = {} | ||
for org in organizations: | ||
projects = Project.objects.filter(organization=org, first_event__isnull=False) | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
projects = [ | ||
project | ||
for project in projects | ||
if Group.objects.filter( | ||
project=project, | ||
last_seen__gte=timezone.now() - ACTIVE_PROJECT_THRESHOLD, | ||
).exists() | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
|
||
project_file_map = {project.slug: get_all_stacktrace_paths(project) for project in projects} | ||
filename_maps[org.slug] = project_file_map | ||
return filename_maps | ||
|
||
|
||
def get_all_stacktrace_paths(project: Project) -> List[str]: | ||
groups = Group.objects.filter( | ||
project=project, last_seen__gte=timezone.now() - GROUP_ANALYSIS_RANGE | ||
) | ||
|
||
all_stacktrace_paths = set() | ||
for group in groups: | ||
event = group.get_latest_event() | ||
is_python_stacktrace, stacktrace_paths = get_stacktrace_paths(event.data) | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not is_python_stacktrace: | ||
return [] | ||
all_stacktrace_paths.update(stacktrace_paths) | ||
|
||
return list(all_stacktrace_paths) | ||
|
||
|
||
def get_stacktrace_paths(data: NodeData) -> Tuple[bool, Set[str]]: | ||
""" | ||
Get the stacktrace_paths from the stacktrace for the latest event for an issue. | ||
""" | ||
stacktraces = get_stacktrace(data) | ||
stacktrace_paths = set() | ||
for stacktrace in stacktraces: | ||
try: | ||
paths = [frame["filename"] for frame in stacktrace["frames"]] | ||
if len(paths) == 0: | ||
continue | ||
if paths[0].endswith(".py"): | ||
stacktrace_paths.update(paths) | ||
else: | ||
return False, set() # (is_python, stacktrace_paths) | ||
except Exception: | ||
logger.exception("Error getting filenames for project {project.slug}") | ||
return True, stacktrace_paths # (is_python, stacktrace_paths) | ||
|
||
|
||
def get_stacktrace(data: NodeData) -> List[Mapping[str, Any]]: | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exceptions = get_path(data, "exception", "values", filter=True) | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if exceptions: | ||
return [e["stacktrace"] for e in exceptions if get_path(e, "stacktrace", "frames")] | ||
|
||
stacktrace = data.get("stacktrace") | ||
armenzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if stacktrace and stacktrace.get("frames"): | ||
return [stacktrace] | ||
|
||
return [] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we need to register it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think it has to do with
find-missing-codemappings
vsfind_missing_codemappings
.