
    .`i|                     T   d dl Z d dlmZ d dlmZ d dlmZ 	 d dlmZ	 d dl
mZ d dlmZ n# e$ r dZdZ	Y nw xY wd dlZdedz  fd	Z G d
 d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)KVConnectorPrometheus)PrometheusStatLogger)SpecDecodingProm)serve)metrics)Metricreturnc                      t           dS 	 t          j                    j        j        S # t           j        j        $ r Y dS w xY 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     p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/v1/metrics/ray_wrappers.py_get_replica_idr      sO    t,..9CC1   tts   ( A A c                   |    e Zd Zd Zedee         dz  deedf         fd            Zd Z	ededefd	            Z
dS )
RayPrometheusMetricc                 @    t           t          d          d | _        d S )Nz1RayPrometheusMetric requires Ray to be installed.)ray_metricsImportErrormetricselfs    r   __init__zRayPrometheusMetric.__init__   s"    QRRR"r   
labelnamesNr	   .c                 p    | rt          |           ng }|                    d           t          |          S )N	ReplicaId)listappendtuple)r   labelss     r   _get_tag_keysz!RayPrometheusMetric._get_tag_keys#   s7    %/7j!!!Rk"""V}}r   c                    |r~t          | j        j                  dz
  }t          |          |k    r"t          d| dt          |                     |                    t          | j        j        |                     t                      pd|d<   |r[|                                D ],\  }}t          |t                    st          |          ||<   -| 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vs         r   r$   zRayPrometheusMetric.labels)   s    	D4;011A5H6{{h&& > (> >03F> >   DK$96 B BCCC$3$5$5$;[! 	7$**,, - -1!!S)) -&)!ffLOK((666r   namec                 .    t          j        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)r6   s    r   !_get_sanitized_opentelemetry_namez5RayPrometheusMetric._get_sanitized_opentelemetry_name=   s     v&T222r   )__name__
__module____qualname__r   staticmethodr!   r0   r#   r%   r$   r;   r   r   r   r   r      s        # # #
 $s)d"2 uS#X    \
  ( 3 3 3 3 3 \3 3 3r   r   c            
       h    e Zd ZdZ	 	 	 ddededz  dee         dz  dedz  fdZd	eez  fd
Z	d Z
dS )RayGaugeWrapperzVWraps around ray.util.metrics.Gauge to provide same API as
    prometheus_client.Gauger(   Nr6   documentationr   multiprocess_modec                     ~|                      |          }|                     |          }t          j        |||          | _        d S N)r6   descriptiontag_keys)r%   r;   r   Gauger   )r   r6   rB   r   rC   rG   s         r   r   zRayGaugeWrapper.__init__Q   sS     %%j1155d;;!'%
 
 
r   valuec                 6    | j                             |          S N)r   setr   rI   s     r   rL   zRayGaugeWrapper.setf   s    {u%%%r   c                 X    | j                             t          j                              S rK   )r   rL   timer   s    r   set_to_current_timez#RayGaugeWrapper.set_to_current_timei   s    {ty{{+++r   )r(   Nr(   )r<   r=   r>   __doc__r0   r!   r   intfloatrL   rP   r   r   r   rA   rA   M   s          %''+(*
 

 Tz
 I$	

 :
 
 
 
*&u & & & &, , , , ,r   rA   c                   X    e Zd ZdZ	 	 ddededz  dee         dz  fdZdd	eez  fd
Z	dS )RayCounterWrapperzZWraps around ray.util.metrics.Counter to provide same API as
    prometheus_client.Counterr(   Nr6   rB   r   c                     |                      |          }|                     |          }t          j        |||          | _        d S rE   )r%   r;   r   Counterr   )r   r6   rB   r   rG   s        r   r   zRayCounterWrapper.__init__r   sP     %%j1155d;;!)%
 
 
r         ?rI   c                 F    |dk    rd S | j                             |          S )Nr   )r   incrM   s     r   rZ   zRayCounterWrapper.inc   s$    A::F{u%%%r   )r(   N)rX   )
r<   r=   r>   rQ   r0   r!   r   rR   rS   rZ   r   r   r   rU   rU   n   s        ! ! %''+	
 

 Tz
 I$	
 
 
 
& &u & & & & & &r   rU   c            
       n    e Zd ZdZ	 	 	 ddededz  dee         dz  dee         dz  fdZd	eez  fd
Z	dS )RayHistogramWrapperz^Wraps around ray.util.metrics.Histogram to provide same API as
    prometheus_client.Histogramr(   Nr6   rB   r   bucketsc                     |                      |          }|                     |          }|r|ng }t          j        ||||          | _        d S )N)r6   rF   rG   
boundaries)r%   r;   r   	Histogramr   )r   r6   rB   r   r]   rG   r_   s          r   r   zRayHistogramWrapper.__init__   sa     %%j1155d;; '/WWR
!+%!	
 
 
r   rI   c                 6    | j                             |          S rK   )r   observerM   s     r   rb   zRayHistogramWrapper.observe   s    {""5)))r   )r(   NN)
r<   r=   r>   rQ   r0   r!   rS   r   rR   rb   r   r   r   r\   r\      s        # # %''+&*
 

 Tz
 I$	

 et#
 
 
 
$*S5[ * * * * * *r   r\   c                       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)r<   r=   r>   rQ   rU   _counter_clsr   r   r   rd   rd      s          %LLLr   rd   c                       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)
r<   r=   r>   rQ   rA   
_gauge_clsrU   re   r\   _histogram_clsr   r   r   rg   rg      s)          !J$L(NNNr   rg   c                   <    e Zd ZdZeZeZeZ	e
ZeZed             ZdS )RayPrometheusStatLoggerz1RayPrometheusStatLogger uses Ray metrics instead.c                      d S rK   r   r   r   r   _unregister_vllm_metricsz0RayPrometheusStatLogger._unregister_vllm_metrics   s	     	r   N)r<   r=   r>   rQ   rA   rh   rU   re   r\   ri   rd   _spec_decoding_clsrg   _kv_connector_clsr?   rm   r   r   r   rk   rk      sN        ;; J$L(N,0  \  r   rk   )rO   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   regexr9   r0   r   r   rA   rU   r\   rd   rg   rk   r   r   r   <module>rw      s    V V V V V V 8 8 8 8 8 8 8 8 8 8 8 8&&&&&&//////'''''''   KIII    t    -3 -3 -3 -3 -3 -3 -3 -3`, , , , ,) , , ,B& & & & &+ & & &0* * * * *- * * *4% % % % %* % % %	) 	) 	) 	) 	)4 	) 	) 	)    2     s   + 	77