
    &`i0                     t   d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZ  e j        e          Z ej        d          Zdedefd	Ze G d
 d                      Ze G d de                      Z e G d de                      Ze G d de                      Zg dZ!dS )    N)AnyDictListOptionalTupleUnion)env_bool)CountGauge	HistogramSum)DeveloperAPIz^[a-zA-Z_:][a-zA-Z0-9_:]*$namereturnc                     t          |           dk    rt          d          t                              |           s t	          j        d|  dt                     dS dS )Nr   z8Empty name is not allowed. Please provide a metric name.zInvalid metric name: z. Metric will be discarded and data will not be collected or published. Metric names can only contain letters, numbers, _, and :. Metric names cannot start with numbers.TF)len
ValueError_VALID_METRIC_NAME_REmatchwarningswarnUserWarning)r   s    d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/metrics.py_is_invalid_metric_namer      sq    
4yyA~~STTT &&t,, 6D 6 6 6 	
 	
 	
 t5    c            
           e Zd ZdZ	 	 ddededeeedf                  fdZd	eeef         fd
Z		 dde
eef         deeeef                  ddfdZd Zd Zedeeef         fd            ZdS )MetriczThe parent class of custom metrics.

    Ray's custom metrics APIs are rooted from this class and share
    the same public methods.
     Nr   descriptiontag_keys.c                    t          |          | _        || _        || _        i | _        |pt                      | _        d | _        t          | j        t
                    s$t          dt          | j                             | j        D ]7}t          |t                    s t          dt          |           d          8d| j        v r%t          j        d| j         dt                     d S d S )Nz&tag_keys should be a tuple type, got: zTag keys must be str, got .:zMetric name z contains a : character, which is no longer allowed. Please migrate to the new metric name format. This will be an error in the future.)r   _discard_metric_name_description_default_tagstuple	_tag_keys_metric
isinstance	TypeErrortypestrr   r   FutureWarning)selfr   r   r    keys        r   __init__zMetric.__init__1   s     7t<<
'!,UWW$.%00 	RD<P<PRR   > 	K 	KCc3'' K IT#YY I I IJJJK $*M8tz 8 8 8 	     r   default_tagsc                     |                                 D ]V\  }}|| j        vrt          d| d          t          |t                    s t          dt          |           d          W|| _        | S )a  Set default tags of metrics.

        Example:
            >>> from ray.util.metrics import Counter
            >>> # Note that set_default_tags returns the instance itself.
            >>> counter = Counter("name", tag_keys=("a",))
            >>> counter2 = counter.set_default_tags({"a": "b"})
            >>> assert counter is counter2
            >>> # this means you can instantiate it in this way.
            >>> counter = Counter("name", tag_keys=("a",)).set_default_tags({"a": "b"})

        Args:
            default_tags: Default tags that are
                used for every record method.

        Returns:
            Metric: it returns the instance itself.
        zUnrecognized tag key r"   Tag values must be str, got )itemsr)   r   r+   r.   r,   r-   r'   )r0   r3   r1   vals       r   set_default_tagszMetric.set_default_tagsT   s    & %**,, 	M 	MHC$.(( !?!?!?!?@@@c3'' M KtCyy K K KLLLM *r   valuetagsr   c                     | j         rdS | j        J |                     |          }|                     |           | j                            ||           dS )zRecord the metric point of the metric.

        Tags passed in will take precedence over the metric's default tags.

        Args:
            value: The value to be recorded as a metric point.
        Nr:   )r$   r*   _get_final_tags_validate_tagsrecord)r0   r9   r:   
final_tagss       r   _recordzMetric._recordp   sj      	F|'''))$//
J'''E
33333r   c                     |s| j         S |                                D ]7}t          |t                    s t	          dt          |           d          8i | j         |S )Nr5   r"   )r'   valuesr+   r.   r,   r-   )r0   r:   r7   s      r   r=   zMetric._get_final_tags   sz     	&%%;;== 	M 	MCc3'' M KtCyy K K KLLLM .$$---r   c                     g }| j         D ]}||vr|                    |           |r&t          dd                    |           d          d S )NzMissing value for tag key(s): ,r"   )r)   appendr   join)r0   r@   missing_tagstag_keys       r   r>   zMetric._validate_tags   su    ~ 	- 	-Gj((##G,,,  	YWchh|>T>TWWWXXX	Y 	Yr   c                 8    | j         | j        | j        | j        dS )a0  Return the information of this metric.

        Example:
            >>> from ray.util.metrics import Counter
            >>> counter = Counter("name", description="desc")
            >>> print(counter.info)
            {'name': 'name', 'description': 'desc', 'tag_keys': (), 'default_tags': {}}
        )r   r   r    r3   )r%   r&   r)   r'   )r0   s    r   infozMetric.info   s)     J, .	
 
 	
r   r   NN)__name__
__module____qualname____doc__r.   r   r   r2   r   r8   r   intfloatrA   r=   r>   propertyr   rK    r   r   r   r   )   s,         .2	! !! ! 5c?+	! ! ! !FT#s(^    > *.4 4S%Z 4 tCH~&4 
	4 4 4 4*. . .	Y 	Y 	Y 
d38n 
 
 
 X
 
 
r   r   c            
            e Zd ZdZ	 	 ddededeeedf                  f fdZd	 Zdde	e
ef         deeef         fdZ xZS )Counterac  A cumulative metric that is monotonically increasing.

    This corresponds to Prometheus' counter metric:
    https://prometheus.io/docs/concepts/metric_types/#counter

    Before Ray 2.10, this exports a Prometheus gauge metric instead of
    a counter metric, which is wrong.
    Since 2.10, this exports both counter (with a suffix "_total") and
    gauge metrics (for bug compatibility).
    Use `RAY_EXPORT_COUNTER_AS_GAUGE=0` to disable exporting the gauge metric.

    Args:
        name: Name of the metric.
        description: Description of the metric.
        tag_keys: Tag keys of the metric.
    r   Nr   r   r    .c                 *   t                                          |||           | j        r	d | _        d S t	          dd          r(	 t          | j        | j        | j                  | _        d S 	 t          | j        | j        | j                  | _        d S )NRAY_enable_open_telemetryT)
superr2   r$   r*   r	   CythonCountr%   r&   r)   	CythonSumr0   r   r   r    	__class__s       r   r2   zCounter.__init__   s     	{H555 	XDLLL3T:: X  +J 14>   
  )T5FWWr   c                 @    | j         }| j        | j        | j        f}||fS rM   )r^   r%   r&   r)   r0   deserializerserialized_datas      r   
__reduce__zCounter.__reduce__   s'    ~:t'8$.I_,,r         ?r9   r:   c                     t          |t          t          f          s t          dt	          |           d          |dk    rt          d|           |                     ||           dS )a0  Increment the counter by `value` (defaults to 1).

        Tags passed in will take precedence over the metric's default tags.

        Args:
            value(int, float): Value to increment the counter by (default=1).
            tags(Dict[str, str]): Tags to set or override for this counter.
         value must be int or float, got r"   r   zvalue must be >0, got r<   N)r+   rR   rS   r,   r-   r   rA   r0   r9   r:   s      r   inczCounter.inc   sw     %#u.. 	OMtE{{MMMNNNA::=e==>>>U&&&&&r   rL   )rd   N)rN   rO   rP   rQ   r.   r   r   r2   rc   r   rR   rS   r   rh   __classcell__r^   s   @r   rW   rW      s         ( .2	X XX X 5c?+	X X X X X X4- - -
' 'sEz* 'S#X ' ' ' ' ' ' ' 'r   rW   c                        e Zd ZdZ	 	 	 ddededee         deeedf                  f fd	Z	dd
e
eef         deeef         fdZd Ze fd            Z xZS )r   a  Tracks the size and number of events in buckets.

    Histograms allow you to calculate aggregate quantiles
    such as 25, 50, 95, 99 percentile latency for an RPC.

    This corresponds to Prometheus' histogram metric:
    https://prometheus.io/docs/concepts/metric_types/#histogram

    Args:
        name: Name of the metric.
        description: Description of the metric.
        boundaries: Boundaries of histogram buckets.
        tag_keys: Tag keys of the metric.
    r   Nr   r   
boundariesr    .c                 |   t                                          |||           |t          |          dk    rt          d          t	          |          D ]!\  }}|dk    rt          d| d| d          "|| _        | j        r	d | _        d S t          | j	        | j
        | j        | j                  | _        d S )Nr   zuboundaries argument should be provided when using the Histogram class. e.g., Histogram("name", boundaries=[1.0, 2.0])z'Invalid `boundaries` argument at index z, z(. Use positive values for the arguments.)rZ   r2   r   r   	enumeraterl   r$   r*   CythonHistogramr%   r&   r)   )r0   r   r   rl   r    iboundaryr^   s          r   r2   zHistogram.__init__   s     	{H555ZA!5!5;  
 %Z00 	 	KAx1}} QQ Q&Q Q Q    % 	DLLL*
D-t DLLLr   r9   r:   c                     t          |t          t          f          s t          dt	          |           d          |                     ||           dS )a&  Observe a given `value` and add it to the appropriate bucket.

        Tags passed in will take precedence over the metric's default tags.

        Args:
            value(int, float): Value to set the gauge to.
            tags(Dict[str, str]): Tags to set or override for this gauge.
        rf   r"   Nr+   rR   rS   r,   r-   rA   rg   s      r   observezHistogram.observe  sU     %#u.. 	OMtE{{MMMNNNUD!!!!!r   c                 L    t           }| j        | j        | j        | j        f}||fS rM   )r   r%   r&   rl   r)   r`   s      r   rc   zHistogram.__reduce__+  s0     JON	
 _,,r   c                 f    t                      j        }|                    d| j        i           |S )z*Return information about histogram metric.rl   )rZ   rK   updaterl   )r0   rK   r^   s     r   rK   zHistogram.info5  s.     ww|\4?3444r   )r   NNrM   )rN   rO   rP   rQ   r.   r   rS   r   r   r2   r   rR   r   rt   rc   rT   rK   ri   rj   s   @r   r   r      s         $ "&.2   K	
 5c?+     :" "U3:. "d38n " " " "- - -     X    r   r   c            
            e Zd ZdZ	 	 ddededeeedf                  f fdZdd	eee	e
f                  d
eeef         fdZd Z xZS )r   at  Gauges keep the last recorded value and drop everything before.

    Unlike counters, gauges can go up or down over time.

    This corresponds to Prometheus' gauge metric:
    https://prometheus.io/docs/concepts/metric_types/#gauge

    Args:
        name: Name of the metric.
        description: Description of the metric.
        tag_keys: Tag keys of the metric.
    r   Nr   r   r    .c                     t                                          |||           | j        r	d | _        d S t	          | j        | j        | j                  | _        d S rM   )rZ   r2   r$   r*   CythonGauger%   r&   r)   r]   s       r   r2   zGauge.__init__L  sV     	{H555 	VDLLL&tz43DdnUUDLLLr   r9   r:   c                     |dS t          |t          t          f          s t          dt	          |           d          |                     ||           dS )a?  Set the gauge to the given `value`.

        Tags passed in will take precedence over the metric's default tags.

        Args:
            value(int, float): Value to set the gauge to. If `None`, this method is a
                no-op.
            tags(Dict[str, str]): Tags to set or override for this gauge.
        Nrf   r"   rs   rg   s      r   setz	Gauge.setX  s_     =F%#u.. 	OMtE{{MMMNNNUD!!!!!r   c                 @    t           }| j        | j        | j        f}||fS rM   )r   r%   r&   r)   r`   s      r   rc   zGauge.__reduce__j  s%    :t'8$.I_,,r   rL   rM   )rN   rO   rP   rQ   r.   r   r   r2   r   rR   rS   r   r|   rc   ri   rj   s   @r   r   r   =  s           .2	
V 
V
V 
V 5c?+	
V 
V 
V 
V 
V 
V" "%U
"34 "DcN " " " "$- - - - - - -r   r   )rW   r   r   )"loggingrer   typingr   r   r   r   r   r   ray._private.ray_constantsr	   ray._rayletr
   r[   r   rz   r   ro   r   r\   ray.util.annotationsr   	getLoggerrN   loggercompiler   r.   boolr   r   rW   __all__rU   r   r   <module>r      s    				  : : : : : : : : : : : : : : : : / / / / / /            . - - - - -		8	$	$
 #
#@AA # $     
 
 
 
 
 
 
 
D ?' ?' ?' ?' ?'f ?' ?' ?'D J J J J J J J JZ /- /- /- /- /-F /- /- /-d  r   