Skip to content

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.metrics module-attribute

metrics = MetricsCollector()

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

collector: 'MetricsCollector | None'

vllm_mlx.metrics.InferenceTracker.endpoint instance-attribute

endpoint: str

vllm_mlx.metrics.InferenceTracker.stream instance-attribute

stream: bool

vllm_mlx.metrics.InferenceTracker.start_time class-attribute instance-attribute

start_time: float = field(default_factory=time.perf_counter)

vllm_mlx.metrics.InferenceTracker._finished class-attribute instance-attribute

_finished: bool = False

vllm_mlx.metrics.InferenceTracker._ttft_observed class-attribute instance-attribute

_ttft_observed: bool = False

vllm_mlx.metrics.InferenceTracker.observe_ttft

observe_ttft() -> None

Record time to first token once for this inference request.

Source code in vllm_mlx/metrics.py
def observe_ttft(self) -> None:
    """Record time to first token once for this inference request."""

    if self.collector is None or self._ttft_observed:
        return
    self.collector.observe_ttft(
        endpoint=self.endpoint,
        stream=self.stream,
        value=time.perf_counter() - self.start_time,
    )
    self._ttft_observed = True

vllm_mlx.metrics.InferenceTracker.finish

finish(*, result: str, prompt_tokens: int = 0, completion_tokens: int = 0) -> None

Record terminal latency and token counts once for this request.

Source code in vllm_mlx/metrics.py
def finish(
    self,
    *,
    result: str,
    prompt_tokens: int = 0,
    completion_tokens: int = 0,
) -> None:
    """Record terminal latency and token counts once for this request."""

    if self.collector is None or self._finished:
        return
    self.collector.observe_inference(
        endpoint=self.endpoint,
        stream=self.stream,
        result=result,
        duration=time.perf_counter() - self.start_time,
        prompt_tokens=prompt_tokens,
        completion_tokens=completion_tokens,
    )
    self._finished = True

vllm_mlx.metrics.MetricsCollector

MetricsCollector()

Lazy Prometheus-backed metrics collector.

Source code in vllm_mlx/metrics.py
def __init__(self) -> None:
    self._enabled = False
    self._lock = threading.Lock()
    self._prom = None

vllm_mlx.metrics.MetricsCollector._enabled instance-attribute

_enabled = False

vllm_mlx.metrics.MetricsCollector._lock instance-attribute

_lock = threading.Lock()

vllm_mlx.metrics.MetricsCollector._prom instance-attribute

_prom = None

vllm_mlx.metrics.MetricsCollector.enabled property

enabled: bool

Return whether metric collection is enabled.

vllm_mlx.metrics.MetricsCollector.configure

configure(*, enabled: bool) -> None

Enable or disable collection and lazily initialize Prometheus state.

Source code in vllm_mlx/metrics.py
def configure(self, *, enabled: bool) -> None:
    """Enable or disable collection and lazily initialize Prometheus state."""

    with self._lock:
        self._enabled = enabled
        if not enabled or self._prom is not None:
            return
        self._init_prometheus()

vllm_mlx.metrics.MetricsCollector._init_prometheus

_init_prometheus() -> None
Source code in vllm_mlx/metrics.py
def _init_prometheus(self) -> None:
    from prometheus_client import (
        CONTENT_TYPE_LATEST,
        CollectorRegistry,
        Counter,
        Gauge,
        Histogram,
        generate_latest,
    )

    registry = CollectorRegistry(auto_describe=True)
    self._prom = {
        "registry": registry,
        "generate_latest": generate_latest,
        "content_type": CONTENT_TYPE_LATEST,
        "http_requests_total": Counter(
            "vllm_mlx_http_requests_total",
            "HTTP requests handled by the server.",
            ["method", "path", "status_code"],
            registry=registry,
        ),
        "http_request_duration_seconds": Histogram(
            "vllm_mlx_http_request_duration_seconds",
            "HTTP request latency in seconds.",
            ["method", "path"],
            registry=registry,
            buckets=(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30),
        ),
        "http_requests_in_flight": Gauge(
            "vllm_mlx_http_requests_in_flight",
            "HTTP requests currently in flight.",
            ["method", "path"],
            registry=registry,
        ),
        "inference_requests_total": Counter(
            "vllm_mlx_inference_requests_total",
            "Inference requests completed by endpoint.",
            ["endpoint", "stream", "result"],
            registry=registry,
        ),
        "inference_request_duration_seconds": Histogram(
            "vllm_mlx_inference_request_duration_seconds",
            "End-to-end inference latency in seconds.",
            ["endpoint", "stream"],
            registry=registry,
            buckets=(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60),
        ),
        "inference_ttft_seconds": Histogram(
            "vllm_mlx_inference_ttft_seconds",
            "Time to first token for streaming endpoints.",
            ["endpoint", "stream"],
            registry=registry,
            buckets=(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10),
        ),
        "prompt_tokens_total": Counter(
            "vllm_mlx_prompt_tokens_total",
            "Prompt/input tokens processed by endpoint.",
            ["endpoint", "stream"],
            registry=registry,
        ),
        "completion_tokens_total": Counter(
            "vllm_mlx_completion_tokens_total",
            "Generated output tokens produced by endpoint.",
            ["endpoint", "stream"],
            registry=registry,
        ),
        "model_loaded": Gauge(
            "vllm_mlx_model_loaded",
            "Whether a generation model is currently loaded.",
            registry=registry,
        ),
        "engine_type": Gauge(
            "vllm_mlx_engine_type",
            "Current engine mode.",
            ["engine_type"],
            registry=registry,
        ),
        "engine_is_mllm": Gauge(
            "vllm_mlx_engine_is_mllm",
            "Whether the loaded engine is multimodal.",
            registry=registry,
        ),
        "scheduler_waiting_requests": Gauge(
            "vllm_mlx_scheduler_waiting_requests",
            "Requests currently waiting in the scheduler.",
            registry=registry,
        ),
        "scheduler_running_requests": Gauge(
            "vllm_mlx_scheduler_running_requests",
            "Requests currently running in the scheduler.",
            registry=registry,
        ),
        "engine_steps_executed": Gauge(
            "vllm_mlx_engine_steps_executed",
            "Scheduler/engine steps executed since startup.",
            registry=registry,
        ),
        "engine_uptime_seconds": Gauge(
            "vllm_mlx_engine_uptime_seconds",
            "Engine uptime in seconds.",
            registry=registry,
        ),
        "metal_memory_bytes": Gauge(
            "vllm_mlx_metal_memory_bytes",
            "Metal memory usage in bytes.",
            ["kind"],
            registry=registry,
        ),
        "cache_type": Gauge(
            "vllm_mlx_cache_type",
            "Current active cache backend.",
            ["cache_type"],
            registry=registry,
        ),
        "cache_entry_count": Gauge(
            "vllm_mlx_cache_entry_count",
            "Cache entries or allocated blocks, depending on cache type.",
            registry=registry,
        ),
        "cache_hits": Gauge(
            "vllm_mlx_cache_hits",
            "Cache hits since startup/reset.",
            registry=registry,
        ),
        "cache_misses": Gauge(
            "vllm_mlx_cache_misses",
            "Cache misses since startup/reset.",
            registry=registry,
        ),
        "cache_evictions": Gauge(
            "vllm_mlx_cache_evictions",
            "Cache evictions since startup/reset.",
            registry=registry,
        ),
        "cache_hit_rate": Gauge(
            "vllm_mlx_cache_hit_rate",
            "Cache hit rate.",
            registry=registry,
        ),
        "cache_utilization_ratio": Gauge(
            "vllm_mlx_cache_utilization_ratio",
            "Cache utilization ratio.",
            registry=registry,
        ),
        "cache_memory_bytes": Gauge(
            "vllm_mlx_cache_memory_bytes",
            "Cache memory usage in bytes.",
            registry=registry,
        ),
        "cache_memory_limit_bytes": Gauge(
            "vllm_mlx_cache_memory_limit_bytes",
            "Cache memory limit in bytes when available.",
            registry=registry,
        ),
        "cache_tokens_saved": Gauge(
            "vllm_mlx_cache_tokens_saved",
            "Prompt tokens saved by cache reuse since startup/reset.",
            registry=registry,
        ),
        "model_registry_entries": Gauge(
            "vllm_mlx_model_registry_entries",
            "Tracked model ownership entries.",
            registry=registry,
        ),
        "model_registry_active_owners": Gauge(
            "vllm_mlx_model_registry_active_owners",
            "Active model owners in the registry.",
            registry=registry,
        ),
        "mcp_connected_servers": Gauge(
            "vllm_mlx_mcp_connected_servers",
            "Connected MCP servers.",
            registry=registry,
        ),
        "mcp_total_servers": Gauge(
            "vllm_mlx_mcp_total_servers",
            "Configured MCP servers.",
            registry=registry,
        ),
        "mcp_tools_available": Gauge(
            "vllm_mlx_mcp_tools_available",
            "Available MCP tools.",
            registry=registry,
        ),
    }

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
def track_inference(self, endpoint: str, *, stream: bool) -> InferenceTracker:
    """Create request-scoped inference timing state for an endpoint."""

    if not self._enabled:
        return InferenceTracker(None, endpoint, stream)
    return InferenceTracker(self, endpoint, stream)

vllm_mlx.metrics.MetricsCollector.observe_http_start

observe_http_start(*, method: str, path: str) -> None

Increment the in-flight request gauge for a normalized route.

Source code in vllm_mlx/metrics.py
def observe_http_start(self, *, method: str, path: str) -> None:
    """Increment the in-flight request gauge for a normalized route."""

    if not self._enabled or self._prom is None:
        return
    self._prom["http_requests_in_flight"].labels(method=method, path=path).inc()

vllm_mlx.metrics.MetricsCollector.observe_http_finish

observe_http_finish(*, method: str, path: str, status_code: int, duration: float) -> None

Record an HTTP result and decrement its in-flight gauge.

Source code in vllm_mlx/metrics.py
def observe_http_finish(
    self,
    *,
    method: str,
    path: str,
    status_code: int,
    duration: float,
) -> None:
    """Record an HTTP result and decrement its in-flight gauge."""

    if not self._enabled or self._prom is None:
        return
    self._prom["http_requests_in_flight"].labels(method=method, path=path).dec()
    self._prom["http_requests_total"].labels(
        method=method,
        path=path,
        status_code=str(status_code),
    ).inc()
    self._prom["http_request_duration_seconds"].labels(
        method=method,
        path=path,
    ).observe(duration)

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
def observe_inference(
    self,
    *,
    endpoint: str,
    stream: bool,
    result: str,
    duration: float,
    prompt_tokens: int,
    completion_tokens: int,
) -> None:
    """Record one terminal inference outcome, latency, and token totals."""

    if not self._enabled or self._prom is None:
        return
    stream_label = _bool_str(stream)
    self._prom["inference_requests_total"].labels(
        endpoint=endpoint,
        stream=stream_label,
        result=result,
    ).inc()
    self._prom["inference_request_duration_seconds"].labels(
        endpoint=endpoint,
        stream=stream_label,
    ).observe(duration)
    if prompt_tokens > 0:
        self._prom["prompt_tokens_total"].labels(
            endpoint=endpoint,
            stream=stream_label,
        ).inc(prompt_tokens)
    if completion_tokens > 0:
        self._prom["completion_tokens_total"].labels(
            endpoint=endpoint,
            stream=stream_label,
        ).inc(completion_tokens)

vllm_mlx.metrics.MetricsCollector.observe_ttft

observe_ttft(*, endpoint: str, stream: bool, value: float) -> None

Observe time to first token for a streaming or buffered request.

Source code in vllm_mlx/metrics.py
def observe_ttft(self, *, endpoint: str, stream: bool, value: float) -> None:
    """Observe time to first token for a streaming or buffered request."""

    if not self._enabled or self._prom is None:
        return
    self._prom["inference_ttft_seconds"].labels(
        endpoint=endpoint,
        stream=_bool_str(stream),
    ).observe(value)

vllm_mlx.metrics.MetricsCollector._update_engine_gauges

_update_engine_gauges(*, engine: Any | None, mcp_manager: Any | None) -> None
Source code in vllm_mlx/metrics.py
def _update_engine_gauges(
    self,
    *,
    engine: Any | None,
    mcp_manager: Any | None,
) -> None:
    assert self._prom is not None

    stats = engine.get_stats() if engine is not None else {}

    self._prom["model_loaded"].set(1 if engine is not None else 0)
    current_engine_type = (
        stats.get("engine_type", "unknown") if engine else "unknown"
    )
    for engine_type in ("simple", "batched", "unknown"):
        self._prom["engine_type"].labels(engine_type=engine_type).set(
            1 if current_engine_type == engine_type else 0
        )
    self._prom["engine_is_mllm"].set(1 if stats.get("is_mllm") else 0)

    self._prom["scheduler_waiting_requests"].set(
        _coerce_int(stats.get("num_waiting"))
    )
    self._prom["scheduler_running_requests"].set(
        _coerce_int(stats.get("num_running"))
    )
    self._prom["engine_steps_executed"].set(
        _coerce_int(stats.get("steps_executed"))
    )
    self._prom["engine_uptime_seconds"].set(
        _coerce_float(stats.get("uptime_seconds"))
    )

    self._prom["metal_memory_bytes"].labels(kind="active").set(
        _coerce_float(stats.get("metal_active_memory_gb")) * 1e9
    )
    self._prom["metal_memory_bytes"].labels(kind="peak").set(
        _coerce_float(stats.get("metal_peak_memory_gb")) * 1e9
    )
    self._prom["metal_memory_bytes"].labels(kind="cache").set(
        _coerce_float(stats.get("metal_cache_memory_gb")) * 1e9
    )

    cache_type = "none"
    cache_stats = None
    for candidate in ("memory_aware_cache", "paged_cache", "prefix_cache"):
        if candidate in stats:
            cache_type = candidate
            cache_stats = stats[candidate]
            break

    for candidate in ("none", "prefix_cache", "memory_aware_cache", "paged_cache"):
        self._prom["cache_type"].labels(cache_type=candidate).set(
            1 if cache_type == candidate else 0
        )

    if isinstance(cache_stats, dict):
        self._prom["cache_entry_count"].set(
            _coerce_float(
                cache_stats.get(
                    "entry_count", cache_stats.get("allocated_blocks", 0)
                )
            )
        )
        self._prom["cache_hits"].set(
            _coerce_float(cache_stats.get("hits", cache_stats.get("cache_hits", 0)))
        )
        self._prom["cache_misses"].set(
            _coerce_float(
                cache_stats.get("misses", cache_stats.get("cache_misses", 0))
            )
        )
        self._prom["cache_evictions"].set(
            _coerce_float(cache_stats.get("evictions", 0))
        )
        self._prom["cache_hit_rate"].set(
            _coerce_float(
                cache_stats.get("hit_rate", cache_stats.get("cache_hit_rate", 0.0))
            )
        )
        self._prom["cache_utilization_ratio"].set(
            _coerce_float(
                cache_stats.get(
                    "memory_utilization", cache_stats.get("utilization", 0.0)
                )
            )
        )
        self._prom["cache_tokens_saved"].set(
            _coerce_float(cache_stats.get("tokens_saved", 0))
        )
        self._prom["cache_memory_bytes"].set(
            _coerce_float(cache_stats.get("current_memory_mb", 0.0)) * 1024 * 1024
        )
        self._prom["cache_memory_limit_bytes"].set(
            _coerce_float(cache_stats.get("max_memory_mb", 0.0)) * 1024 * 1024
        )
    else:
        self._prom["cache_entry_count"].set(0)
        self._prom["cache_hits"].set(0)
        self._prom["cache_misses"].set(0)
        self._prom["cache_evictions"].set(0)
        self._prom["cache_hit_rate"].set(0)
        self._prom["cache_utilization_ratio"].set(0)
        self._prom["cache_tokens_saved"].set(0)
        self._prom["cache_memory_bytes"].set(0)
        self._prom["cache_memory_limit_bytes"].set(0)

    try:
        from .model_registry import get_registry

        registry_stats = get_registry().get_stats()
    except Exception:
        registry_stats = {}
    self._prom["model_registry_entries"].set(
        _coerce_int(registry_stats.get("total_entries"))
    )
    self._prom["model_registry_active_owners"].set(
        _coerce_int(registry_stats.get("active_owners"))
    )

    if mcp_manager is not None:
        try:
            statuses = list(mcp_manager.get_server_status())
            connected = sum(1 for s in statuses if s.state.value == "connected")
            total = len(statuses)
            tools = len(mcp_manager.get_all_tools())
        except Exception:
            connected = total = tools = 0
    else:
        connected = total = tools = 0
    self._prom["mcp_connected_servers"].set(connected)
    self._prom["mcp_total_servers"].set(total)
    self._prom["mcp_tools_available"].set(tools)

vllm_mlx.metrics.MetricsCollector.render_metrics

render_metrics(*, engine: Any | None, mcp_manager: Any | None) -> tuple[bytes, str]

Refresh runtime gauges and render Prometheus exposition bytes.

Raises:

  • RuntimeError

    If metrics are disabled.

Source code in vllm_mlx/metrics.py
def render_metrics(
    self,
    *,
    engine: Any | None,
    mcp_manager: Any | None,
) -> tuple[bytes, str]:
    """Refresh runtime gauges and render Prometheus exposition bytes.

    Raises:
        RuntimeError: If metrics are disabled.
    """

    if not self._enabled:
        raise RuntimeError("metrics_disabled")
    if self._prom is None:
        self._init_prometheus()
    self._update_engine_gauges(engine=engine, mcp_manager=mcp_manager)
    return (
        self._prom["generate_latest"](self._prom["registry"]),
        self._prom["content_type"],
    )

vllm_mlx.metrics._bool_str

_bool_str(value: bool) -> str
Source code in vllm_mlx/metrics.py
def _bool_str(value: bool) -> str:
    return "true" if value else "false"

vllm_mlx.metrics._coerce_float

_coerce_float(value: Any, default: float = 0.0) -> float
Source code in vllm_mlx/metrics.py
def _coerce_float(value: Any, default: float = 0.0) -> float:
    try:
        if value is None:
            return default
        return float(value)
    except (TypeError, ValueError):
        return default

vllm_mlx.metrics._coerce_int

_coerce_int(value: Any, default: int = 0) -> int
Source code in vllm_mlx/metrics.py
def _coerce_int(value: Any, default: int = 0) -> int:
    try:
        if value is None:
            return default
        return int(value)
    except (TypeError, ValueError):
        return default

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
vllm_mlx.metrics._bool_str(value: bool) -> str

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.

View source #L17-L18.

vllm_mlx.metrics._coerce_float · function
vllm_mlx.metrics._coerce_float(value: Any, default: float = 0.0) -> float

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.

View source #L21-L27.

vllm_mlx.metrics._coerce_int · function
vllm_mlx.metrics._coerce_int(value: Any, default: int = 0) -> int

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.

View source #L30-L36.

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.

View source #L40-L81.

vllm_mlx.metrics.InferenceTracker.observe_ttft · method
vllm_mlx.metrics.InferenceTracker.observe_ttft() -> None

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.

View source #L50-L60.

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.

View source #L62-L81.

vllm_mlx.metrics.MetricsCollector · class
vllm_mlx.metrics.MetricsCollector()

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.

View source #L84-L529.

vllm_mlx.metrics.MetricsCollector.__init__ · method
vllm_mlx.metrics.MetricsCollector.__init__() -> None

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.

View source #L87-L90.

vllm_mlx.metrics.MetricsCollector.enabled · method
vllm_mlx.metrics.MetricsCollector.enabled() -> bool

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.

View source #L93-L96.

vllm_mlx.metrics.MetricsCollector.configure · method
vllm_mlx.metrics.MetricsCollector.configure(*, enabled: bool) -> None

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.

View source #L98-L105.

vllm_mlx.metrics.MetricsCollector._init_prometheus · method
vllm_mlx.metrics.MetricsCollector._init_prometheus() -> None

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.

View source #L107-L291.

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.

View source #L293-L298.

vllm_mlx.metrics.MetricsCollector.observe_http_start · method
vllm_mlx.metrics.MetricsCollector.observe_http_start(*, method: str, path: str) -> None

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.

View source #L300-L305.

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.

View source #L307-L328.

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.

View source #L330-L363.

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.

View source #L365-L373.

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.

View source #L375-L507.

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.

View source #L509-L529.

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