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: 18 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import requests
import time
from metrics import MetricsHandler


Comment on lines 17 to 18
Copy link
Contributor

Choose a reason for hiding this comment

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

undo this change, lets add the 2 lines back

from prometheus_client import generate_latest


Expand Down Expand Up @@ -131,7 +129,21 @@ 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():
"""
Returns the total disk usage of all Docker images in 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

cmd = [
"sh", "-c",
'docker images --format "{{.Size}}" | awk \'/GB/ {gsub("GB", ""); sum+=($1*1024*1024*1024)} /MB/ {gsub("MB", ""); sum+=($1*1024*1024)} /kB/ {gsub("kB", ""); sum+=($1*1024)} END {print int(sum)}\''
]
Copy link
Contributor

Choose a reason for hiding this comment

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

in your computer, whats the output of

docker system df --format {{json .}}

is there an "Images" section, if so, can we pull the size from there instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image Yes theres an image section and this is what it outputs in the terminal

result = subprocess.run(cmd, capture_output=True, text=True, check=True)
output = result.stdout.strip()
return int(output)
except Exception:
logging.exception("Error getting Docker image disk usage:")
return None

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 +224,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