o
    i|                     @   s   d dl Z d dlmZ d dlmZ d dlmZ zd dlmZ	 d dl
mZ d dlmZ W n ey7   dZdZ	Y nw d dlZdedB fd	d
ZG dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)KVConnectorPrometheus)PrometheusStatLogger)SpecDecodingProm)serve)metrics)Metricreturnc                   C   s4   t du rdS zt  jjW S  t jjy   Y dS w )zHGet the current Ray Serve replica ID, or None if not in a Serve context.N)	ray_serveget_replica_context
replica_id	unique_id
exceptionsRayServeException r   r   R/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/v1/metrics/ray_wrappers.py_get_replica_id   s   r   c                   @   sX   e Zd Zdd Zedee dB deedf fddZd	d
 Z	ededefddZ
dS )RayPrometheusMetricc                 C   s   t d u rtdd | _d S )Nz1RayPrometheusMetric requires Ray to be installed.)ray_metricsImportErrormetricselfr   r   r   __init__   s   
zRayPrometheusMetric.__init__
labelnamesNr   .c                 C   s"   | rt | ng }|d t|S )N	ReplicaId)listappendtuple)r   labelsr   r   r   _get_tag_keys#   s   
z!RayPrometheusMetric._get_tag_keysc                 O   s   |r&t | jjd }t ||krtd| dt | |t| jj| t p*d|d< |rI| D ]\}}t|t	sBt	|||< q3| j
| | S )N   z=Number of labels must match the number of tag keys. Expected z, got  r   )lenr   	_tag_keys
ValueErrorupdatezipr   items
isinstancestrset_default_tags)r   r   labelskwargsexpectedkvr   r   r   r   )   s&   
zRayPrometheusMetric.labelsnamec                 C   s   t dd| S )a  
        For compatibility with Ray + OpenTelemetry, the metric name must be
        sanitized. In particular, this replaces disallowed character (e.g., ':')
        with '_' in the metric name.
        Allowed characters: a-z, A-Z, 0-9, _

        # ruff: noqa: E501
        Ref: https://github.com/open-telemetry/opentelemetry-cpp/blob/main/sdk/src/metrics/instrument_metadata_validator.cc#L22-L23
        Ref: https://github.com/ray-project/ray/blob/master/src/ray/stats/metric.cc#L107
        z[^a-zA-Z0-9_]_)resub)r/   r   r   r   !_get_sanitized_opentelemetry_name=   s   z5RayPrometheusMetric._get_sanitized_opentelemetry_name)__name__
__module____qualname__r   staticmethodr   r)   r   r   r   r3   r   r   r   r   r      s    $r   c                
   @   s\   e Zd ZdZ			ddededB dee dB dedB fdd	Zd
eeB fddZ	dd Z
dS )RayGaugeWrapperzVWraps around ray.util.metrics.Gauge to provide same API as
    prometheus_client.Gauger!   Nr/   documentationr   multiprocess_modec                 C   s,   ~|  |}| |}tj|||d| _d S N)r/   descriptiontag_keys)r   r3   r   Gauger   )r   r/   r9   r   r:   r=   r   r   r   r   Q   s   


zRayGaugeWrapper.__init__valuec                 C      | j |S N)r   setr   r?   r   r   r   rB   f      zRayGaugeWrapper.setc                 C   s   | j t S rA   )r   rB   timer   r   r   r   set_to_current_timei   s   z#RayGaugeWrapper.set_to_current_time)r!   Nr!   )r4   r5   r6   __doc__r)   r   r   intfloatrB   rF   r   r   r   r   r8   M   s     

r8   c                   @   sL   e Zd ZdZ		ddededB dee dB fddZdd
eeB fddZ	dS )RayCounterWrapperzZWraps around ray.util.metrics.Counter to provide same API as
    prometheus_client.Counterr!   Nr/   r9   r   c                 C   s*   |  |}| |}tj|||d| _d S r;   )r   r3   r   Counterr   )r   r/   r9   r   r=   r   r   r   r   r   s   

zRayCounterWrapper.__init__      ?r?   c                 C   s   |dkrd S | j |S )Nr   )r   incrC   r   r   r   rM      s   zRayCounterWrapper.inc)r!   N)rL   )
r4   r5   r6   rG   r)   r   r   rH   rI   rM   r   r   r   r   rJ   n   s    

rJ   c                
   @   sX   e Zd ZdZ			ddededB dee dB dee dB fdd	Zd
eeB fddZ	dS )RayHistogramWrapperz^Wraps around ray.util.metrics.Histogram to provide same API as
    prometheus_client.Histogramr!   Nr/   r9   r   bucketsc                 C   s8   |  |}| |}|r|ng }tj||||d| _d S )N)r/   r<   r=   
boundaries)r   r3   r   	Histogramr   )r   r/   r9   r   rO   r=   rP   r   r   r   r      s   

zRayHistogramWrapper.__init__r?   c                 C   r@   rA   )r   observerC   r   r   r   rR      rD   zRayHistogramWrapper.observe)r!   NN)
r4   r5   r6   rG   r)   r   rI   r   rH   rR   r   r   r   r   rN      s    


rN   c                   @   s   e Zd ZdZeZdS )RaySpecDecodingPromz
    RaySpecDecodingProm is used by RayMetrics to log to Ray metrics.
    Provides the same metrics as SpecDecodingProm but uses Ray's
    util.metrics library.
    N)r4   r5   r6   rG   rJ   _counter_clsr   r   r   r   rS      s    rS   c                   @   s   e Zd ZdZeZeZeZ	dS )RayKVConnectorPrometheusz
    RayKVConnectorPrometheus is used by RayMetrics to log Ray
    metrics. Provides the same metrics as KV connectors but
    uses Ray's util.metrics library.
    N)
r4   r5   r6   rG   r8   
_gauge_clsrJ   rT   rN   _histogram_clsr   r   r   r   rU      s
    rU   c                   @   s0   e Zd ZdZeZeZeZ	e
ZeZedd ZdS )RayPrometheusStatLoggerz1RayPrometheusStatLogger uses Ray metrics instead.c                   C   s   d S rA   r   r   r   r   r   _unregister_vllm_metrics   s   z0RayPrometheusStatLogger._unregister_vllm_metricsN)r4   r5   r6   rG   r8   rV   rJ   rT   rN   rW   rS   _spec_decoding_clsrU   _kv_connector_clsr7   rY   r   r   r   r   rX      s    rX   )rE   4vllm.distributed.kv_transfer.kv_connector.v1.metricsr   vllm.v1.metrics.loggersr   vllm.v1.spec_decode.metricsr   rayr   r	   ray.utilr   r   ray.util.metricsr   r   regexr1   r)   r   r   r8   rJ   rN   rS   rU   rX   r   r   r   r   <module>   s*   
0!
