o
    |j6h(                     @  s"  U d dl mZ d dlZ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 eeZeZedZedZe Zded	< dad
ed< ddgZded< G dd dZG dd de	eef ejdZej d+ddZ!d,ddZ"d-ddZ#d.d d!Z$d/d$d%Z%d0d)d*Z&dS )1    )annotationsN)Any	GeneratorGenericListOptionalTypeVar)cygrpc)ChannelArgumentTypeClientCallTracerCapsuleServerCallTracerFactoryCapsulezthreading.RLock_plugin_lockzOptional['ObservabilityPlugin']_OBSERVABILITY_PLUGINs"   google.monitoring.v3.MetricServices*   google.devtools.cloudtrace.v2.TraceServicezList[bytes]_SERVICES_TO_EXCLUDEc                   @  s    e Zd ZdZdd Zdd ZdS )ServerCallTracerFactoryzAn encapsulation of a ServerCallTracerFactory.

    Instances of this class can be passed to a Channel as values for the
    grpc.experimental.server_call_tracer_factory option
    c                 C  s
   || _ d S N_address)selfaddress r   b/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/grpc/_observability.py__init__/   s   
z ServerCallTracerFactory.__init__c                 C     | j S r   r   r   r   r   r   __int__2   s   zServerCallTracerFactory.__int__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   (   s    r   c                   @  s   e Zd ZU dZdZded< dZded< ejd.ddZ	ejd/ddZ
ejddd0ddZejd1ddZd2d!d"Zd2d#d$Zd3d%d&Zed4d'd(Zed4d)d*Zed4d+d,Zd-S )5ObservabilityPlugina  Abstract base class for observability plugin.

    *This is a semi-private class that was intended for the exclusive use of
     the gRPC team.*

    The ClientCallTracerCapsule and ClientCallTracerCapsule created by this
    plugin should be injected to gRPC core using observability_init at the
    start of a program, before any channels/servers are built.

    Any future methods added to this interface cannot have the
    @abc.abstractmethod annotation.

    Attributes:
      _stats_enabled: A bool indicates whether tracing is enabled.
      _tracing_enabled: A bool indicates whether stats(metrics) is enabled.
      _registered_methods: A set which stores the registered method names in
        bytes.
    Fbool_tracing_enabled_stats_enabledmethod_namebytestargetreturnr   c                 C     t  )a  Creates a ClientCallTracerCapsule.

        After register the plugin, if tracing or stats is enabled, this method
        will be called after a call was created, the ClientCallTracer created
        by this method will be saved to call context.

        The ClientCallTracer is an object which implements `grpc_core::ClientCallTracer`
        interface and wrapped in a PyCapsule using `client_call_tracer` as name.

        Args:
          method_name: The method name of the call in byte format.
          target: The channel target of the call in byte format.
          registered_method: Whether this method is pre-registered.

        Returns:
          A PyCapsule which stores a ClientCallTracer object.
        NotImplementedError)r   r$   r&   r   r   r   create_client_call_tracerP      z-ObservabilityPlugin.create_client_call_tracertrace_idstrspan_id
is_sampledNonec                 C  r(   )a  Saves the trace_id and span_id related to the current span.

        After register the plugin, if tracing is enabled, this method will be
        called after the server finished sending response.

        This method can be used to propagate census context.

        Args:
          trace_id: The identifier for the trace associated with the span as a
            32-character hexadecimal encoded string,
            e.g. 26ed0036f2eff2b7317bccce3e28d01f
          span_id: The identifier for the span as a 16-character hexadecimal encoded
            string. e.g. 113ec879e62583bc
          is_sampled: A bool indicates whether the span is sampled.
        r)   )r   r-   r/   r0   r   r   r   save_trace_contextg   s   z&ObservabilityPlugin.save_trace_context)xdsr3   (Optional[ServerCallTracerFactoryCapsule]c                C  r(   )ap  Creates a ServerCallTracerFactoryCapsule.

        This method will be called at server initialization time to create a
        ServerCallTracerFactory, which will be registered to gRPC core.

        The ServerCallTracerFactory is an object which implements
        `grpc_core::ServerCallTracerFactory` interface and wrapped in a PyCapsule
        using `server_call_tracer_factory` as name.

        Args:
          xds: Whether the server is xds server.
        Returns:
          A PyCapsule which stores a ServerCallTracerFactory object. Or None if
        plugin decides not to create ServerCallTracerFactory.
        r)   )r   r3   r   r   r   !create_server_call_tracer_factory|   r,   z5ObservabilityPlugin.create_server_call_tracer_factorymethodrpc_latencyfloatstatus_coder   c                 C  r(   )aa  Record the latency of the RPC.

        After register the plugin, if stats is enabled, this method will be
        called at the end of each RPC.

        Args:
          method: The fully-qualified name of the RPC method being invoked.
          target: The target name of the RPC method being invoked.
          rpc_latency: The latency for the RPC in seconds, equals to the time between
            when the client invokes the RPC and when the client receives the status.
          status_code: An element of grpc.StatusCode in string format representing the
            final status for the RPC.
        r)   )r   r6   r&   r7   r9   r   r   r   record_rpc_latency   s   z&ObservabilityPlugin.record_rpc_latencyenablec                 C  
   || _ dS )zxEnable or disable tracing.

        Args:
          enable: A bool indicates whether tracing should be enabled.
        Nr"   r   r;   r   r   r   set_tracing      
zObservabilityPlugin.set_tracingc                 C  r<   )z}Enable or disable stats(metrics).

        Args:
          enable: A bool indicates whether stats should be enabled.
        Nr#   r>   r   r   r   	set_stats   r@   zObservabilityPlugin.set_statsc                 C  r(   )zSaves the method name to registered_method list.

        When exporting metrics, method name for unregistered methods will be replaced
        with 'other' by default.

        Args:
          method_name: The method name in bytes.
        r)   )r   r$   r   r   r   save_registered_method   s   	z*ObservabilityPlugin.save_registered_methodc                 C  r   r   r=   r   r   r   r   tracing_enabled      z#ObservabilityPlugin.tracing_enabledc                 C  r   r   rA   r   r   r   r   stats_enabled   rE   z!ObservabilityPlugin.stats_enabledc                 C  s   | j p| jS r   )rD   rF   r   r   r   r   observability_enabled   s   z)ObservabilityPlugin.observability_enabledN)r$   r%   r&   r%   r'   r   )r-   r.   r/   r.   r0   r!   r'   r1   )r3   r!   r'   r4   )
r6   r.   r&   r.   r7   r8   r9   r   r'   r1   )r;   r!   r'   r1   )r$   r%   r'   r1   )r'   r!   )r   r   r   r   r"   __annotations__r#   abcabstractmethodr+   r2   r5   r:   r?   rB   rC   propertyrD   rF   rG   r   r   r   r   r    6   s,   
 


r    )	metaclassr'   4Generator[Optional[ObservabilityPlugin], None, None]c                   c  s2    t  tV  W d   dS 1 sw   Y  dS )zGet the ObservabilityPlugin in _observability module.

    Returns:
      The ObservabilityPlugin currently registered with the _observability
    module. Or None if no plugin exists at the time of calling this method.
    N)r   r   r   r   r   r   
get_plugin   s   "rN   observability_pluginOptional[ObservabilityPlugin]r1   c                 C  s>   t  | rtrtd| aW d   dS 1 sw   Y  dS )zSave ObservabilityPlugin to _observability module.

    Args:
      observability_plugin: The ObservabilityPlugin to save.

    Raises:
      ValueError: If an ObservabilityPlugin was already registered at the
    time of calling this method.
    z%observability_plugin was already set!N)r   r   
ValueErrorrO   r   r   r   
set_plugin   s
   "rS   c                 C  s   t |  dS )ah  Initialize observability with provided ObservabilityPlugin.

    This method have to be called at the start of a program, before any
    channels/servers are built.

    Args:
      observability_plugin: The ObservabilityPlugin to use.

    Raises:
      ValueError: If an ObservabilityPlugin was already registered at the
    time of calling this method.
    N)rS   rR   r   r   r   observability_init   s   rT   c                   C  s   t d t  dS )zClear the observability context, including ObservabilityPlugin and
    ServerCallTracerFactory

    This method have to be called after exit observability context so that
    it's possible to re-initialize again.
    N)rS   _cygrpc clear_server_call_tracer_factoryr   r   r   r   observability_deinit   s   rW   state'_channel._RPCState'c                 C  s   t D ]}|| jdv r dS qt 3}|r6|jr>| j| j }|d }|| j| j|| j	 W d   dS W d   dS W d   dS 1 sIw   Y  dS )zRecord the latency of the RPC, if the plugin is registered and stats is enabled.

    This method will be called at the end of each RPC.

    Args:
      state: a grpc._channel._RPCState object which contains the stats related to the
    RPC.
    utf8Ni  )
r   r6   encoderN   rF   rpc_end_timerpc_start_timer:   r&   code)rX   exclude_prefixpluginrpc_latency_srpc_latency_msr   r   r   maybe_record_rpc_latency  s    

"rc   r3   r!   r
   c                 C  sd   t  %}|r|jrt|| }|rdt|ffW  d    S 	 W d    dS 1 s+w   Y  d S )Nz,grpc.experimental.server_call_tracer_factoryr   )rN   rF   rU   &get_server_call_tracer_factory_addressr   )r3   r`   "server_call_tracer_factory_addressr   r   r   (create_server_call_tracer_factory_option  s   

$rf   )r'   rM   )rO   rP   r'   r1   )rO   r    r'   r1   )r'   r1   )rX   rY   r'   r1   )r3   r!   r'   r
   )'
__future__r   rI   
contextliblogging	threadingtypingr   r   r   r   r   r   grpc._cythonr	   rU   grpc._typingr
   	getLoggerr   _LOGGER_channelr   r   RLockr   rH   r   r   r   ABCMetar    contextmanagerrN   rS   rT   rW   rc   rf   r   r   r   r   <module>   s<    


 



