Skip to content
5 changes: 5 additions & 0 deletions metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Metrics(enum.Enum):
prometheus_client.Gauge,
["repo"],
)
DOCKER_IMAGE_DISK_USAGE_BYTES = (
"docker_image_disk_usage_bytes",
"Total disk usage of all Docker images in bytes",
prometheus_client.Gauge,
)

def __init__(self, title, description, prometheus_type, labels=()):
# we use the above default value for labels because it matches what's used
Expand Down
21 changes: 20 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,23 @@ def push_update_success_as_discord_embed(
)
except Exception:
logger.exception("push_update_success_as_discord_embed had a bad time")

def get_docker_images_disk_usage_bytes():
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a try, do we have an except for this? similar to 132

# Get docker system df output as JSON lines
result = subprocess.run(
["docker", "system", "df", "--format", "{{json .}}"],
capture_output=True, text=True, check=True
)
for line in result.stdout.splitlines():
data = json.loads(line)
if data.get("Type") == "Images":
size_str = data["Size"] # e.g., "8.423GB"
num, unit = float(size_str[:-2]), size_str[-2:]
multipliers = {"GB": 1024**3, "MB": 1024**2, "kB": 1024, "B": 1}
return int(num * multipliers.get(unit, 1))
return None
except Exception:
logger.exception("Error getting Docker image disk usage")

def update_repo(repo_config: RepoToWatch) -> RepoUpdateResult:
MetricsHandler.last_push_timestamp.labels(repo=repo_config.name).set(time.time())
Expand Down Expand Up @@ -212,6 +228,9 @@ async def github_webhook(request: Request):

@app.get("/metrics")
def get_metrics():
usage = get_docker_images_disk_usage_bytes()
if usage is not None:
docker_image_disk_usage_bytes.set(usage)
return Response(
media_type="text/plain",
content=generate_latest(),
Expand Down