|
1 | 1 | from typing import Any
|
2 | 2 |
|
| 3 | +import sentry_sdk |
| 4 | + |
| 5 | +from sentry.eventstore.models import GroupEvent |
| 6 | +from sentry.rules import MatchType, match_values |
| 7 | +from sentry.rules.conditions.event_attribute import attribute_registry |
| 8 | +from sentry.utils import json |
| 9 | +from sentry.utils.registry import NoRegistrationExistsError |
3 | 10 | from sentry.workflow_engine.models.data_condition import Condition
|
4 | 11 | from sentry.workflow_engine.registry import condition_handler_registry
|
5 | 12 | from sentry.workflow_engine.types import DataConditionHandler, WorkflowJob
|
@@ -29,3 +36,53 @@ class EventSeenCountConditionHandler(DataConditionHandler[WorkflowJob]):
|
29 | 36 | def evaluate_value(job: WorkflowJob, comparison: Any) -> bool:
|
30 | 37 | event = job["event"]
|
31 | 38 | return event.group.times_seen == comparison
|
| 39 | + |
| 40 | + |
| 41 | +@condition_handler_registry.register(Condition.EVENT_ATTRIBUTE) |
| 42 | +class EventAttributeConditionHandler(DataConditionHandler[WorkflowJob]): |
| 43 | + @staticmethod |
| 44 | + def get_attribute_values(event: GroupEvent, attribute: str) -> list[str]: |
| 45 | + path = attribute.split(".") |
| 46 | + first_attribute = path[0] |
| 47 | + try: |
| 48 | + attribute_handler = attribute_registry.get(first_attribute) |
| 49 | + except NoRegistrationExistsError: |
| 50 | + attribute_handler = None |
| 51 | + |
| 52 | + if not attribute_handler: |
| 53 | + attribute_values = [] |
| 54 | + else: |
| 55 | + try: |
| 56 | + attribute_values = attribute_handler.handle(path, event) |
| 57 | + except KeyError as e: |
| 58 | + attribute_values = [] |
| 59 | + sentry_sdk.capture_exception(e) |
| 60 | + |
| 61 | + attribute_values = [str(value).lower() for value in attribute_values if value is not None] |
| 62 | + |
| 63 | + return attribute_values |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def evaluate_value(job: WorkflowJob, comparison: Any) -> bool: |
| 67 | + comparison_dict = json.loads(comparison) |
| 68 | + |
| 69 | + event = job["event"] |
| 70 | + attribute = comparison_dict.get("attribute", "") |
| 71 | + attribute_values = EventAttributeConditionHandler.get_attribute_values(event, attribute) |
| 72 | + |
| 73 | + match = comparison_dict.get("match") |
| 74 | + desired_value = comparison_dict.get("value") |
| 75 | + if not (match and desired_value) and not (match in (MatchType.IS_SET, MatchType.NOT_SET)): |
| 76 | + return False |
| 77 | + |
| 78 | + desired_value = str(desired_value).lower() |
| 79 | + |
| 80 | + # NOTE: IS_SET condition differs btw tagged_event and event_attribute so not handled by match_values |
| 81 | + if match == MatchType.IS_SET: |
| 82 | + return bool(attribute_values) |
| 83 | + elif match == MatchType.NOT_SET: |
| 84 | + return not attribute_values |
| 85 | + |
| 86 | + return match_values( |
| 87 | + group_values=attribute_values, match_value=desired_value, match_type=match |
| 88 | + ) |
0 commit comments