From 1028fdf587b2add206ddb1a72006a979173b71e9 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Sun, 25 Feb 2024 10:35:24 +0000 Subject: [PATCH] Add support for test performance --- status_cake_exporter/_status_cake.py | 48 +++++++++++++++++++++++++ status_cake_exporter/_test_collector.py | 14 ++++++++ 2 files changed, 62 insertions(+) diff --git a/status_cake_exporter/_status_cake.py b/status_cake_exporter/_status_cake.py index 84b8fbf..6a319c2 100644 --- a/status_cake_exporter/_status_cake.py +++ b/status_cake_exporter/_status_cake.py @@ -32,6 +32,10 @@ class ListUptimeTestParameters(PaginationParameters): tags: NotRequired[str] +class ListUptimeTestHistoryParameters(PaginationParameters): + """Parameters expected by the StatusCake API uptime history endpoint""" + + limit: NotRequired[int] class StatusCake: """ @@ -168,6 +172,13 @@ def list_tests(self, tags: str = "") -> list[dict]: uptime_api.list_uptime_tests, params, ) + + # Fetch the performance of each test and add it to the response + for test in response: + history = self.get_test_history(test["id"]) + test["performance"] = history["data"][0]["performance"] + + print(response) return response # https://github.com/StatusCakeDev/statuscake-py/issues/8 @@ -181,3 +192,40 @@ def list_tests(self, tags: str = "") -> list[dict]: except Exception as e: logger.error(f"Error while fetching tests: {e}") raise e + + """ + Fetches the history of a test + + Args: + test_id: [str] The ID of the test + + Returns: + list[dict[str, Any]] + + Raises: + Exception: If an error occurs while fetching the test history + """ + def get_test_history(self, test_id: str) -> list[dict[str, Any]]: + """ + Returns the history of a test + + Args: + test_id: [str] The ID of the test + + Returns: + list[dict[str, Any]] + + Raises: + Exception: If an error occurs while fetching the test history + """ + api_client = self.__get_api_client() + + try: + uptime_api: UptimeApi = UptimeApi(api_client) + params = ListUptimeTestHistoryParameters(limit=1) + response = uptime_api.list_uptime_test_history(test_id, **params) + return response + + except Exception as e: + logger.error(f"Error while fetching test history: {e}") + raise e \ No newline at end of file diff --git a/status_cake_exporter/_test_collector.py b/status_cake_exporter/_test_collector.py index b610110..c866241 100644 --- a/status_cake_exporter/_test_collector.py +++ b/status_cake_exporter/_test_collector.py @@ -88,6 +88,7 @@ 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 ), @@ -167,6 +168,19 @@ def collect(self): yield uptime_gauge + # status_cake_test_performance - gauge + logger.info(f"Publishing {len(metrics)} performance metric(s).") + performance_gauge = GaugeMetricFamily( + "status_cake_test_performance", + "Tests and their performance", + labels=["test_id"], + ) + + for i in metrics: + performance_gauge.add_metric([i["test_id"]], (i["test_performance"])) + + yield performance_gauge + except Exception as e: import traceback