From 8cef6d853f0e8b84eb3624a3f7e6720db7f34d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20V=C3=B5sandi?= Date: Sun, 13 Feb 2022 22:02:51 +0200 Subject: [PATCH] Even more generalized exception handling --- camdetect.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/camdetect.py b/camdetect.py index ed0ba3f..0d1449f 100755 --- a/camdetect.py +++ b/camdetect.py @@ -27,39 +27,36 @@ THRESHOLD_BLOCKS = 20 THRESHOLD_MOTION_START = 2 counter_dropped_bytes = Counter( - "camtiler_client_dropped_bytes", + "camdetect_dropped_bytes", "Bytes that were not not handled or part of actual JPEG frames") counter_rx_bytes = Counter( - "camtiler_client_rx_bytes", + "camdetect_rx_bytes", "Bytes received over HTTP stream") counter_tx_bytes = Counter( - "camtiler_client_tx_bytes", + "camdetect_tx_bytes", "Bytes transmitted over HTTP streams") counter_rx_frames = Counter( - "camtiler_client_rx_frames", + "camdetect_rx_frames", "Frames received") counter_tx_frames = Counter( - "camtiler_client_tx_frames", + "camdetect_tx_frames", "Frames transmitted") counter_tx_events = Counter( - "camtiler_client_tx_events", + "camdetect_tx_events", "Events emitted") -counter_eos = Counter( - "camtiler_client_eos", - "Count of End of Stream occurrences") -counter_connection_errors = Counter( - "camtiler_client_connection_errors", +counter_errors = Counter( + "camdetect_errors", "Upstream connection errors", - ["error"]) + ["exception"]) counter_movement_frames = Counter( - "camtiler_client_movement_frames", + "camdetect_movement_frames", "Frames with movement detected in them") gauge_total_blocks = Gauge( - "camtiler_client_total_blocks", + "camdetect_total_blocks", "Total DCT blocks") gauge_active_blocks = Gauge( - "camtiler_client_active_blocks", + "camdetect_active_blocks", "Total active, threshold exceeding DCT blocks") class Frame(object): @@ -148,10 +145,11 @@ async def client(): try: async with session.get(url) as resp: await client_connect(resp) - except aiohttp.ClientConnectorError as e: - counter_connection_errors.labels(error=e.strerror).inc() - except asyncio.exceptions.TimeoutError: - counter_connection_errors.labels(error="Timeout").inc() + except (aiohttp.ClientError, asyncio.exceptions.TimeoutError) as e: + j = "%s.%s" % (e.__class__.__module__, e.__class__.__name__) + print("Caught exception %s" % j) + counter_errors.labels(exception=j).inc() + await asyncio.sleep(1) app = Sanic("lease")