
    &`i8$                        d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ dZdZdZd	Zdd
eee                  dee         fdZdeeef         fdZd
edeeef         fdZ ed           G d de
j                              Z ed           G d de
j                              Z ed           G d de
j                              ZdS )    )DictListOptionalTupleUnionN)context)metrics)	PublicAPI
deploymentreplicaapplicationroutetag_keysreturnc                    | t                      } t          j                    | S t          | v rt	          dt           d          t
          | v rt	          dt
           d          t          | v rt	          dt           d          t          t
          f}t          j                    j        r|t          fz  }| r|| z   } n|} | S )z&Add serve context tags to the tag_keysN''' tag is reserved for Ray Serve metrics)tupler   _get_internal_replica_contextDEPLOYMENT_TAG
ValueErrorREPLICA_TAGAPPLICATION_TAGapp_name)r   ray_serve_tagss     e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/metrics.py_add_serve_metric_tagsr      s    77 ,..6!!T^TTTUUUhQ[QQQRRR(""U_UUUVVV %k2N,..7 -?,, "!H,!O    default_tagsc                    t          j                    | S t          | v rt          dt           d          t          | v rt          dt           d          t
          | v rt          dt
           d          t          j                    }|j        | t          <   |j        | t          <   |j        r|j        | t
          <   | S )z5Add serve context tags and values to the default_tagsNr   r   )	r   r   r   r   r   r   r   replica_tagr   )r   replica_contexts     r   _add_serve_metric_default_tagsr#   )   s    ,..6%%T^TTTUUUl""Q[QQQRRR,&&U_UUUVVV;==O#2#=L  / ;L A(7(@_%r   tagsc                     t           j        j                                        }t          | v rt          |vr|j        |t          <   dS dS dS )z/Add serve context tag values to the metric tagsN)rayserver   _get_serve_request_context	ROUTE_TAGr   )r   r$   _request_contexts      r   _add_serve_context_tag_valuesr+   <   sN     y(CCEEH$!6!6*0Y !6!6r   beta)	stabilityc            	            e Zd ZdZ	 ddededeee                  f fdZdeeef         f fd	Z	dde
eef         deeef         f fdZ xZS )Countera  A serve cumulative metric that is monotonically increasing.

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

    Serve-related tags ("deployment", "replica", "application", "route")
    are added automatically if not provided.

    .. code-block:: python

            @serve.deployment
            class MyDeployment:
                def __init__(self):
                    self.num_requests = 0
                    self.my_counter = metrics.Counter(
                        "my_counter",
                        description=("The number of odd-numbered requests "
                            "to this deployment."),
                        tag_keys=("model",),
                    )
                    self.my_counter.set_default_tags({"model": "123"})

                def __call__(self):
                    self.num_requests += 1
                    if self.num_requests % 2 == 1:
                        self.my_counter.inc()

    .. note::

        Before Ray 2.10, this exports a Prometheus gauge metric instead of
        a counter metric.
        Starting in Ray 2.10, this exports both the proper counter metric
        (with a suffix "_total") and gauge metric (for compatibility).
        The gauge metric will be removed in a future Ray release and you can set
        `RAY_EXPORT_COUNTER_AS_GAUGE=0` to disable exporting it in the meantime.

    Args:
        name: Name of the metric.
        description: Description of the metric.
        tag_keys: Tag keys of the metric.
     Nnamedescriptionr   c                    |r4t          |t                    st          dt          |                     t	          |          }t                                          |||           |                     i            d S Nz&tag_keys should be a tuple type, got: 
isinstancer   	TypeErrortyper   super__init__set_default_tagsselfr1   r2   r   	__class__s       r   r:   zCounter.__init__p         	Jx77 	LDNNLL   *(33{H555b!!!!!r   r   c                 d    t                                          t          |                     d S Nr9   r;   r#   r=   r   r>   s     r   r;   zCounter.set_default_tags{   *      !?!M!MNNNNNr         ?valuer$   c                 v    t          | j        |           t                                          ||           dS )zcIncrement the counter by the given value, add serve context
        tag values to the tags
        N)r+   	_tag_keysr9   incr=   rF   r$   r>   s      r   rI   zCounter.inc~   5     	&dnd;;;E4     r   r0   N)rE   N)__name__
__module____qualname____doc__strr   r   r:   r   r;   r   intfloatrI   __classcell__r>   s   @r   r/   r/   D   s        ( (V RV	" 	"	"&)	":B5::N	" 	" 	" 	" 	" 	"OT#s(^ O O O O O O! !sEz* !S#X ! ! ! ! ! ! ! ! ! !r   r/   c            	            e Zd ZdZ	 ddededeee                  f fdZdeeef         f fd	Z	dd
e
eef         deeef         f fdZ xZS )Gaugea  Gauges keep the last recorded value and drop everything before.

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

    Serve-related tags ("deployment", "replica", "application", "route")
    are added automatically if not provided.

    .. code-block:: python

            @serve.deployment
            class MyDeployment:
                def __init__(self):
                    self.num_requests = 0
                    self.my_gauge = metrics.Gauge(
                        "my_gauge",
                        description=("The current memory usage."),
                        tag_keys=("model",),
                    )
                    self.my_counter.set_default_tags({"model": "123"})

                def __call__(self):
                    process = psutil.Process()
                    self.gauge.set(process.memory_info().rss)

    Args:
        name: Name of the metric.
        description: Description of the metric.
        tag_keys: Tag keys of the metric.
    r0   Nr1   r2   r   c                    |r4t          |t                    st          dt          |                     t	          |          }t                                          |||           |                     i            d S r4   r5   r<   s       r   r:   zGauge.__init__   r?   r   r   c                 d    t                                          t          |                     d S rA   rB   rC   s     r   r;   zGauge.set_default_tags   rD   r   rF   r$   c                 v    t          | j        |           t                                          ||           dS )z[Set the gauge to the given value, add serve context
        tag values to the tags
        N)r+   rH   r9   setrJ   s      r   r[   z	Gauge.set   rK   r   rL   rA   )rM   rN   rO   rP   rQ   r   r   r:   r   r;   r   rR   rS   r[   rT   rU   s   @r   rW   rW      s         @ RV	" 	"	"&)	":B5::N	" 	" 	" 	" 	" 	"OT#s(^ O O O O O O! !sEz* !$sCx. ! ! ! ! ! ! ! ! ! !r   rW   c                        e Zd ZdZ	 	 	 ddededee         deee                  f fdZ	d	e
eef         f fd
Zddeeef         de
eef         f fdZ xZS )	Histograma  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

    Serve-related tags ("deployment", "replica", "application", "route")
    are added automatically if not provided.

    .. code-block:: python

            @serve.deployment
            class MyDeployment:
                def __init__(self):
                    self.my_histogram = Histogram(
                        "my_histogram",
                        description=("Histogram of the __call__ method running time."),
                        boundaries=[1,2,4,8,16,32,64],
                        tag_keys=("model",),
                    )
                    self.my_histogram.set_default_tags({"model": "123"})

                def __call__(self):
                    start = time.time()
                    self.my_histogram.observe(time.time() - start)

    Args:
        name: Name of the metric.
        description: Description of the metric.
        boundaries: Boundaries of histogram buckets.
        tag_keys: Tag keys of the metric.
    r0   Nr1   r2   
boundariesr   c                    |r4t          |t                    st          dt          |                     t	          |          }t                                          ||||           |                     i            d S r4   r5   )r=   r1   r2   r^   r   r>   s        r   r:   zHistogram.__init__   s      	Jx77 	LDNNLL   *(33{JAAAb!!!!!r   r   c                 d    t                                          t          |                     d S rA   rB   rC   s     r   r;   zHistogram.set_default_tags   rD   r   rF   r$   c                 v    t          | j        |           t                                          ||           dS )zRObserve the given value, add serve context
        tag values to the tags
        N)r+   rH   r9   observerJ   s      r   rb   zHistogram.observe   s5     	&dnd;;;t$$$$$r   )r0   NNrA   )rM   rN   rO   rP   rQ   r   rS   r   r   r:   r   r;   r   rR   rb   rT   rU   s   @r   r]   r]      s       ! !L "&)-" "" " K	"
 5:&" " " " " "OT#s(^ O O O O O O% %U3:. %d38n % % % % % % % % % %r   r]   rA   )typingr   r   r   r   r   r&   	ray.server   ray.utilr	   ray.util.annotationsr
   r   r   r   r)   rQ   r   r#   r+   r/   rW   r]    r   r   <module>rh      s   5 5 5 5 5 5 5 5 5 5 5 5 5 5 



             * * * * * *	 XeCj%9 U3Z    6c3h    &1E 1c3h 1 1 1 1 V>! >! >! >! >!go >! >! >!B V3! 3! 3! 3! 3!GM 3! 3! 3!l V;% ;% ;% ;% ;%! ;% ;% ;% ;% ;%r   