o
    7?eJ'                     @  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 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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"d,d%d&Z#dS )-    )annotationsN)Any	GeneratorGenericListOptionalTypeVar)cygrpc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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dZejd/ddZejd0ddZd1d!d"Zd1d#d$Zed2d%d&Zed2d'd(Zed2d)d*Zd+S )3ObservabilityPluginan  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 inject 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.
    Fbool_tracing_enabled_stats_enabledmethod_namebytesreturnr
   c                 C     t  )aH  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.

        Returns:
        A PyCapsule which stores a ClientCallTracer object.
        NotImplementedError)selfr    r   T/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/grpc/_observability.pycreate_client_call_tracer?      z-ObservabilityPlugin.create_client_call_tracerclient_call_tracerNonec                 C  r   )a  Deletes the ClientCallTracer stored in ClientCallTracerCapsule.

        After register the plugin, if tracing or stats is enabled, this method
        will be called at the end of the call to destroy the ClientCallTracer.

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

        Args:
        client_call_tracer: A PyCapsule which stores a ClientCallTracer object.
        r   )r   r   r   r   r   delete_client_call_tracerT   s   z-ObservabilityPlugin.delete_client_call_tracertrace_idstrspan_id
is_sampledc                 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#   r$   r   r   r   save_trace_contexte   r   z&ObservabilityPlugin.save_trace_contextr   c                 C  r   )a2  Creates a ServerCallTracerFactoryCapsule.

        After register the plugin, if tracing or stats is enabled, this method
        will be called by calling observability_init, the ServerCallTracerFactory
        created by this method 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.

        Returns:
        A PyCapsule which stores a ServerCallTracerFactory object.
        r   r   r   r   r   !create_server_call_tracer_factoryz   s   z5ObservabilityPlugin.create_server_call_tracer_factorymethodrpc_latencyfloatstatus_coder   c                 C  r   )a  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.
        rpc_latency: The latency for the RPC, 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   r(   r)   r+   r   r   r   record_rpc_latency   s   z&ObservabilityPlugin.record_rpc_latencyenablec                 C  
   || _ dS )zvEnable 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   r0   r   r   r   	set_stats   r2   zObservabilityPlugin.set_statsc                 C     | j S Nr/   r&   r   r   r   tracing_enabled      z#ObservabilityPlugin.tracing_enabledc                 C  r5   r6   r3   r&   r   r   r   stats_enabled   r8   z!ObservabilityPlugin.stats_enabledc                 C  s   | j p| jS r6   )r7   r9   r&   r   r   r   observability_enabled   s   z)ObservabilityPlugin.observability_enabledN)r   r   r   r
   )r   r
   r   r   )r!   r"   r#   r"   r$   r   r   r   )r   r   )r(   r"   r)   r*   r+   r   r   r   )r-   r   r   r   )r   r   )__name__
__module____qualname____doc__r   __annotations__r   abcabstractmethodr   r    r%   r'   r,   r1   r4   propertyr7   r9   r:   r   r   r   r   r   '   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   "rE   observability_pluginOptional[ObservabilityPlugin]r   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rF   r   r   r   
set_plugin   s
   "rJ   c                 C  s8   t |  zt|  W dS  ty   td Y dS w )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.
    z)Failed to set server call tracer factory!N)rJ   _cygrpcZset_server_call_tracer_factory	Exception_LOGGER	exceptionrI   r   r   r   observability_init   s   rO   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)rJ   rK   Z clear_server_call_tracer_factoryr   r   r   r   observability_deinit   s   rP   client_call_tracer_capsuler   c                 C  sR   t  }|r	|js	 W d   dS ||  W d   dS 1 s"w   Y  dS )a  Deletes the ClientCallTracer stored in ClientCallTracerCapsule.

    This method will be called at the end of the call to destroy the ClientCallTracer.

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

    Args:
    client_call_tracer_capsule: A PyCapsule which stores a ClientCallTracer object.
    N)rE   r:   r    )rQ   pluginr   r   r   delete_call_tracer   s   
"rS   state'_channel._RPCState'c                 C  s   t D ]}|| jdv r dS qt ,}|r|js"	 W d   dS | j| j }| d }|| j|| j	 W d   dS 1 sBw   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   r(   encoderE   r9   Zrpc_end_timeZrpc_start_timetotal_secondsr,   code)rT   Zexclude_prefixrR   r)   Zrpc_latency_msr   r   r   maybe_record_rpc_latency	  s   

"rZ   )r   rD   )rF   rG   r   r   )rF   r   r   r   )r   r   )rQ   r   r   r   )rT   rU   r   r   )$
__future__r   r@   
contextliblogging	threadingtypingr   r   r   r   r   r   Zgrpc._cythonr	   rK   	getLoggerr;   rM   Z_channelr
   r   RLockr   r?   r   r   ABCMetar   contextmanagerrE   rJ   rO   rP   rS   rZ   r   r   r   r   <module>   s8    


 



