Skip to content

Commit

Permalink
Merge pull request #8 from eliashaeussler/fix/timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler authored Jul 6, 2024
2 parents 2543b85 + bb10e95 commit a0120db
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions metrics_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import sys
import time
import urllib
from urllib.request import urlopen
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY
Expand All @@ -17,8 +18,8 @@ class DbTrainMetricsCollector():
Collect and export DB train metrics.
"""

STATUS_URL="https://iceportal.de/api1/rs/status"
TRIP_URL="https://iceportal.de/api1/rs/tripInfo/trip"
STATUS_URL = "https://iceportal.de/api1/rs/status"
TRIP_URL = "https://iceportal.de/api1/rs/tripInfo/trip"

def __init__(self, logger):
self.logger = logger
Expand Down Expand Up @@ -64,8 +65,11 @@ def collect_speed(self):
"""

try:
with urlopen(self.STATUS_URL) as res:
with urlopen(self.STATUS_URL, timeout=20) as res:
status_response = json.load(res)
except urllib.error.URLError as error:
self.logger.error('Error while fetching JSON from %s: %s', self.STATUS_URL, str(error))
return None
except json.decoder.JSONDecodeError as error:
self.logger.error('Error while decoding JSON from %s: %s', self.STATUS_URL, str(error))
return None
Expand All @@ -78,8 +82,11 @@ def collect_trip(self):
"""

try:
with urlopen(self.TRIP_URL) as res:
with urlopen(self.TRIP_URL, timeout=20) as res:
trip_response = json.load(res)
except urllib.error.URLError as error:
self.logger.error('Error while fetching JSON from %s: %s', self.TRIP_URL, str(error))
return None, None, None
except json.decoder.JSONDecodeError as error:
self.logger.error('Error while decoding JSON from %s: %s', self.TRIP_URL, str(error))
return None, None, None
Expand Down

0 comments on commit a0120db

Please sign in to comment.