def send_incident_alert_notification(action,
                                     incident,
                                     metric_value=None,
                                     method=None):
    """
    When a metric alert is triggered, send incident data to the SentryApp's webhook.
    :param action: The triggered `AlertRuleTriggerAction`.
    :param incident: The `Incident` for which to build a payload.
    :param metric_value: The value of the metric that triggered this alert to
    fire. If not provided we'll attempt to calculate this ourselves.
    :return:
    """
    sentry_app = action.sentry_app
    organization = incident.organization
    metrics.incr("notifications.sent",
                 instance=sentry_app.slug,
                 skip_internal=False)

    try:
        install = SentryAppInstallation.objects.get(
            organization=organization.id,
            sentry_app=sentry_app,
            status=SentryAppInstallationStatus.INSTALLED,
        )
    except SentryAppInstallation.DoesNotExist:
        logger.info(
            "metric_alert_webhook.missing_installation",
            extra={
                "action": action.id,
                "incident": incident.id,
                "organization": organization.slug,
                "sentry_app_id": sentry_app.id,
            },
        )
        return

    send_and_save_webhook_request(
        sentry_app,
        AppPlatformEvent(
            resource="metric_alert",
            action=INCIDENT_STATUS[incident_status_info(
                incident, metric_value, action, method)].lower(),
            install=install,
            data=build_incident_attachment(action, incident, metric_value,
                                           method),
        ),
    )
Esempio n. 2
0
def build_incident_attachment(action,
                              incident,
                              integration_key,
                              metric_value=None,
                              method=None):
    data = incident_attachment_info(incident,
                                    metric_value,
                                    action=action,
                                    method=method)
    incident_status = incident_status_info(incident,
                                           metric_value,
                                           action=action,
                                           method=method)
    if incident_status == IncidentStatus.CRITICAL:
        severity = "critical"
    elif incident_status == IncidentStatus.WARNING:
        severity = "warning"
    elif incident_status == IncidentStatus.CLOSED:
        severity = "info"

    event_action = "resolve"
    if incident_status in [IncidentStatus.WARNING, IncidentStatus.CRITICAL]:
        event_action = "trigger"

    return {
        "routing_key": integration_key,
        "event_action": event_action,
        "dedup_key":
        f"incident_{incident.organization_id}_{incident.identifier}",
        "payload": {
            "summary": incident.alert_rule.name,
            "severity": severity,
            "source": str(incident.identifier),
            "custom_details": {
                "details": data["text"]
            },
        },
        "links": [{
            "href": data["title_link"],
            "text": data["title"]
        }],
    }