[APIServer] metrics use port the same as api_port (#5016)

* metrics use port the same as api_port

* Be tolerant to tests that monkeypatch/partially mock args.

* Reduce code redundancy

---------

Co-authored-by: YuBaoku <49938469+EmmonsCurse@users.noreply.github.com>
This commit is contained in:
zhouchong
2025-11-17 10:42:45 +08:00
committed by GitHub
parent 68f638f8b9
commit 5444af6ff6
3 changed files with 295 additions and 3 deletions

View File

@@ -543,6 +543,13 @@ def launch_api_server() -> None:
metrics_app = FastAPI()
# Be tolerant to tests that monkeypatch/partially mock args.
_metrics_port = getattr(args, "metrics_port", None)
_main_port = getattr(args, "port", None)
if _metrics_port is None or (_main_port is not None and _metrics_port == _main_port):
metrics_app = app
@metrics_app.get("/metrics")
async def metrics():
@@ -599,6 +606,12 @@ def launch_metrics_server():
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
controller_app = FastAPI()
@@ -707,13 +720,17 @@ def main():
if not load_data_service():
return
api_server_logger.info("FastDeploy LLM engine initialized!\n")
console_logger.info(f"Launching metrics service at http://{args.host}:{args.metrics_port}/metrics")
if args.metrics_port is not None and args.metrics_port != args.port:
launch_metrics_server()
console_logger.info(f"Launching metrics service at http://{args.host}:{args.metrics_port}/metrics")
else:
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")
launch_worker_monitor()
launch_controller_server()
launch_metrics_server()
launch_api_server()