o
    
۾i                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZ ee	Z
dedeeeef  deeef dB fd	d
Zdeeeef  fddZdedeeef dB fddZdS )zK
Utility functions that create ORCA endpoint load report response headers.
    N)Mapping)init_logger)Gaugeget_metrics_snapshotmetrics_formatnamed_metricsreturnc                 C   s   |   dvrtdt dS i }ddd |D i}|   dkr1dd	d
 |D }d| |d< n|   dkrAdt| |d< td| |S )a  
    Creates ORCA headers named 'endpoint-load-metrics' in the specified format
    and adds custom metrics to named_metrics.
    ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0
    ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto

    Parameters:
    - metrics_format (str): The format of the header ('TEXT', 'JSON').
    - named_metrics (List[Tuple[str, float]]): List of tuples with metric names
    and their corresponding double values.

    Returns:
    - Optional[Mapping[str,str]]: A dictionary with header key as
    'endpoint-load-metrics' and values as the ORCA header strings with
    format prefix and data in  with named_metrics in.
    )textjsonzAWarning: `%s` format is not supported in the ORCA response headerNr   c                 S   s*   i | ]\}}t |trt |tr||qS  
isinstancestrfloat.0metric_namevaluer   r   X/home/ubuntu/.local/lib/python3.10/site-packages/vllm/entrypoints/openai/orca_metrics.py
<dictcomp>-   s    z&create_orca_header.<locals>.<dictcomp>r	   z, c                 S   s4   g | ]\}}t |trt |trd | d| qS )znamed_metrics.=r   r   r   r   r   
<listcomp>7   s    z&create_orca_header.<locals>.<listcomp>zTEXT zendpoint-load-metricsr
   zJSON zCreated ORCA header %s)lowerloggerwarningformatjoinr
   dumpsinfo)r   r   headerorca_reportnative_http_headerr   r   r   create_orca_header   s,   	r"   c                  C   sX   g } ddd}t  }|D ]}||j}|dur)t|tr)| t|t|jf q| S )a  
    Collects current metrics from Prometheus and returns some of them
    in the form of the `named_metrics` list for `create_orca_header()`.

    Parameters:
    - None

    Returns:
    - list[tuple[str, float]]: List of tuples of metric names and their values.
    kv_cache_usage_percnum_requests_waiting)zvllm:kv_cache_usage_perczvllm:num_requests_waitingN)	r   getnamer   r   appendr   r   r   )r   prometheus_to_orca_metricsmetricsmetric	orca_namer   r   r   !get_named_metrics_from_prometheusI   s   r,   c                 C   s   | sdS t  }t| |S )a  
    Creates ORCA headers named 'endpoint-load-metrics' in the specified format.
    Metrics are collected from Prometheus using `get_named_metrics_from_prometheus()`.

    ORCA headers format description: https://docs.google.com/document/d/1C1ybMmDKJIVlrbOLbywhu9iRYo4rilR-cT50OTtOFTs/edit?tab=t.0
    ORCA proto https://github.com/cncf/xds/blob/main/xds/data/orca/v3/orca_load_report.proto

    Parameters:
    - metrics_format (str): The format of the header ('TEXT', 'JSON').

    Returns:
    - Optional[Mapping[str,str]]: A dictionary with header key as
    'endpoint-load-metrics' and values as the ORCA header strings with
    format prefix and data in  with named_metrics in.
    N)r,   r"   )r   r   r   r   r   metrics_headerd   s   
r-   )__doc__r
   collections.abcr   vllm.loggerr   vllm.v1.metrics.readerr   r   __name__r   r   listtupler   r"   r,   r-   r   r   r   r   <module>   s   
9"