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

Make os.distribution searchable #69865

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
23 changes: 21 additions & 2 deletions src/sentry/rules/conditions/event_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"stacktrace.package": Columns.STACK_PACKAGE,
"unreal.crashtype": Columns.UNREAL_CRASH_TYPE,
"app.in_foreground": Columns.APP_IN_FOREGROUND,
"os.distribution.name": Columns.OS_DISTRIBUTION_NAME,
"os.distribution.version": Columns.OS_DISTRIBUTION_VERSION,
}


Expand Down Expand Up @@ -112,8 +114,8 @@ def _get_attribute_values(self, event: GroupEvent, attr: str) -> Sequence[object
return value
return [value]

elif len(path) != 2:
return []
elif len(path) < 2:
return [] # all attribute paths below have at least 2 elements

elif path[0] == "exception":
if path[1] not in ("type", "value"):
Expand Down Expand Up @@ -211,6 +213,23 @@ def _get_attribute_values(self, event: GroupEvent, attr: str) -> Sequence[object
response = {}
return [response.get(path[1])]

elif len(path) < 3:
return [] # all attribute paths below have at least 3 elements

elif path[0] == "os":
if path[1] in ("distribution"):
Comment on lines +219 to +220
Copy link
Member

Choose a reason for hiding this comment

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

If someone were to search for os.distribution then we'd get an index error here. I think we can extend the pattern above by removing the elif len(path) != 2 check and structuring it as:

# remove the len(path) != 2 check

# everything that indexes on 2 segments of the path

if len(path) == 2:
    return []  # No other attribute paths of 2 have been mapped

# The new branch for indexing os.distribution.name/version

return [] # Handles all other cases

Everything else looks good to me though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I added delimiting path-length branches and a test that would have failed in the previous version with an IndexError.

if path[2] in ("name", "version"):
contexts = event.data["contexts"]
os_context = contexts.get("os")
if os_context is None:
os_context = {}

distribution = os_context.get(path[1])
if distribution is None:
distribution = {}

return [distribution.get(path[2])]
return []
return []

return []
Expand Down
16 changes: 16 additions & 0 deletions src/sentry/snuba/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,22 @@ class Columns(Enum):
issue_platform_name="contexts[app.in_foreground]",
alias="app.in_foreground",
)
OS_DISTRIBUTION_NAME = Column(
group_name="events.contexts[os.distribution.name]",
event_name="contexts[os.distribution.name]",
transaction_name="contexts[os.distribution.name]",
discover_name="contexts[os.distribution.name]",
issue_platform_name="contexts[os.distribution.name]",
alias="os.distribution.name",
)
OS_DISTRIBUTION_VERSION = Column(
group_name="events.contexts[os.distribution.version]",
event_name="contexts[os.distribution.version]",
transaction_name="contexts[os.distribution.version]",
discover_name="contexts[os.distribution.version]",
issue_platform_name="contexts[os.distribution.version]",
alias="os.distribution.version",
)
# Transactions specific columns
TRANSACTION_OP = Column(
group_name=None,
Expand Down
47 changes: 47 additions & 0 deletions tests/sentry/rules/conditions/test_event_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def get_event(self, **kwargs):
"unreal": {
"crash_type": "crash",
},
"os": {
"distribution": {
"name": "ubuntu",
"version": "22.04",
}
},
},
"threads": {
"values": [
Expand Down Expand Up @@ -757,6 +763,47 @@ def test_app_in_foreground(self):
)
self.assertDoesNotPass(rule, event)

def test_os_distribution_only(self):
event = self.get_event()
rule = self.get_rule(
data={"match": MatchType.EQUAL, "attribute": "os.distribution", "value": "irrelevant"}
)
self.assertDoesNotPass(rule, event)

def test_os_distribution_name_and_version(self):
event = self.get_event()
rule = self.get_rule(
data={"match": MatchType.EQUAL, "attribute": "os.distribution.name", "value": "ubuntu"}
)
self.assertPasses(rule, event)

rule = self.get_rule(
data={
"match": MatchType.EQUAL,
"attribute": "os.distribution.version",
"value": "22.04",
}
)
self.assertPasses(rule, event)

rule = self.get_rule(
data={
"match": MatchType.EQUAL,
"attribute": "os.distribution.name",
"value": "slackware",
}
)
self.assertDoesNotPass(rule, event)

rule = self.get_rule(
data={
"match": MatchType.EQUAL,
"attribute": "os.distribution.version",
"value": "20.04",
}
)
self.assertDoesNotPass(rule, event)

def test_unreal_crash_type(self):
event = self.get_event()
rule = self.get_rule(
Expand Down
Loading