diff --git a/status_cake_exporter/_status_cake.py b/status_cake_exporter/_status_cake.py index d943d08..deea8b0 100644 --- a/status_cake_exporter/_status_cake.py +++ b/status_cake_exporter/_status_cake.py @@ -5,7 +5,7 @@ from statuscake import ApiClient, Configuration from statuscake.apis import MaintenanceWindowsApi, UptimeApi -from statuscake.exceptions import ApiValueError, ForbiddenException +from statuscake.exceptions import ApiValueError, ForbiddenException, ApiException from typing_extensions import NotRequired, TypedDict logger = logging.getLogger("status_cake") @@ -223,6 +223,14 @@ def get_test_history(self, test_id: str) -> list[dict[str, Any]]: response = uptime_api.list_uptime_test_history(test_id, **params) return response + except ApiException as e: + if e.status == 429: + backoff=int(e.headers["x-ratelimit-reset"]) + logger.debug(f"Hit statuscake API rate limit. Waiting {backoff} seconds before retrying...") + sleep(backoff) + return uptime_api.list_uptime_test_history(test_id, **params) + + raise e except Exception as e: logger.error(f"Error while fetching test history: {e}") raise e diff --git a/status_cake_exporter/_test_collector.py b/status_cake_exporter/_test_collector.py index 79227d4..39a60ac 100644 --- a/status_cake_exporter/_test_collector.py +++ b/status_cake_exporter/_test_collector.py @@ -78,8 +78,7 @@ def transform( for i in tests: logger.debug(f"Transforming test id: {i['id']}") - t.append( - { + test = { "test_id": str(i["id"]), "test_type": str( i["test_type"] @@ -88,12 +87,14 @@ def transform( "test_url": i["website_url"], "test_status_int": get_uptime_status(i["status"]), "test_uptime_percent": str(i["uptime"]), - "test_performance": str(i["performance"]), "maintenance_status_int": get_test_maintenance_status( i["id"], tests_in_maintenance ), } - ) + + if hasattr(i, 'performance'): + test["test_performance"]= str(i["performance"]) + t.append(test) logger.debug(f"Transformed test id: {i['id']}") logger.debug(f"Test transformation complete. Returning {len(t)} metrics") @@ -179,7 +180,8 @@ def collect(self): ) for i in metrics: - performance_gauge.add_metric([i["test_id"]], (i["test_performance"])) + if 'test_performance' in i.keys(): + performance_gauge.add_metric([i["test_id"]], (i["test_performance"])) yield performance_gauge