From 9e24ff46a19fdbbe70dd20342dde20fe0b0fd91b Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Tue, 9 Dec 2025 14:28:24 +0800 Subject: [PATCH 1/6] more --- python/sglang/srt/utils/common.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index cf7264225dc6..a32a02a7d078 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1503,6 +1503,33 @@ def add_prometheus_middleware(app): metrics_route.path_regex = re.compile("^/metrics(?P.*)$") app.routes.append(metrics_route) + _register_promethus_middleware_counter(app) + + +def _register_promethus_middleware_counter(app): + from prometheus_client import Counter + + http_response_status_counter = Counter( + name="sglang:http_response_status_total", + documentation="Total number of HTTP responses by endpoint and status code", + labelnames=["endpoint", "status_code", "method"], + ) + + @app.middleware("http") + async def track_http_status_code(request, call_next): + endpoint = request.url.path + method = request.method + + response = await call_next(request) + + http_response_status_counter.labels( + endpoint=endpoint, + status_code=str(response.status_code), + method=method, + ).inc() + + return response + def bind_port(port): """Bind to a specific port, assuming it's available.""" From f17c0686d4ffee4c84045da377731fd8bb8be69a Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Tue, 9 Dec 2025 14:34:45 +0800 Subject: [PATCH 2/6] more --- python/sglang/srt/utils/common.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index a32a02a7d078..98ee5968bb05 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1517,15 +1517,16 @@ def _register_promethus_middleware_counter(app): @app.middleware("http") async def track_http_status_code(request, call_next): - endpoint = request.url.path - method = request.method - response = await call_next(request) + route = request.scope.get("route") + endpoint = route.path if route else "Unknown" + print(f"hi middleware {request.scope=} {route=} {type(route)=}") + http_response_status_counter.labels( endpoint=endpoint, status_code=str(response.status_code), - method=method, + method=request.method, ).inc() return response From 4e6e7e9fe61cd262f287e1800f79a282ad11f71d Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Tue, 9 Dec 2025 15:01:45 +0800 Subject: [PATCH 3/6] more --- python/sglang/srt/entrypoints/http_server.py | 4 ++++ python/sglang/srt/utils/common.py | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/entrypoints/http_server.py b/python/sglang/srt/entrypoints/http_server.py index cf0a3784fe8c..e51e0f0c166d 100644 --- a/python/sglang/srt/entrypoints/http_server.py +++ b/python/sglang/srt/entrypoints/http_server.py @@ -126,6 +126,7 @@ get_bool_env_var, kill_process_tree, set_uvicorn_logging_configs, + add_prometheus_track_response_middleware, ) from sglang.utils import get_exception_traceback from sglang.version import __version__ @@ -1397,6 +1398,9 @@ def launch_server( ) ) + if server_args.enable_metrics: + add_prometheus_track_response_middleware(app) + # Pass additional arguments to the lifespan function. # They will be used for additional initialization setups. if server_args.tokenizer_worker_num == 1: diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 98ee5968bb05..8c40470b3d8e 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1503,10 +1503,8 @@ def add_prometheus_middleware(app): metrics_route.path_regex = re.compile("^/metrics(?P.*)$") app.routes.append(metrics_route) - _register_promethus_middleware_counter(app) - -def _register_promethus_middleware_counter(app): +def add_prometheus_track_response_middleware(app): from prometheus_client import Counter http_response_status_counter = Counter( From 20fda71ab8b003dc0233d38d59e21d2a8826daec Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Tue, 9 Dec 2025 15:06:30 +0800 Subject: [PATCH 4/6] more --- python/sglang/srt/utils/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 8c40470b3d8e..852c7f9a1fb4 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1519,7 +1519,6 @@ async def track_http_status_code(request, call_next): route = request.scope.get("route") endpoint = route.path if route else "Unknown" - print(f"hi middleware {request.scope=} {route=} {type(route)=}") http_response_status_counter.labels( endpoint=endpoint, From 4f4d51c77124e4ab2e48df63c90bd32554660107 Mon Sep 17 00:00:00 2001 From: fzyzcjy Date: Tue, 9 Dec 2025 15:09:24 +0800 Subject: [PATCH 5/6] fmt --- python/sglang/srt/entrypoints/http_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/sglang/srt/entrypoints/http_server.py b/python/sglang/srt/entrypoints/http_server.py index e51e0f0c166d..5eb908d62bcb 100644 --- a/python/sglang/srt/entrypoints/http_server.py +++ b/python/sglang/srt/entrypoints/http_server.py @@ -122,11 +122,11 @@ from sglang.srt.utils import ( add_api_key_middleware, add_prometheus_middleware, + add_prometheus_track_response_middleware, delete_directory, get_bool_env_var, kill_process_tree, set_uvicorn_logging_configs, - add_prometheus_track_response_middleware, ) from sglang.utils import get_exception_traceback from sglang.version import __version__ From 464177c62a4acde723e6ad457a7d4ed388d5d3b1 Mon Sep 17 00:00:00 2001 From: fzyzcjy <5236035+fzyzcjy@users.noreply.github.com> Date: Tue, 9 Dec 2025 15:11:36 +0800 Subject: [PATCH 6/6] Update common.py --- python/sglang/srt/utils/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 852c7f9a1fb4..4723c555d753 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -1508,7 +1508,7 @@ def add_prometheus_track_response_middleware(app): from prometheus_client import Counter http_response_status_counter = Counter( - name="sglang:http_response_status_total", + name="sglang:http_responses_total", documentation="Total number of HTTP responses by endpoint and status code", labelnames=["endpoint", "status_code", "method"], )