|
| 1 | +import logging |
| 2 | + |
| 3 | +from sentry.issues.grouptype import MetricIssuePOC |
| 4 | +from sentry.models.organizationmember import OrganizationMember |
| 5 | +from sentry.models.team import Team |
| 6 | +from sentry.notifications.models.notificationaction import ActionTarget |
| 7 | +from sentry.notifications.notification_action.exceptions import NotificationHandlerException |
| 8 | +from sentry.notifications.notification_action.registry import ( |
| 9 | + group_type_notification_registry, |
| 10 | + metric_alert_handler_registry, |
| 11 | +) |
| 12 | +from sentry.notifications.notification_action.types import LegacyRegistryHandler |
| 13 | +from sentry.utils.registry import NoRegistrationExistsError |
| 14 | +from sentry.workflow_engine.models import Action, DataConditionGroupAction, Detector |
| 15 | +from sentry.workflow_engine.types import WorkflowEventData |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +@group_type_notification_registry.register(MetricIssuePOC.slug) |
| 21 | +class MetricAlertRegistryHandler(LegacyRegistryHandler): |
| 22 | + @staticmethod |
| 23 | + def handle_workflow_action(job: WorkflowEventData, action: Action, detector: Detector) -> None: |
| 24 | + try: |
| 25 | + handler = metric_alert_handler_registry.get(action.type) |
| 26 | + handler.invoke_legacy_registry(job, action, detector) |
| 27 | + except NoRegistrationExistsError: |
| 28 | + logger.exception( |
| 29 | + "No metric alert handler found for action type: %s", |
| 30 | + action.type, |
| 31 | + extra={"action_id": action.id}, |
| 32 | + ) |
| 33 | + raise |
| 34 | + except Exception as e: |
| 35 | + logger.exception( |
| 36 | + "Error invoking metric alert handler", |
| 37 | + extra={"action_id": action.id}, |
| 38 | + ) |
| 39 | + raise NotificationHandlerException(e) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def target(action: Action) -> OrganizationMember | Team | str | None: |
| 43 | + target_identifier = action.config.get("target_identifier") |
| 44 | + if target_identifier is None: |
| 45 | + return None |
| 46 | + |
| 47 | + target_type = action.config.get("target_type") |
| 48 | + if target_type == ActionTarget.USER.value: |
| 49 | + dcga = DataConditionGroupAction.objects.get(action=action) |
| 50 | + return OrganizationMember.objects.get( |
| 51 | + user_id=int(target_identifier), |
| 52 | + organization=dcga.condition_group.organization, |
| 53 | + ) |
| 54 | + elif target_type == ActionTarget.TEAM.value: |
| 55 | + try: |
| 56 | + return Team.objects.get(id=int(target_identifier)) |
| 57 | + except Team.DoesNotExist: |
| 58 | + pass |
| 59 | + elif target_type == ActionTarget.SPECIFIC.value: |
| 60 | + # TODO: This is only for email. We should have a way of validating that it's |
| 61 | + # ok to contact this email. |
| 62 | + return target_identifier |
| 63 | + return None |
0 commit comments