Skip to content
Merged
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
22 changes: 15 additions & 7 deletions python/sglang/srt/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,25 +2329,30 @@ def dec(self, key: str):
self._gauge.dec()


def add_prometheus_track_response_middleware(app):
def add_prometheus_track_response_middleware(
app, extra_labels: Optional[Dict[str, str]] = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pass extra labels from the HTTP server

In the normal HTTP-server launch path, the only in-repo call remains add_prometheus_track_response_middleware(app) in python/sglang/srt/entrypoints/http_server.py:2370, so ServerArgs.extra_metric_labels never reaches this new parameter. Deployments using --extra-metric-labels will still get those labels on scheduler/tokenizer metrics but not on these HTTP counters/gauges, which defeats the new filtering/joining use case this parameter is meant to support.

Useful? React with 👍 / 👎.

):
from prometheus_client import Counter, Gauge

extra_labels = extra_labels or {}
extra_label_names = list(extra_labels.keys())

http_request_counter = Counter(
name="sglang:http_requests_total",
documentation="Total number of HTTP requests by endpoint and method",
labelnames=["endpoint", "method"],
labelnames=extra_label_names + ["endpoint", "method"],
)

http_response_counter = Counter(
name="sglang:http_responses_total",
documentation="Total number of HTTP responses by endpoint and status code",
labelnames=["endpoint", "status_code", "method"],
labelnames=extra_label_names + ["endpoint", "status_code", "method"],
)

http_requests_active = Gauge(
name="sglang:http_requests_active",
documentation="Number of currently active HTTP requests",
labelnames=["endpoint", "method"],
labelnames=extra_label_names + ["endpoint", "method"],
multiprocess_mode="livesum",
)

Expand All @@ -2373,23 +2378,26 @@ async def track_http_status_code(request, call_next):
method = request.method
routing_key = request.headers.get("x-smg-routing-key")

http_request_counter.labels(endpoint=path, method=method).inc()
http_requests_active.labels(endpoint=path, method=method).inc()
http_request_counter.labels(**extra_labels, endpoint=path, method=method).inc()
http_requests_active.labels(**extra_labels, endpoint=path, method=method).inc()
if routing_key:
routing_keys_active.inc(routing_key)

try:
response = await call_next(request)

http_response_counter.labels(
**extra_labels,
endpoint=path,
method=method,
status_code=str(response.status_code),
).inc()

return response
finally:
http_requests_active.labels(endpoint=path, method=method).dec()
http_requests_active.labels(
**extra_labels, endpoint=path, method=method
).dec()
if routing_key:
routing_keys_active.dec(routing_key)

Expand Down
Loading