oncall-engine/engine/apps/webhooks/tasks/alert_group_status.py
Michael Derynck 3d74cbf3f5
Webhook 2 improvements and fixes (#1829)
- Rename Firing to Alert Group Created to reduce confusion as to why the
event only first once and not when unresolve or unacknowledge returns
the alert group to the firing state.
- Increase password field length
- Do not filter webhook execution by team, team is just for filtering
ownership now
- Do not log webhook triggers in alert group escalation log if the
webhook does not trigger (Status/response will still be stored)
- Fix formatting for response content and data fields on the Status page
- Add a content length limit for responses being stored (50000
characters)
2023-04-26 15:55:08 -06:00

75 lines
2.6 KiB
Python

import logging
from celery.utils.log import get_task_logger
from django.conf import settings
from apps.alerts.models import AlertGroup, AlertGroupLogRecord
from apps.webhooks.models import Webhook
from common.custom_celery_tasks import shared_dedicated_queue_retry_task
from .trigger_webhook import send_webhook_event
logger = get_task_logger(__name__)
logger.setLevel(logging.DEBUG)
MAX_RETRIES = 10
ACTION_TO_TRIGGER_TYPE = {
AlertGroupLogRecord.TYPE_ACK: Webhook.TRIGGER_ACKNOWLEDGE,
AlertGroupLogRecord.TYPE_RESOLVED: Webhook.TRIGGER_RESOLVE,
AlertGroupLogRecord.TYPE_SILENCE: Webhook.TRIGGER_SILENCE,
AlertGroupLogRecord.TYPE_UN_SILENCE: Webhook.TRIGGER_UNSILENCE,
AlertGroupLogRecord.TYPE_UN_RESOLVED: Webhook.TRIGGER_UNRESOLVE,
AlertGroupLogRecord.TYPE_UN_ACK: Webhook.TRIGGER_UNACKNOWLEDGE,
}
@shared_dedicated_queue_retry_task(
bind=True, autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else MAX_RETRIES
)
def alert_group_created(self, alert_group_id):
try:
alert_group = AlertGroup.unarchived_objects.get(pk=alert_group_id)
except AlertGroup.DoesNotExist:
return
trigger_type = Webhook.TRIGGER_ALERT_GROUP_CREATED
organization_id = alert_group.channel.organization_id
team_id = alert_group.channel.team_id
webhooks = Webhook.objects.filter(trigger_type=trigger_type, organization_id=organization_id, team_id=team_id)
# check if there are any webhooks before going on
if not webhooks:
return
send_webhook_event.apply_async(
(trigger_type, alert_group_id), kwargs={"organization_id": organization_id, "team_id": team_id}
)
@shared_dedicated_queue_retry_task(
bind=True, autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else MAX_RETRIES
)
def alert_group_status_change(self, action_type, alert_group_id, user_id):
try:
alert_group = AlertGroup.unarchived_objects.get(pk=alert_group_id)
except (AlertGroup.DoesNotExist):
return
trigger_type = ACTION_TO_TRIGGER_TYPE.get(action_type)
if trigger_type is None:
return
organization_id = alert_group.channel.organization_id
team_id = alert_group.channel.team_id
webhooks = Webhook.objects.filter(trigger_type=trigger_type, organization_id=organization_id, team_id=team_id)
# check if there are any webhooks before going on
if not webhooks:
return
send_webhook_event.apply_async(
(trigger_type, alert_group_id),
kwargs={"organization_id": organization_id, "team_id": team_id, "user_id": user_id},
)