
    PhD                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ dZ ej                         ZddZddZd Zd Zd	 Zd
 Zd Zd Zd Zd Z G d de      Z G d d      Zy)    N)	threading)ttypes)SERVER_SENDc                 |    t               r2t        j                  | |      }t               }|j	                  |       yy)a   This is annotation API.
    You can add your own annotation from in your code.
    Annotation is recorded with timestamp automatically.
    e.g.) put_annotation('cache hit for %s' % request)

    :param msg: String message
    :param endpoint: host info
    N)	is_sampleZipkinDataBuilderbuild_annotationget_trace_dataadd_annotation)msgendpointa
trace_datas       ^C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\eventlet/zipkin/api.pyput_annotationr      s6     {..sH=#%
!!!$     c                 ~    t               r3t        j                  | ||      }t               }|j	                  |       yy)a!   This is binary annotation API.
    You can add your own key-value extra information from in your code.
    Key-value doesn't have a time component.
    e.g.) put_key_value('http.uri', '/hoge/index.html')

    :param key: String
    :param value: String
    :param endpoint: host info
    N)r   r   build_binary_annotationr
   add_binary_annotation)keyvaluer   br   s        r   put_key_valuer       s8     {55c5(K#%
((+ r   c                  "    t        t        d      S )z6 Return whether the current thread is tracking or not r   )hasattr_tls r   r   
is_tracingr   0   s    4&&r   c                  N    t               xr t        j                  j                  S )zV Return whether it should record trace information
        for the request or not
    )r   r   r   sampledr   r   r   r   r   5   s     <3DOO333r   c                  8    t               rt        j                  S y Nr   r   r   r   r   r   r
   r
   <   s    | r   c                     | t         _        y r"   )r   r   )r   s    r   set_trace_datar%   A   s	     DOr   c                  &    t               rt        `y y r"   r#   r   r   r   init_trace_datar'   E   s    |O r   c                  .    t        j                  dd      S )z
    Create a random 64-bit signed integer appropriate
    for use as trace and span IDs.
    XXX: By experimentation zipkin has trouble recording traces with ids
    larger than (2 ** 56) - 1
    r   l   )randomrandintr   r   r   _uniq_idr+   J   s     >>!]++r   c                      t               S r"   r+   r   r   r   generate_trace_idr.   T   
    :r   c                      t               S r"   r-   r   r   r   generate_span_idr1   X   r/   r   c                   (    e Zd ZeZd Zd Zd Zd Zy)	TraceDatac                     || _         || _        || _        || _        || _        || _        g | _        g | _        d| _        y)aT  
        :param name: RPC name (String)
        :param trace_id: int
        :param span_id: int
        :param parent_id: int or None
        :param sampled: lets the downstream servers know
                    if I should record trace data for the request (bool)
        :param endpoint: zipkin._thrift.zipkinCore.ttypes.EndPoint
        FN)	nametrace_idspan_id	parent_idr    r   annotationsbannotations_done)selfr5   r6   r7   r8   r    r   s          r   __init__zTraceData.__init__`   sE     	 " 
r   c                     |j                   | j                  |_         | j                  sF| j                  j	                  |       |j
                  | j                  k(  r| j                          y y y r"   )hostr   r;   r9   appendr   END_ANNOTATIONflush)r<   
annotations     r   r   zTraceData.add_annotationt   sY    ??""mmJOzz##J/4#6#66

 7 r   c                     |j                   | j                  |_         | j                  s| j                  j	                  |       y y r"   )r?   r   r;   r:   r@   )r<   bannotations     r   r   zTraceData.add_binary_annotation|   s:    ##}}Kzz$$[1 r   c                    t         j                  | j                  | j                  | j                  | j
                  | j                  | j                        }t        j                  |       g | _        g | _        d| _
        y )Nr5   r6   r7   r8   r9   r:   T)r   
build_spanr5   r6   r7   r8   r9   r:   clientsend_to_collectorr;   )r<   spans     r   rB   zTraceData.flush   sl     ++59]]48LL6:nn8<8H8H9=9J9J , L 	  &
r   N)	__name__
__module____qualname__r   rA   r=   r   r   rB   r   r   r   r3   r3   \   s     N(2
r   r3   c                   r    e Zd Zed        Zedd       Zedd       Zed	d       Zed        Zed        Z	y)
r   c                 8    t        j                  | |||||      S )N)r5   r6   idr8   r9   binary_annotations)r   SpanrG   s         r   rH   zZipkinDataBuilder.build_span   s'     {{#+
 	
r   Nc                     t        | t              r| j                  d      } t        j                  t        j
                         dz  dz  t        |       |      S )Nzutf-8i  )
isinstanceunicodeencoder   
Annotationtimestr)r   r   s     r   r	   z"ZipkinDataBuilder.build_annotation   sH    eW%LL)E  t!3d!:!$UX7 	7r   c                 f    t         j                  j                  }t        j                  | |||      S r"   )r   AnnotationTypeSTRINGBinaryAnnotation)r   r   r   annotation_types       r   r   z)ZipkinDataBuilder.build_binary_annotation   s*     //66&&sE?HMMr   c                     | t         j                  |       } |t         j                         }t        j                  | ||      S )Nipv4portservice_name)r   _ipv4_to_int_get_script_namer   Endpointra   s      r   build_endpointz ZipkinDataBuilder.build_endpoint   sF    $11$7D,==?L%
 	
r   c                 Z    t        j                  dt        j                  |             d   S )Nz!ir   )structunpacksocket	inet_aton)rb   s    r   re   zZipkinDataBuilder._ipv4_to_int   s#    }}T6#3#3D#9:1==r   c                  b    t         j                  j                  t        j                  d         S )Nr   )ospathbasenamesysargvr   r   r   rf   z"ZipkinDataBuilder._get_script_name   s    ww,,r   r"   )NNN)
rL   rM   rN   staticmethodrH   r	   r   rh   re   rf   r   r   r   r   r      sz    	
 	
 7 7 N N 	
 	
 > > - -r   r   r"   )ro   rr   rY   rj   rl   r)   eventlet.greenr   "eventlet.zipkin._thrift.zipkinCorer   ,eventlet.zipkin._thrift.zipkinCore.constantsr   rI   localr   r   r   r   r   r
   r%   r'   r+   r.   r1   objectr3   r   r   r   r   <module>rz      s~    	 
     $ 5 D 
y%, '
4
!
,0 0f+- +-r   