Skip to content

Commit 3558d45

Browse files
author
Alex Chantavy
authored
Handle long URIs (lyft#31)
If an escalation policy has over 155 services, this script will create a URL that is too long for urllib3 to handle. This PR handles that case by splitting up that call into 2 chunks.
1 parent 306216b commit 3558d45

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pull_alerts.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import logging
6+
import urllib
67
from collections import Counter, OrderedDict, defaultdict, namedtuple
78
from datetime import datetime, timedelta
89
from dateutil import tz
@@ -45,11 +46,24 @@ def pretty_output(self):
4546

4647
def recent_incidents_for_services(services, time_window):
4748
service_ids = [service.id for service in services]
48-
recent_incidents = list(pagerduty_service.incidents.list(
49-
service_ids=service_ids,
50-
since=datetime.now() - time_window
51-
))
52-
return recent_incidents
49+
try:
50+
recent_incidents = list(pagerduty_service.incidents.list(
51+
service_ids=service_ids,
52+
since=datetime.now() - time_window
53+
))
54+
return recent_incidents
55+
56+
except urllib.error.HTTPError as e:
57+
if e.reason == 'URI Too Long':
58+
mid_point = int(len(service_ids)/2)
59+
return recent_incidents_for_services(
60+
service_ids=service_ids[:mid_point],
61+
time_window=time_window
62+
) + recent_incidents_for_services(
63+
service_ids=service_ids[mid_point:],
64+
time_window=time_window
65+
)
66+
raise
5367

5468

5569
def print_all_incidents(

0 commit comments

Comments
 (0)