Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rate limits and missing performance data #29

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion status_cake_exporter/_status_cake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
12 changes: 7 additions & 5 deletions status_cake_exporter/_test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down
Loading