litellm/litellm/proxy/prometheus_cleanup.py
Mateo Wang 48b5a5a0cc
style: unify ruff format width on 120 (#31518)
The repo linted at 120 (E501, isort) but ran ruff format at 88 via a
--line-length 88 override in the Makefile and CI, leaving the formatter
and the linter disagreeing on wrap width. Drop the override so ruff.toml's
line-length = 120 is the single source of truth and reformat the tree to
match.
2026-06-27 12:39:29 -07:00

39 lines
1.3 KiB
Python

"""
Prometheus multiprocess directory cleanup utilities.
Wipes all .db files on startup so workers start with a clean slate.
"""
from __future__ import annotations
import glob
import os
from litellm._logging import verbose_proxy_logger
def wipe_directory(directory: str) -> None:
"""Delete all .db files in the directory. Called once before workers fork."""
files = glob.glob(os.path.join(directory, "*.db"))
deleted = 0
for filepath in files:
try:
os.remove(filepath)
deleted += 1
except OSError as e:
verbose_proxy_logger.warning(f"Failed to delete stale prometheus file {filepath}: {e}")
if deleted:
verbose_proxy_logger.info(f"Prometheus cleanup: wiped {deleted} stale .db files from {directory}")
def mark_worker_exit(worker_pid: int) -> None:
"""Remove prometheus .db files for a dead worker. Called by gunicorn child_exit hook."""
if not os.environ.get("PROMETHEUS_MULTIPROC_DIR"):
return
try:
from prometheus_client import multiprocess
multiprocess.mark_process_dead(worker_pid)
verbose_proxy_logger.info(f"Prometheus cleanup: marked worker {worker_pid} as dead")
except Exception as e:
verbose_proxy_logger.warning(f"Failed to mark prometheus worker {worker_pid} as dead: {e}")