def handle_trigger_action(action_id, incident_id, project_id, method, metric_value=None, **kwargs): try: action = AlertRuleTriggerAction.objects.select_related( "alert_rule_trigger", "alert_rule_trigger__alert_rule").get(id=action_id) except AlertRuleTriggerAction.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_action") return try: incident = Incident.objects.select_related("organization").get( id=incident_id) except Incident.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_incident") return try: project = Project.objects.get(id=project_id) except Project.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_project") return metrics.incr("incidents.alert_rules.action.{}.{}".format( AlertRuleTriggerAction.Type(action.type).name.lower(), method)) getattr(action, method)(action, incident, project, metric_value=metric_value)
def serialize(self, obj, attrs, user, **kwargs): from sentry.incidents.serializers import ACTION_TARGET_TYPE_TO_STRING result = { "id": str(obj.id), "alertRuleTriggerId": str(obj.alert_rule_trigger_id), "type": AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(obj.type)).slug, "targetType": ACTION_TARGET_TYPE_TO_STRING[AlertRuleTriggerAction.TargetType( obj.target_type)], "targetIdentifier": self.get_identifier_from_action(obj), "inputChannelId": self.get_input_channel_id(obj), "integrationId": obj.integration_id, "sentryAppId": obj.sentry_app_id, "dateCreated": obj.date_added, "desc": self.human_desc(obj), } # Check if action is a Sentry App that has Alert Rule UI Component settings if obj.sentry_app_id and obj.sentry_app_config: result["settings"] = obj.sentry_app_config return result
def serialize(self, obj, attrs, user): from sentry.incidents.endpoints.serializers import action_target_type_to_string return { "id": str(obj.id), "alertRuleTriggerId": str(obj.alert_rule_trigger_id), "type": AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(obj.type)).slug, "targetType": action_target_type_to_string[AlertRuleTriggerAction.TargetType( obj.target_type)], "targetIdentifier": self.get_identifier_from_action(obj), "integrationId": obj.integration_id, "sentryAppId": obj.sentry_app_id, "dateCreated": obj.date_added, "desc": self.human_desc(obj), }
def validate_type(self, type): try: return AlertRuleTriggerAction.Type(type) except ValueError: raise serializers.ValidationError( "Invalid type, valid values are %s" % [item.value for item in AlertRuleTriggerAction.Type])
def assert_action_serialized(self, action, result): assert result["id"] == six.text_type(action.id) assert result["alertRuleTriggerId"] == six.text_type( action.alert_rule_trigger_id) assert (result["type"] == AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(action.type)).slug) assert (result["targetType"] == action_target_type_to_string[ AlertRuleTriggerAction.TargetType(action.target_type)]) assert result["targetIdentifier"] == action.target_identifier assert result["integrationId"] == action.integration_id assert result["dateCreated"] == action.date_added
def handle_trigger_action(action_id, incident_id, project_id, method, metric_value=None, **kwargs): from sentry.incidents.logic import CRITICAL_TRIGGER_LABEL, WARNING_TRIGGER_LABEL try: action = AlertRuleTriggerAction.objects.select_related( "alert_rule_trigger", "alert_rule_trigger__alert_rule").get(id=action_id) except AlertRuleTriggerAction.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_action") return try: incident = Incident.objects.select_related("organization").get( id=incident_id) except Incident.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_incident") return try: project = Project.objects.get(id=project_id) except Project.DoesNotExist: metrics.incr("incidents.alert_rules.action.skipping_missing_project") return metrics.incr("incidents.alert_rules.action.{}.{}".format( AlertRuleTriggerAction.Type(action.type).name.lower(), method)) if method == "resolve": if (action.alert_rule_trigger.label == CRITICAL_TRIGGER_LABEL and AlertRuleTrigger.objects.filter( alert_rule=action.alert_rule_trigger.alert_rule, label=WARNING_TRIGGER_LABEL, ).exists()): # If we're resolving a critical trigger and a warning exists then we want to treat this # as if firing a warning, rather than resolving this trigger new_status = IncidentStatus.WARNING else: new_status = IncidentStatus.CLOSED else: if action.alert_rule_trigger.label == CRITICAL_TRIGGER_LABEL: new_status = IncidentStatus.CRITICAL else: new_status = IncidentStatus.WARNING getattr(action, method)(action, incident, project, metric_value=metric_value, new_status=new_status)
def serialize(self, obj, attrs, user): from sentry.incidents.endpoints.serializers import action_target_type_to_string return { "id": six.text_type(obj.id), "alertRuleTriggerId": six.text_type(obj.alert_rule_trigger_id), "type": AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(obj.type) ).slug, "targetType": action_target_type_to_string[ AlertRuleTriggerAction.TargetType(obj.target_type) ], "targetIdentifier": obj.target_display if obj.target_display is not None else obj.target_identifier, "integrationId": obj.integration_id, "dateCreated": obj.date_added, }
def test_not_updated_fields(self): self.create_member(user=self.user, organization=self.organization, role="owner", teams=[self.team]) self.login_as(self.user) with self.feature("organizations:incidents"): resp = self.get_valid_response( self.organization.slug, self.alert_rule.id, self.trigger.id, self.action.id, type=AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(self.action.type)).slug, targetType=action_target_type_to_string[ AlertRuleTriggerAction.TargetType( self.action.target_type)], targetIdentifier=self.action.target_identifier, ) # Alert rule should be exactly the same assert resp.data == serialize(self.action)
def test_simple(self): self.create_member(user=self.user, organization=self.organization, role="owner", teams=[self.team]) self.login_as(self.user) with self.feature("organizations:incidents"): resp = self.get_response( self.organization.slug, self.alert_rule.id, self.trigger.id, self.action.id, type=AlertRuleTriggerAction.get_registered_type( AlertRuleTriggerAction.Type(self.action.type)).slug, target_type=action_target_type_to_string[ AlertRuleTriggerAction.TargetType.TEAM], target_identifier=six.text_type(self.team.id), ) self.action.target_type = AlertRuleTriggerAction.TargetType.TEAM.value self.action.target_identifier = six.text_type(self.team.id) assert resp.data == serialize(self.action) assert resp.data["targetIdentifier"] == six.text_type(self.team.id)