Browse Source

Make `app.in_foreground` searchable (#43964)

Manoel Aranda Neto 2 years ago
parent
commit
850661247f

+ 12 - 0
src/sentry/rules/conditions/event_attribute.py

@@ -33,6 +33,7 @@ ATTR_CHOICES = {
     "stacktrace.filename": Columns.STACK_FILENAME,
     "stacktrace.abs_path": Columns.STACK_ABS_PATH,
     "stacktrace.package": Columns.STACK_PACKAGE,
+    "app.in_foreground": Columns.APP_IN_FOREGROUND,
 }
 
 
@@ -184,6 +185,17 @@ class EventAttributeCondition(EventCondition):
                 if device is None:
                     device = []
                 return [device.get(path[1])]
+
+        elif path[0] == "app":
+            if path[1] in ("in_foreground"):
+                contexts = event.data["contexts"]
+                response = contexts.get("app")
+                if response is None:
+                    response = {}
+                return [response.get(path[1])]
+
+            return []
+
         return []
 
     def render_label(self) -> str:

+ 8 - 0
src/sentry/snuba/events.py

@@ -559,6 +559,14 @@ class Columns(Enum):
         issue_platform_name="contexts.value",
         alias="contexts.value",
     )
+    APP_IN_FOREGROUND = Column(
+        group_name="events.contexts[app.in_foreground]",
+        event_name="contexts[app.in_foreground]",
+        transaction_name="contexts[app.in_foreground]",
+        discover_name="contexts[app.in_foreground]",
+        issue_platform_name="contexts[app.in_foreground]",
+        alias="app.in_foreground",
+    )
     # Transactions specific columns
     TRANSACTION_OP = Column(
         group_name=None,

+ 15 - 0
tests/sentry/rules/conditions/test_event_attribute.py

@@ -50,6 +50,9 @@ class EventAttributeConditionTest(RuleTestCase):
                     "screen_dpi": 123,
                     "screen_density": 2.5,
                 },
+                "app": {
+                    "in_foreground": True,
+                },
             },
         }
         data.update(kwargs)
@@ -727,3 +730,15 @@ class EventAttributeConditionTest(RuleTestCase):
             }
         )
         self.assertDoesNotPass(rule, event)
+
+    def test_app_in_foreground(self):
+        event = self.get_event()
+        rule = self.get_rule(
+            data={"match": MatchType.EQUAL, "attribute": "app.in_foreground", "value": "True"}
+        )
+        self.assertPasses(rule, event)
+
+        rule = self.get_rule(
+            data={"match": MatchType.EQUAL, "attribute": "app.in_foreground", "value": "False"}
+        )
+        self.assertDoesNotPass(rule, event)