inference/research
verifiedhigh · 0.93updated 2026-10-04

SGLang add_prometheus_middleware() - the /metrics mount and the flag that gates it

repo

Fields

id
sources/w3m-sglang-prometheus-middleware
Publisher
sgl-project/sglang
Kind
repo
Accessed
2026-10-04
notes
add_prometheus_middleware(app) at line 2738 of python/sglang/srt/utils/common.py on main, 2026-10-04: builds a fresh CollectorRegistry, attaches a multiprocess.MultiProcessCollector, and appends Mount("/metrics", make_asgi_app(registry=registry)) with path_regex '^/metrics(?P<path>.*)$' as a workaround for a 307 redirect. Its only call site is python/sglang/srt/entrypoints/http_server.py line 297, inside `if get_observability().enable_metrics:` - so the endpoint is on the main HTTP server port and does not exist at all unless the flag is set. A second middleware, add_prometheus_track_response_middleware, is installed at line 2627 under the same condition. This is the primary-source basis for endpoint='/metrics' and enabled_by_default=false on every SGLang metric_exposure record.

Notes

add_prometheus_middleware(app) at line 2738 of python/sglang/srt/utils/common.py on main, 2026-10-04: builds a fresh CollectorRegistry, attaches a multiprocess.MultiProcessCollector, and appends Mount("/metrics", make_asgi_app(registry=registry)) with path_regex '^/metrics(?P<path>.*)$' as a workaround for a 307 redirect. Its only call site is python/sglang/srt/entrypoints/http_server.py line 297, inside `if get_observability().enable_metrics:` - so the endpoint is on the main HTTP server port and does not exist at all unless the flag is set. A second middleware, add_prometheus_track_response_middleware, is installed at line 2627 under the same condition. This is the primary-source basis for endpoint='/metrics' and enabled_by_default=false on every SGLang metric_exposure record.

Cross-references

This record references no other records.