Skip to content

Commit

Permalink
Add support for test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed Feb 25, 2024
1 parent 45c7852 commit 9d59171
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
38 changes: 38 additions & 0 deletions status_cake_exporter/_status_cake.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class ListUptimeTestParameters(PaginationParameters):
tags: NotRequired[str]


class ListUptimeTestHistoryParameters(PaginationParameters):
"""Parameters expected by the StatusCake API uptime history endpoint"""

limit: NotRequired[int]


class StatusCake:
"""
A wrapper class for the StatusCake API client.
Expand Down Expand Up @@ -169,6 +175,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
Expand All @@ -182,3 +195,28 @@ def list_tests(self, tags: str = "") -> list[dict]:
except Exception as e:
logger.error(f"Error while fetching tests: {e}")
raise e

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
14 changes: 14 additions & 0 deletions status_cake_exporter/_test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9d59171

Please sign in to comment.