Tiny add sglang:http_requests_active metric (#16479)

This commit is contained in:
fzyzcjy
2026-01-05 17:20:31 +08:00
committed by GitHub
parent da2f8cc33f
commit 7f35c46efb
2 changed files with 20 additions and 8 deletions
+12 -1
View File
@@ -1502,7 +1502,7 @@ def add_prometheus_middleware(app):
def add_prometheus_track_response_middleware(app): def add_prometheus_track_response_middleware(app):
from prometheus_client import Counter from prometheus_client import Counter, Gauge
http_request_counter = Counter( http_request_counter = Counter(
name="sglang:http_requests_total", name="sglang:http_requests_total",
@@ -1516,6 +1516,13 @@ def add_prometheus_track_response_middleware(app):
labelnames=["endpoint", "status_code", "method"], labelnames=["endpoint", "status_code", "method"],
) )
http_requests_active = Gauge(
name="sglang:http_requests_active",
documentation="Number of currently active HTTP requests",
labelnames=["endpoint", "method"],
multiprocess_mode="livesum",
)
@app.middleware("http") @app.middleware("http")
async def track_http_status_code(request, call_next): async def track_http_status_code(request, call_next):
# With recording all requests, we have the risk of high cardinality if requests have arbitrary unhandled paths. # With recording all requests, we have the risk of high cardinality if requests have arbitrary unhandled paths.
@@ -1524,7 +1531,9 @@ def add_prometheus_track_response_middleware(app):
method = request.method method = request.method
http_request_counter.labels(endpoint=path, method=method).inc() http_request_counter.labels(endpoint=path, method=method).inc()
http_requests_active.labels(endpoint=path, method=method).inc()
try:
response = await call_next(request) response = await call_next(request)
http_response_counter.labels( http_response_counter.labels(
@@ -1534,6 +1543,8 @@ def add_prometheus_track_response_middleware(app):
).inc() ).inc()
return response return response
finally:
http_requests_active.labels(endpoint=path, method=method).dec()
# https://github.com/blueswen/fastapi-observability/blob/132a3c576f8b09e5311c68bd553215013bc75685/fastapi_app/utils.py#L98 # https://github.com/blueswen/fastapi-observability/blob/132a3c576f8b09e5311c68bd553215013bc75685/fastapi_app/utils.py#L98
+1
View File
@@ -127,6 +127,7 @@ class TestEnableMetrics(CustomTestCase):
"sglang:time_to_first_token_seconds", "sglang:time_to_first_token_seconds",
"sglang:inter_token_latency_seconds", "sglang:inter_token_latency_seconds",
"sglang:e2e_request_latency_seconds", "sglang:e2e_request_latency_seconds",
"sglang:http_requests_active",
] ]
for metric in essential_metrics: for metric in essential_metrics:
self.assertIn(metric, metrics_text, f"Missing metric: {metric}") self.assertIn(metric, metrics_text, f"Missing metric: {metric}")