|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +Utility functions that create ORCA endpoint load report response headers. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +from collections.abc import Mapping |
| 9 | + |
| 10 | +from vllm.logger import init_logger |
| 11 | +from vllm.v1.metrics.reader import Gauge, get_metrics_snapshot |
| 12 | + |
| 13 | +logger = init_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def create_orca_header( |
| 17 | + metrics_format: str, named_metrics: list[tuple[str, float]] |
| 18 | +) -> Mapping[str, str] | None: |
| 19 | + """ |
| 20 | + Creates ORCA headers named 'endpoint-load-metrics' in the specified format |
| 21 | + and adds custom metrics to named_metrics. |
| 22 | + ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0 |
| 23 | + ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto |
| 24 | +
|
| 25 | + Parameters: |
| 26 | + - metrics_format (str): The format of the header ('TEXT', 'JSON'). |
| 27 | + - named_metrics (List[Tuple[str, float]]): List of tuples with metric names |
| 28 | + and their corresponding double values. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + - Optional[Mapping[str,str]]: A dictionary with header key as |
| 32 | + 'endpoint-load-metrics' and values as the ORCA header strings with |
| 33 | + format prefix and data in with named_metrics in. |
| 34 | + """ |
| 35 | + |
| 36 | + if metrics_format.lower() not in ["text", "json"]: |
| 37 | + logger.warning( |
| 38 | + "Warning: `%s` format is not supported in the ORCA response header", |
| 39 | + format, |
| 40 | + ) |
| 41 | + return None |
| 42 | + |
| 43 | + header = {} |
| 44 | + orca_report = { |
| 45 | + "named_metrics": { |
| 46 | + metric_name: value |
| 47 | + for metric_name, value in named_metrics |
| 48 | + if isinstance(metric_name, str) and isinstance(value, float) |
| 49 | + } |
| 50 | + } |
| 51 | + # output example: |
| 52 | + # endpoint-load-metrics: TEXT named_metrics.kv_cache_utilization=0.4 |
| 53 | + if metrics_format.lower() == "text": |
| 54 | + native_http_header = ", ".join( |
| 55 | + [ |
| 56 | + f"named_metrics.{metric_name}={value}" |
| 57 | + for metric_name, value in named_metrics |
| 58 | + if isinstance(metric_name, str) and isinstance(value, float) |
| 59 | + ] |
| 60 | + ) |
| 61 | + header["endpoint-load-metrics"] = f"TEXT {native_http_header}" |
| 62 | + |
| 63 | + # output example: |
| 64 | + # endpoint-load-metrics: JSON “named_metrics”: {“custom-metric-util”: 0.4} |
| 65 | + elif metrics_format.lower() == "json": |
| 66 | + header["endpoint-load-metrics"] = f"JSON {json.dumps(orca_report)}" |
| 67 | + |
| 68 | + logger.info("Created ORCA header %s", header) |
| 69 | + |
| 70 | + return header |
| 71 | + |
| 72 | + |
| 73 | +def get_named_metrics_from_prometheus() -> list[tuple[str, float]]: |
| 74 | + """ |
| 75 | + Collects current metrics from Prometheus and returns some of them |
| 76 | + in the form of the `named_metrics` list for `create_orca_header()`. |
| 77 | +
|
| 78 | + Parameters: |
| 79 | + - None |
| 80 | +
|
| 81 | + Returns: |
| 82 | + - list[tuple[str, float]]: List of tuples of metric names and their values. |
| 83 | + """ |
| 84 | + named_metrics: list[tuple[str, float]] = [] |
| 85 | + # Map from prometheus metric names to ORCA named metrics. |
| 86 | + prometheus_to_orca_metrics = { |
| 87 | + "vllm:kv_cache_usage_perc": "kv_cache_usage_perc", |
| 88 | + "vllm:num_requests_waiting": "num_requests_waiting", |
| 89 | + } |
| 90 | + metrics = get_metrics_snapshot() |
| 91 | + for metric in metrics: |
| 92 | + orca_name = prometheus_to_orca_metrics.get(metric.name) |
| 93 | + # If this metric is mapped into ORCA, then add it to the report. |
| 94 | + # Note: Only Gauge metrics are currently supported. |
| 95 | + if orca_name is not None and isinstance(metric, Gauge): |
| 96 | + named_metrics.append((str(orca_name), float(metric.value))) |
| 97 | + return named_metrics |
| 98 | + |
| 99 | + |
| 100 | +def metrics_header(metrics_format: str) -> Mapping[str, str] | None: |
| 101 | + """ |
| 102 | + Creates ORCA headers named 'endpoint-load-metrics' in the specified format. |
| 103 | + Metrics are collected from Prometheus using `get_named_metrics_from_prometheus()`. |
| 104 | +
|
| 105 | + ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0 |
| 106 | + ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto |
| 107 | +
|
| 108 | + Parameters: |
| 109 | + - metrics_format (str): The format of the header ('TEXT', 'JSON'). |
| 110 | +
|
| 111 | + Returns: |
| 112 | + - Optional[Mapping[str,str]]: A dictionary with header key as |
| 113 | + 'endpoint-load-metrics' and values as the ORCA header strings with |
| 114 | + format prefix and data in with named_metrics in. |
| 115 | + """ |
| 116 | + if not metrics_format: |
| 117 | + return None |
| 118 | + # Get named metrics from prometheus. |
| 119 | + named_metrics = get_named_metrics_from_prometheus() |
| 120 | + return create_orca_header(metrics_format, named_metrics) |
0 commit comments