vllm_mlx.metrics¶
Prometheus-first server metrics for vllm-mlx.
View the complete module source at #L1-L532.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.metrics
¶
Prometheus-first server metrics for vllm-mlx.
The public surface is a small internal abstraction that keeps instrumentation call sites stable even if we add OpenTelemetry export later.
vllm_mlx.metrics.InferenceTracker
dataclass
¶
InferenceTracker(collector: 'MetricsCollector | None', endpoint: str, stream: bool, start_time: float = perf_counter(), _finished: bool = False, _ttft_observed: bool = False)
Request-scoped inference timing and token accounting.
vllm_mlx.metrics.InferenceTracker.collector
instance-attribute
¶
vllm_mlx.metrics.InferenceTracker.start_time
class-attribute
instance-attribute
¶
vllm_mlx.metrics.InferenceTracker._finished
class-attribute
instance-attribute
¶
vllm_mlx.metrics.InferenceTracker._ttft_observed
class-attribute
instance-attribute
¶
vllm_mlx.metrics.InferenceTracker.observe_ttft
¶
Record time to first token once for this inference request.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.InferenceTracker.finish
¶
Record terminal latency and token counts once for this request.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector
¶
Lazy Prometheus-backed metrics collector.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector.enabled
property
¶
Return whether metric collection is enabled.
vllm_mlx.metrics.MetricsCollector.configure
¶
Enable or disable collection and lazily initialize Prometheus state.
vllm_mlx.metrics.MetricsCollector._init_prometheus
¶
Source code in vllm_mlx/metrics.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
vllm_mlx.metrics.MetricsCollector.track_inference
¶
track_inference(endpoint: str, *, stream: bool) -> InferenceTracker
Create request-scoped inference timing state for an endpoint.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector.observe_http_start
¶
Increment the in-flight request gauge for a normalized route.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector.observe_http_finish
¶
Record an HTTP result and decrement its in-flight gauge.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector.observe_inference
¶
observe_inference(*, endpoint: str, stream: bool, result: str, duration: float, prompt_tokens: int, completion_tokens: int) -> None
Record one terminal inference outcome, latency, and token totals.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector.observe_ttft
¶
Observe time to first token for a streaming or buffered request.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics.MetricsCollector._update_engine_gauges
¶
Source code in vllm_mlx/metrics.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
vllm_mlx.metrics.MetricsCollector.render_metrics
¶
Refresh runtime gauges and render Prometheus exposition bytes.
Raises:
-
RuntimeError–If metrics are disabled.
Source code in vllm_mlx/metrics.py
vllm_mlx.metrics._bool_str
¶
vllm_mlx.metrics._coerce_float
¶
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.metrics._bool_str · function
Function _bool_str returns 'true' if value else 'false'.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value |
bool |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'true' if value else 'false'
Exceptions and behavior
Function _bool_str returns 'true' if value else 'false'.
No direct raise statement appears in this definition.
vllm_mlx.metrics._coerce_float · function
Function _coerce_float calls float; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value |
Any |
yes |
none |
Required positional or keyword input. |
default |
float |
no |
0.0 |
Optional positional or keyword input; defaults to 0.0. |
Returns
- Type:
float - Direct return expressions:
default;float(value)
Exceptions and behavior
Function _coerce_float calls float; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.metrics._coerce_int · function
Function _coerce_int calls int; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value |
Any |
yes |
none |
Required positional or keyword input. |
default |
int |
no |
0 |
Optional positional or keyword input; defaults to 0. |
Returns
- Type:
int - Direct return expressions:
default;int(value)
Exceptions and behavior
Function _coerce_int calls int; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.metrics.InferenceTracker · class
vllm_mlx.metrics.InferenceTracker(collector: 'MetricsCollector | None', endpoint: str, stream: bool, start_time: float = field(default_factory=time.perf_counter), _finished: bool = False, _ttft_observed: bool = False)
Request-scoped inference timing and token accounting.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
collector |
'MetricsCollector \| None' |
yes |
none |
Required constructor field. |
endpoint |
str |
yes |
none |
Required constructor field. |
stream |
bool |
yes |
none |
Required constructor field. |
start_time |
float |
no |
field(default_factory=time.perf_counter) |
Optional constructor field; defaults to field(default_factory=time.perf_counter). |
_finished |
bool |
no |
False |
Optional constructor field; defaults to False. |
_ttft_observed |
bool |
no |
False |
Optional constructor field; defaults to False. |
Returns
- Constructs:
vllm_mlx.metrics.InferenceTracker
Exceptions and behavior
Class InferenceTracker declares 2 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.metrics.InferenceTracker.observe_ttft · method
Record time to first token once for this inference request.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method InferenceTracker.observe_ttft updates self._ttft_observed; calls self.collector.observe_ttft, time.perf_counter; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.InferenceTracker.finish · method
vllm_mlx.metrics.InferenceTracker.finish(*, result: str, prompt_tokens: int = 0, completion_tokens: int = 0) -> None
Record terminal latency and token counts once for this request.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
result |
str |
yes |
none |
Required keyword-only input. |
prompt_tokens |
int |
no |
0 |
Optional keyword-only input; defaults to 0. |
completion_tokens |
int |
no |
0 |
Optional keyword-only input; defaults to 0. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method InferenceTracker.finish updates self._finished; calls self.collector.observe_inference, time.perf_counter; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector · class
Lazy Prometheus-backed metrics collector.
Parameters
This callable has no explicit inputs.
Returns
- Constructs:
vllm_mlx.metrics.MetricsCollector
Exceptions and behavior
Class MetricsCollector declares 11 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.__init__ · method
Method MetricsCollector.__init__ updates self._enabled, self._lock, self._prom; calls threading.Lock.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MetricsCollector.__init__ updates self._enabled, self._lock, self._prom; calls threading.Lock.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.enabled · method
Return whether metric collection is enabled.
Parameters
This callable has no explicit inputs.
Returns
- Type:
bool - Direct return expressions:
self._enabled
Exceptions and behavior
Method MetricsCollector.enabled returns self._enabled.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.configure · method
Enable or disable collection and lazily initialize Prometheus state.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
enabled |
bool |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MetricsCollector.configure updates self._enabled; calls self._init_prometheus; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector._init_prometheus · method
Method MetricsCollector._init_prometheus updates self._prom; calls CollectorRegistry, Counter, Histogram, Gauge.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MetricsCollector._init_prometheus updates self._prom; calls CollectorRegistry, Counter, Histogram, Gauge.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.track_inference · method
vllm_mlx.metrics.MetricsCollector.track_inference(endpoint: str, *, stream: bool) -> InferenceTracker
Create request-scoped inference timing state for an endpoint.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
endpoint |
str |
yes |
none |
Required positional or keyword input. |
stream |
bool |
yes |
none |
Required keyword-only input. |
Returns
- Type:
InferenceTracker - Direct return expressions:
InferenceTracker(None, endpoint, stream);InferenceTracker(self, endpoint, stream)
Exceptions and behavior
Method MetricsCollector.track_inference calls InferenceTracker; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.observe_http_start · method
Increment the in-flight request gauge for a normalized route.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
method |
str |
yes |
none |
Required keyword-only input. |
path |
str |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MetricsCollector.observe_http_start calls self._prom['http_requests_in_flight'].labels(method=method, path=path).inc, self._prom['http_requests_in_flight'].labels; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.observe_http_finish · method
vllm_mlx.metrics.MetricsCollector.observe_http_finish(*, method: str, path: str, status_code: int, duration: float) -> None
Record an HTTP result and decrement its in-flight gauge.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
method |
str |
yes |
none |
Required keyword-only input. |
path |
str |
yes |
none |
Required keyword-only input. |
status_code |
int |
yes |
none |
Required keyword-only input. |
duration |
float |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MetricsCollector.observe_http_finish calls self._prom['http_requests_in_flight'].labels(method=method, path=path).dec, self._prom['http_requests_in_flight'].labels, self._prom['http_requests_total'].labels(method=method, path=path, status_code=str(status_code)).inc, self._prom['http_requests_total'].labels; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.observe_inference · method
vllm_mlx.metrics.MetricsCollector.observe_inference(*, endpoint: str, stream: bool, result: str, duration: float, prompt_tokens: int, completion_tokens: int) -> None
Record one terminal inference outcome, latency, and token totals.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
endpoint |
str |
yes |
none |
Required keyword-only input. |
stream |
bool |
yes |
none |
Required keyword-only input. |
result |
str |
yes |
none |
Required keyword-only input. |
duration |
float |
yes |
none |
Required keyword-only input. |
prompt_tokens |
int |
yes |
none |
Required keyword-only input. |
completion_tokens |
int |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MetricsCollector.observe_inference calls _bool_str, self._prom['inference_requests_total'].labels(endpoint=endpoint, stream=stream_label, result=result).inc, self._prom['inference_requests_total'].labels, self._prom['inference_request_duration_seconds'].labels(endpoint=endpoint, stream=stream_label).observe; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.observe_ttft · method
vllm_mlx.metrics.MetricsCollector.observe_ttft(*, endpoint: str, stream: bool, value: float) -> None
Observe time to first token for a streaming or buffered request.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
endpoint |
str |
yes |
none |
Required keyword-only input. |
stream |
bool |
yes |
none |
Required keyword-only input. |
value |
float |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MetricsCollector.observe_ttft calls self._prom['inference_ttft_seconds'].labels(endpoint=endpoint, stream=_bool_str(stream)).observe, self._prom['inference_ttft_seconds'].labels, _bool_str; returns None.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector._update_engine_gauges · method
vllm_mlx.metrics.MetricsCollector._update_engine_gauges(*, engine: Any | None, mcp_manager: Any | None) -> None
Method MetricsCollector._update_engine_gauges calls engine.get_stats, self._prom['model_loaded'].set, stats.get, self._prom['engine_type'].labels(engine_type=engine_type).set.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
engine |
Any \| None |
yes |
none |
Required keyword-only input. |
mcp_manager |
Any \| None |
yes |
none |
Required keyword-only input. |
Returns
- Type:
None
Exceptions and behavior
Method MetricsCollector._update_engine_gauges calls engine.get_stats, self._prom['model_loaded'].set, stats.get, self._prom['engine_type'].labels(engine_type=engine_type).set.
No direct raise statement appears in this definition.
vllm_mlx.metrics.MetricsCollector.render_metrics · method
vllm_mlx.metrics.MetricsCollector.render_metrics(*, engine: Any | None, mcp_manager: Any | None) -> tuple[bytes, str]
Refresh runtime gauges and render Prometheus exposition bytes.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
engine |
Any \| None |
yes |
none |
Required keyword-only input. |
mcp_manager |
Any \| None |
yes |
none |
Required keyword-only input. |
Returns
- Type:
tuple[bytes, str] - Direct return expressions:
(self._prom['generate_latest'](self._prom['registry']), self._prom['content_type'])
Exceptions and behavior
Method MetricsCollector.render_metrics calls RuntimeError, self._init_prometheus, self._update_engine_gauges, self._prom['generate_latest']; can raise RuntimeError; returns (self._prom['generate_latest'](self._prom['registry']), self._prom['content_type']).
Directly raised exceptions: RuntimeError.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
_bool_str |
function | _bool_str(value: bool) -> str |
Function _bool_str returns 'true' if value else 'false'. |
#L17-L18 |
_coerce_float |
function | _coerce_float(value: Any, default: float = 0.0) -> float |
Function _coerce_float calls float; has 2 explicit return paths. |
#L21-L27 |
_coerce_int |
function | _coerce_int(value: Any, default: int = 0) -> int |
Function _coerce_int calls int; has 2 explicit return paths. |
#L30-L36 |
InferenceTracker |
class | InferenceTracker(collector: 'MetricsCollector \| None', endpoint: str, stream: bool, start_time: float = field(default_factory=time.perf_counter), _finished: bool = False, _ttft_observed: bool = False) |
Request-scoped inference timing and token accounting. | #L40-L81 |
InferenceTracker.observe_ttft |
method | InferenceTracker.observe_ttft() -> None |
Record time to first token once for this inference request. | #L50-L60 |
InferenceTracker.finish |
method | InferenceTracker.finish(*, result: str, prompt_tokens: int = 0, completion_tokens: int = 0) -> None |
Record terminal latency and token counts once for this request. | #L62-L81 |
MetricsCollector |
class | MetricsCollector() |
Lazy Prometheus-backed metrics collector. | #L84-L529 |
MetricsCollector.__init__ |
method | MetricsCollector.__init__() -> None |
Method MetricsCollector.__init__ updates self._enabled, self._lock, self._prom; calls threading.Lock. |
#L87-L90 |
MetricsCollector.enabled |
method | MetricsCollector.enabled() -> bool |
Return whether metric collection is enabled. | #L93-L96 |
MetricsCollector.configure |
method | MetricsCollector.configure(*, enabled: bool) -> None |
Enable or disable collection and lazily initialize Prometheus state. | #L98-L105 |
MetricsCollector._init_prometheus |
method | MetricsCollector._init_prometheus() -> None |
Method MetricsCollector._init_prometheus updates self._prom; calls CollectorRegistry, Counter, Histogram, Gauge. |
#L107-L291 |
MetricsCollector.track_inference |
method | MetricsCollector.track_inference(endpoint: str, *, stream: bool) -> InferenceTracker |
Create request-scoped inference timing state for an endpoint. | #L293-L298 |
MetricsCollector.observe_http_start |
method | MetricsCollector.observe_http_start(*, method: str, path: str) -> None |
Increment the in-flight request gauge for a normalized route. | #L300-L305 |
MetricsCollector.observe_http_finish |
method | MetricsCollector.observe_http_finish(*, method: str, path: str, status_code: int, duration: float) -> None |
Record an HTTP result and decrement its in-flight gauge. | #L307-L328 |
MetricsCollector.observe_inference |
method | MetricsCollector.observe_inference(*, endpoint: str, stream: bool, result: str, duration: float, prompt_tokens: int, completion_tokens: int) -> None |
Record one terminal inference outcome, latency, and token totals. | #L330-L363 |
MetricsCollector.observe_ttft |
method | MetricsCollector.observe_ttft(*, endpoint: str, stream: bool, value: float) -> None |
Observe time to first token for a streaming or buffered request. | #L365-L373 |
MetricsCollector._update_engine_gauges |
method | MetricsCollector._update_engine_gauges(*, engine: Any \| None, mcp_manager: Any \| None) -> None |
Method MetricsCollector._update_engine_gauges calls engine.get_stats, self._prom['model_loaded'].set, stats.get, self._prom['engine_type'].labels(engine_type=engine_type).set. |
#L375-L507 |
MetricsCollector.render_metrics |
method | MetricsCollector.render_metrics(*, engine: Any \| None, mcp_manager: Any \| None) -> tuple[bytes, str] |
Refresh runtime gauges and render Prometheus exposition bytes. | #L509-L529 |