
    &`i4                     ,   d dl Z d dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZmZmZmZ  e j        e          Z G d de          Z G d d	e          Z G d
 dej                  Zd Zd Zd Z ej        dej        ej        z            Zd ZdS )    N)sync)aggregation_database_exporter)start_http_server)REGISTRYCounterMetricFamilyGaugeMetricFamilyHistogramMetricFamilyUnknownMetricFamilyc                   z    e Zd ZdZdddefdZed             Zed             Zed             Z	ed             Z
d	S )
Optionsa  Options contains options for configuring the exporter.
    The address can be empty as the prometheus client will
    assume it's localhost
    :type namespace: str
    :param namespace: The prometheus namespace to be used. Defaults to ''.
    :type port: int
    :param port: The Prometheus port to be used. Defaults to 8000.
    :type address: str
    :param address: The Prometheus address to be used. Defaults to ''.
    :type registry: registry
    :param registry: The Prometheus address to be used. Defaults to ''.
    :type registry: :class:`~prometheus_client.core.CollectorRegistry`
    :param registry: A Prometheus collector registry instance.
     i@  c                 X    || _         || _        t          |          | _        || _        d S N)
_namespace	_registryint_port_address)self	namespaceportaddressregistrys        t/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/prometheus_exporter.py__init__zOptions.__init__'   s(    #!YY
    c                     | j         S z&Prometheus Collector Registry instancer   r   s    r   r   zOptions.registry-        ~r   c                     | j         S )z Prefix to be used with view name)r   r!   s    r   r   zOptions.namespace2   s     r   c                     | j         S )zPort number to listen)r   r!   s    r   r   zOptions.port7   s     zr   c                     | j         S )z'Endpoint address (default is localhost))r   r!   s    r   r   zOptions.address<        }r   N)__name__
__module____qualname____doc__r   r   propertyr   r   r   r    r   r   r   r      s          "$$X           X   X   X   X  r   r   c                       e Zd ZdZ e            dfdZed             Zed             Zed             Z	ed             Z
d Zd	 Zd
 Zd ZdS )	Collectorz4Collector represents the Prometheus Collector objectNc                 P    |i }|| _         |j        | _        || _        i | _        d S r   )_optionsr   r   _view_name_to_data_map_registered_views)r   optionsview_name_to_data_maps      r   r   zCollector.__init__E   s5     ($&! )&;#!#r   c                     | j         S z,Options to be used to configure the exporterr0   r!   s    r   r3   zCollector.optionsM   r&   r   c                     | j         S r   r    r!   s    r   r   zCollector.registryR   r"   r   c                     | j         S )zOMap with all view data objects
        that will be sent to Prometheus
        )r1   r!   s    r   r4   zCollector.view_name_to_data_mapW   s    
 **r   c                     | j         S )zMap with all registered views)r2   r!   s    r   registered_viewszCollector.registered_views^   s     %%r   c                     t          | j        j        |          }|| j        vrG||j        t          t          t          |j                            |j	        j
        d}|| j        |<   dS dS )zrregister_view will create the needed structure
        in order to be able to sent all data to Prometheus
        )namedocumentationlabelsunitsN)get_view_namer3   r   r;   descriptionlistmapsanitizecolumnsmeasureunit)r   viewv_namedescs       r   register_viewzCollector.register_viewc   sw     t|5t<<...!%!1s8T\::;;*	 D -1D!&))) /.r   c                     |                      |j                   t          | j        j        |j                  }|| j        |<   dS )z)Add view data object to be sent to serverN)rL   rI   rA   r3   r   r4   )r   	view_datarJ   s      r   add_view_datazCollector.add_view_datar   sA    9>***t|5y~FF-6"6***r   c                 B   |d         }|d         }|d         }|d         }t          |          t          |          k    sJ ||f            d |D             }t          |t          j                  rM|                    |          }	|	st          ||||          }	|	||<   |	                    ||j                   dS t          |t          j                  r|j	        t          |j	                  k    sJ g }
d	}t          |j	                  D ];\  }}||j        |         z  }t          |          |g}|
                    |           <|
                    d
|j        g           |                    |          }	|	st          |||          }	|	||<   |	                    ||
|j                   dS t          |t          j                  rL|                    |          }	|	st%          |||          }	|	||<   |	                    ||j                   dS t          |t          j                  rL|                    |          }	|	st+          |||          }	|	||<   |	                    ||j                   dS t/          dt1          |                     )a  to_metric translate the data that OpenCensus create
        to Prometheus format, using Prometheus Metric object
        :type desc: dict
        :param desc: The map that describes view definition
        :type tag_values: tuple of :class:
            `~opencensus.tags.tag_value.TagValue`
        :param object of opencensus.tags.tag_value.TagValue:
            TagValue object used as label values
        :type agg_data: object of :class:
            `~opencensus.stats.aggregation_data.AggregationData`
        :param object of opencensus.stats.aggregation_data.AggregationData:
            Aggregated data that needs to be converted as Prometheus samples
        :rtype: :class:`~prometheus_client.core.CounterMetricFamily` or
                :class:`~prometheus_client.core.HistogramMetricFamily` or
                :class:`~prometheus_client.core.UnknownMetricFamily` or
                :class:`~prometheus_client.core.GaugeMetricFamily`
        r=   r>   r?   r@   c                     g | ]}|r|nd 	S )r   r,   ).0tvs     r   
<listcomp>z'Collector.to_metric.<locals>.<listcomp>   s!    <<<2B&bbB<<<r   )r=   r>   rH   r?   )r?   valueNr   z+Inf)r=   r>   r?   )r?   buckets	sum_valuezunsupported aggregation type )len
isinstanceaggregation_data_moduleCountAggregationDatagetr   
add_metric
count_dataDistributionAggregationDataboundssorted	enumeratecounts_per_bucketstrappendr
   sumSumAggregationDatar   sum_dataLastValueAggregationDatar	   rU   
ValueErrortype)r   rK   
tag_valuesagg_datametrics_mapmetric_namemetric_description
label_keysmetric_unitsmetricrV   	cum_countiiboundbuckets                  r   	to_metriczCollector.to_metricy   s   $ 6l!/2(^
G}:#j//111J
3K111 =<<<<
h 7 LMM E	O __[11F 2,$"4%%	   ,2K(Zx7JKKKF"9"UVV 8	O?fX_&=&===== GI&x77 ' '	EX7;;	e**i0v&&&& NNFH$78999 __[11F 2.$"4%  
 ,2K(!",    
 F"9"LMM 	O __[11F 2,$"4%  
 ,2K(Zx7HIIIF"9"RSS 	O __[11F 2*$"4%  
 ,2K(Zx~FFFF MT(^^MMNNNr   c              #   2  K   i }| j                                                                         D ]K\  }}|| j        vr| j        |         }|j        D ]'}|j        |         }|                     ||||           (L|                                D ]}|V  dS )zCollect fetches the statistics from OpenCensus
        and delivers them as Prometheus Metrics.
        Collect is invoked every time a prometheus.Gatherer is run
        for example when the HTTP endpoint is invoked by Prometheus.
        N)r1   copyitemsr;   tag_value_aggregation_data_maprx   values)r   rn   rJ   rN   rK   rl   rm   rs   s           r   collectzCollector.collect   s       !%!<!A!A!C!C!I!I!K!K 	H 	HFIT222(0D'F H H
$CJOtZ;GGGGH "((** 	 	FLLLL	 	r   )r'   r(   r)   r*   r   r   r+   r3   r   r4   r;   rL   rO   rx   r~   r,   r   r   r.   r.   B   s        >>&wyy $ $ $ $   X   X + + X+ & & X&1 1 17 7 7aO aO aOF    r   r.   c                       e Zd ZdZej         e            fdZed             Z	ed             Z
ed             Zed             Zd Zd Zd	 Zd
 ZdS )PrometheusStatsExportera^  Exporter exports stats to Prometheus, users need
        to register the exporter as an HTTP Handler to be
        able to export.
    :type options:
        :class:`~opencensus.ext.prometheus.stats_exporter.Options`
    :param options: An options object with the parameters to instantiate the
                         prometheus exporter.
    :type gatherer: :class:`~prometheus_client.core.CollectorRegistry`
    :param gatherer: A Prometheus collector registry instance.
    :type transport:
        :class:`opencensus.common.transports.sync.SyncTransport` or
        :class:`opencensus.common.transports.async_.AsyncTransport`
    :param transport: An instance of a Transpor to send data with.
    :type collector:
        :class:`~opencensus.ext.prometheus.stats_exporter.Collector`
    :param collector: An instance of the Prometheus Collector object.
    c                     || _         || _        || _         ||           | _        |                                  t          j        | j                   d S r   )r0   	_gatherer
_collector
_transport
serve_httpr   register)r   r3   gatherer	transport	collectors        r   r   z PrometheusStatsExporter.__init__  sQ      !##)D//$/*****r   c                     | j         S )zOThe transport way to be sent data to server
        (default is sync).
        )r   r!   s    r   r   z!PrometheusStatsExporter.transport      
 r   c                     | j         S )zSCollector class instance to be used
        to communicate with Prometheus
        )r   r!   s    r   r   z!PrometheusStatsExporter.collector  r   r   c                     | j         S r   )r   r!   s    r   r   z PrometheusStatsExporter.gatherer  r"   r   c                     | j         S r6   r7   r!   s    r   r3   zPrometheusStatsExporter.options!  r&   r   c                 B    || j                             |           dS dS )zvexport send the data to the transport class
        in order to be sent to Prometheus in a sync or async way.
        N)r   export)r   rN   s     r   r   zPrometheusStatsExporter.export&  s.      N!!),,,,, ! r   c                      t          d          S )NzNot supported by Prometheus)NotImplementedError)r   rI   s     r   on_register_viewz(PrometheusStatsExporter.on_register_view-  s    "#@AAAr   c                 `    |D ]*}|j         i |_         | j                            |           +dS )a=  Emit exports to the Prometheus if view data has one or more rows.
        Each OpenCensus AggregationData will be converted to
        corresponding Prometheus Metric: SumData will be converted
        to Untyped Metric, CountData will be a Counter Metric
        DistributionData will be a Histogram Metric.
        N)r|   r   rO   )r   rN   v_datas      r   emitzPrometheusStatsExporter.emit0  sG       	1 	1F4<8:5N((0000		1 	1r   c                 x    t          | j        j                  }|rd|ini }t          dd| j        j        i| dS )z*serve_http serves the Prometheus endpoint.addrr   Nr,   )rd   r3   r   r   r   )r   r   kwargss      r   r   z"PrometheusStatsExporter.serve_http>  sL    dl*++&-5&'""2;;t|0;F;;;;;r   N)r'   r(   r)   r*   r   SyncTransportr.   r   r+   r   r   r   r3   r   r   r   r   r,   r   r   r   r      s         & ,0+=+ + + +   X   X   X   X- - -B B B1 1 1< < < < <r   r   c                     | j         dk    rt          d          t          |           }t          | | j        |          }|S )zQnew_stats_exporter returns an exporter
    that exports stats to Prometheus.
    r   z"Namespace can not be empty string.)r3   r   r   )r   rj   new_collectorr   r   )optionr   exporters      r   new_stats_exporterr   E  sR     2=>>>f%%I&I  H Or   c                 "    t          |           S )znew_collector should be used
    to create instance of Collector class in order to
    prevent the usage of constructor directly
    r3   )r.   r   s    r   r   r   T  s    
 W%%%%r   c                 J    d}| dk    r| dz   }t          ||j        z             S )zcreate the name for the viewr   _)rE   r=   )r   rI   r=   s      r   rA   rA   \  s.    DB3D49$%%%r   z[^\w]c                 8    t                               d|           S )zsanitize the given metric name or label according to Prometheus rule.
    Replace all characters other than [A-Za-z0-9_] with '_'.
    r   )_NON_LETTERS_NOR_DIGITS_REsub)keys    r   rE   rE   g  s     &))#s333r   ) loggingreopencensus.common.transportsr   opencensus.statsr   rZ   r   prometheus_clientr   prometheus_client.corer   r   r	   r
   r   	getLoggerr'   loggerobjectr   r.   StatsExporterr   r   r   rA   compileUNICODE
IGNORECASEr   rE   r,   r   r   <module>r      s    				 - - - - - - W W W W W W W W / / / / / /              
	8	$	$( ( ( ( (f ( ( (Vl l l l l l l l^Q< Q< Q< Q< Q<m9 Q< Q< Q<h  & & && & & (RZ"*r}2LMM 4 4 4 4 4r   