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
9 changes: 9 additions & 0 deletions fastdeploy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
import os
import subprocess
import sys
import uuid

# suppress warning log from paddlepaddle
os.environ["GLOG_minloglevel"] = "2"
# suppress log from aistudio
os.environ["AISTUDIO_LOG"] = "critical"
# set prometheus dir
if os.getenv("PROMETHEUS_MULTIPROC_DIR", "") == "":
prom_dir = f"/tmp/fd_prom_{str(uuid.uuid4())}"
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prom_dir
if os.path.exists(prom_dir):
os.rmdir(prom_dir)
os.mkdir(prom_dir)

import typing

from paddleformers.utils.log import logger as pf_logger
Expand Down
17 changes: 9 additions & 8 deletions fastdeploy/entrypoints/openai/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
from fastdeploy.envs import environment_variables
from fastdeploy.metrics.metrics import (
EXCLUDE_LABELS,
cleanup_prometheus_files,
get_filtered_metrics,
main_process_metrics,
)
Expand Down Expand Up @@ -630,17 +629,19 @@ def launch_metrics_server():
if not is_port_available(args.host, args.metrics_port):
raise Exception(f"The parameter `metrics_port`:{args.metrics_port} is already in use.")

prom_dir = cleanup_prometheus_files(True)
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prom_dir
# Move setting prometheus directory to fastdeploy/__init__.py
# prom_dir = cleanup_prometheus_files(True)
# os.environ["PROMETHEUS_MULTIPROC_DIR"] = prom_dir
metrics_server_thread = threading.Thread(target=run_metrics_server, daemon=True)
metrics_server_thread.start()
time.sleep(1)


def setup_metrics_environment():
"""Prepare Prometheus multiprocess directory before starting API workers."""
prom_dir = cleanup_prometheus_files(True)
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prom_dir
# NOTE: This is commented out since PROMETHEUS_MULTIPROC_DIR is already set up in fastdeploy/__init__.py
# def setup_metrics_environment():
# """Prepare Prometheus multiprocess directory before starting API workers."""
# prom_dir = cleanup_prometheus_files(True)
# os.environ["PROMETHEUS_MULTIPROC_DIR"] = prom_dir


controller_app = FastAPI()
Expand Down Expand Up @@ -755,7 +756,7 @@ def main():
launch_metrics_server()
console_logger.info(f"Launching metrics service at http://{args.host}:{args.metrics_port}/metrics")
else:
setup_metrics_environment()
# setup_metrics_environment()
console_logger.info(f"Launching metrics service at http://{args.host}:{args.port}/metrics")
console_logger.info(f"Launching chat completion service at http://{args.host}:{args.port}/v1/chat/completions")
console_logger.info(f"Launching completion service at http://{args.host}:{args.port}/v1/completions")
Expand Down
32 changes: 11 additions & 21 deletions tests/entrypoints/openai/test_metrics_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,17 @@ class WithDict:
assert "without_dict" in data and data["without_dict"] is None


def test_setup_metrics_environment_sets_env_var(tmp_path):
# Cover calling setup_metrics_environment()
with (
patch("fastdeploy.utils.FlexibleArgumentParser.parse_args") as mock_parse_args,
patch("fastdeploy.utils.retrive_model_from_server") as mock_retrive_model,
patch("fastdeploy.entrypoints.chat_utils.load_chat_template") as mock_load_template,
):
mock_parse_args.return_value = _build_mock_args()
mock_retrive_model.return_value = "test-model"
mock_load_template.return_value = None

from fastdeploy.entrypoints.openai import api_server as api_server_mod

api_server = importlib.reload(api_server_mod)

desired_dir = str(tmp_path / "prom_multiproc")

# Patch the name imported into api_server so we don't touch real FS
with patch("fastdeploy.entrypoints.openai.api_server.cleanup_prometheus_files", return_value=desired_dir):
api_server.setup_metrics_environment()
assert os.environ.get("PROMETHEUS_MULTIPROC_DIR") == desired_dir
def test_setup_metrics_environment_sets_env_var():
# Cover 'fastdeploy/__init__.py'
import fastdeploy

# clear PROMETHEUS_MULTIPROC_DIR
os.environ.pop("PROMETHEUS_MULTIPROC_DIR")
# reload fastdeploy
assert os.environ.get("PROMETHEUS_MULTIPROC_DIR") is None
importlib.reload(fastdeploy)
# check if PROMETHEUS_MULTIPROC_DIR is reinitialized
assert os.environ.get("PROMETHEUS_MULTIPROC_DIR").startswith("/tmp/fd_prom")


def test_metrics_app_routes_when_metrics_port_diff():
Expand Down
Loading