Skip to content
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
4 changes: 4 additions & 0 deletions pythonkuma/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ class UptimeKumaAuthenticationException(UptimeKumaException):
"""Uptime Kuma authentication exception."""


class UptimeKumaParseException(UptimeKumaException):
"""Exception raised when parsing of Prometheus metrics fails."""


class UpdateException(Exception):
"""Exception raised for errors fetching latest release from github."""
60 changes: 34 additions & 26 deletions pythonkuma/uptimekuma.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from prometheus_client.parser import text_string_to_metric_families
from yarl import URL

from .exceptions import UptimeKumaAuthenticationException, UptimeKumaConnectionException
from .exceptions import (
UptimeKumaAuthenticationException,
UptimeKumaConnectionException,
UptimeKumaParseException,
)
from .models import UptimeKumaMonitor, UptimeKumaVersion


Expand Down Expand Up @@ -89,28 +93,32 @@ async def metrics(self) -> dict[str | int, UptimeKumaMonitor]:
raise UptimeKumaConnectionException(msg, str(url)) from e
except ClientError as e:
raise UptimeKumaConnectionException from e
else:
for metric in text_string_to_metric_families(await request.text()):
if metric.name == "app_version" and metric.samples:
self.version = UptimeKumaVersion.from_dict(metric.samples[0].labels)
if not metric.name.startswith("monitor"):
continue
for sample in metric.samples:
key = (
int(monitor_id)
if (monitor_id := sample.labels.get("monitor_id"))
else sample.labels["monitor_name"]
)

name = (
f"{sample.name}_{window}"
if (window := sample.labels.get("window"))
else sample.name
)

monitors.setdefault(key, sample.labels).update({name: sample.value})

return {
key: UptimeKumaMonitor.from_dict(value)
for key, value in monitors.items()
}

try:
metrics = set(text_string_to_metric_families(await request.text()))
except ValueError as e:
raise UptimeKumaParseException from e

for metric in metrics:
if metric.name == "app_version" and metric.samples:
self.version = UptimeKumaVersion.from_dict(metric.samples[0].labels)
if not metric.name.startswith("monitor"):
continue
for sample in metric.samples:
key = (
int(monitor_id)
if (monitor_id := sample.labels.get("monitor_id"))
else sample.labels["monitor_name"]
)

name = (
f"{sample.name}_{window}"
if (window := sample.labels.get("window"))
else sample.name
)

monitors.setdefault(key, sample.labels).update({name: sample.value})

return {
key: UptimeKumaMonitor.from_dict(value) for key, value in monitors.items()
}
Loading