Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manually retry for requests.exceptions.Timeout exceptions when sending outgoing webhooks #3632

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed schedule timezone issues ([#3576](https://github.com/grafana/oncall/issues/3576))
- Ignore `requests.exceptions.Timeout` exceptions when attempting to send outgoing webhook requests by @joeyorlando ([#3632](https://github.com/grafana/oncall/pull/3632))

## v1.3.82 (2024-01-04)

Expand Down
36 changes: 29 additions & 7 deletions engine/apps/webhooks/tasks/trigger_webhook.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import logging
import typing
from datetime import datetime
from json import JSONDecodeError

import requests
from celery.utils.log import get_task_logger
from django.conf import settings
from django.db.models import Prefetch
Expand Down Expand Up @@ -40,6 +43,17 @@
}


class WebhookRequestStatus(typing.TypedDict):
url: typing.Optional[str]
request_trigger: typing.Optional[str]
request_headers: typing.Optional[str]
request_data: typing.Optional[str]
status_code: typing.Optional[int]
content: typing.Optional[str]
webhook: Webhook
event_data: str


@shared_dedicated_queue_retry_task(
autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else None
)
Expand All @@ -52,15 +66,14 @@ def send_webhook_event(trigger_type, alert_group_id, organization_id=None, user_
).exclude(is_webhook_enabled=False)

for webhook in webhooks_qs:
print(webhook.name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presumably we don't need this?

execute_webhook.apply_async((webhook.pk, alert_group_id, user_id, None))


def _isoformat_date(date_value):
def _isoformat_date(date_value: datetime) -> typing.Optional[str]:
return date_value.isoformat() if date_value else None


def _build_payload(webhook, alert_group, user):
def _build_payload(webhook: Webhook, alert_group: AlertGroup, user: User) -> typing.Dict[str, typing.Any]:
trigger_type = webhook.trigger_type
event = {
"type": TRIGGER_TYPE_TO_LABEL[trigger_type],
Expand Down Expand Up @@ -96,7 +109,9 @@ def _build_payload(webhook, alert_group, user):
return data


def mask_authorization_header(headers, header_keys_to_mask):
def mask_authorization_header(
headers: typing.Dict[str, str], header_keys_to_mask: typing.List[str]
) -> typing.Dict[str, str]:
masked_headers = headers.copy()
lower_keys = set(k.lower() for k in header_keys_to_mask)
for k in headers.keys():
Expand All @@ -105,8 +120,10 @@ def mask_authorization_header(headers, header_keys_to_mask):
return masked_headers


def make_request(webhook, alert_group, data):
status = {
def make_request(
webhook: Webhook, alert_group: AlertGroup, data: typing.Dict[str, typing.Any]
) -> typing.Tuple[bool, WebhookRequestStatus, typing.Optional[str], typing.Optional[Exception]]:
status: WebhookRequestStatus = {
"url": None,
"request_trigger": None,
"request_headers": None,
Expand Down Expand Up @@ -172,7 +189,12 @@ def make_request(webhook, alert_group, data):


@shared_dedicated_queue_retry_task(
autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else 3
autoretry_for=(Exception,),
# This allows to exclude some exceptions that match autoretry_for but for which you don’t want a retry.
# https://docs.celeryq.dev/en/stable/userguide/tasks.html#Task.dont_autoretry_for
dont_autoretry_for=(requests.exceptions.Timeout,),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching this error will catch both requests.exceptions.ConnectTimeout and requests.exceptions.ReadTimeout errors (source code):

  • requests.exceptions.ConnectTimeout: "The request timed out while trying to connect to the remote server"
  • requests.exceptions.ReadTimeout: "The server did not send any data in the allotted amount of time"

both of these seem out of our control

retry_backoff=True,
max_retries=1 if settings.DEBUG else 3,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change (rest is just adding some type hints)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative here would be to not use dont_autoretry_for and instead manually retry the task if isinstance(exception, requests.exceptions.Timeout) (up to a max of 3 after which point we simply return). This would allow to still retry for these tasks but without having to raise an exception (and avoiding triggering our AmixrRetriedFailedTasks alert)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be preferred to have some limited retry to cover customer network hiccups

)
def execute_webhook(webhook_pk, alert_group_id, user_id, escalation_policy_id):
from apps.webhooks.models import Webhook
Expand Down