U
    @f[                     @   s   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 ZdddZdddZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd dZG dd dZdS )    N)	threading)ttypes)SERVER_SENDc                 C   s&   t  r"t| |}t }|| dS )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_data r   M/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/zipkin/api.pyput_annotation   s    	r   c                 C   s(   t  r$t| ||}t }|| dS )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   r   r   r   put_key_value    s    
r   c                   C   s
   t tdS )z6 Return whether the current thread is tracking or not r   )hasattr_tlsr   r   r   r   
is_tracing0   s    r   c                   C   s   t  o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    r   c                   C   s   t  rtjS d S Nr   r   r   r   r   r   r   r   <   s    r   c                 C   s
   | t _d S r   )r   r   )r   r   r   r   set_trace_dataA   s    r   c                   C   s   t  r
t`d S r   r   r   r   r   r   init_trace_dataE   s    r   c                   C   s   t 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   r   _uniq_idJ   s    r!   c                   C   s   t  S r   r!   r   r   r   r   generate_trace_idT   s    r#   c                   C   s   t  S r   r"   r   r   r   r   generate_span_idX   s    r$   c                   @   s0   e Zd ZeZdd Zdd Zdd Zdd Zd	S )
	TraceDatac                 C   s:   || _ || _|| _|| _|| _|| _g | _g | _d| _dS )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)selfr&   r'   r(   r)   r   r   r   r   r   __init__`   s    
zTraceData.__init__c                 C   s<   |j d kr| j|_ | js8| j| |j| jkr8|   d S r   )hostr   r,   r*   appendr   END_ANNOTATIONflush)r-   
annotationr   r   r   r	   t   s    
zTraceData.add_annotationc                 C   s(   |j d kr| j|_ | js$| j| d S r   )r/   r   r,   r+   r0   )r-   Zbannotationr   r   r   r   |   s    
zTraceData.add_binary_annotationc                 C   sB   t j| j| j| j| j| j| jd}t	| g | _g | _d| _
d S )Nr&   r'   r(   r)   r*   r+   T)r   
build_spanr&   r'   r(   r)   r*   r+   clientZsend_to_collectorr,   )r-   spanr   r   r   r2      s    
zTraceData.flushN)	__name__
__module____qualname__r   r1   r.   r	   r   r2   r   r   r   r   r%   \   s
   r%   c                   @   sZ   e Zd Zedd ZedddZedddZeddd	Zed
d Zedd Z	dS )r   c                 C   s   t j| |||||dS )N)r&   r'   idr)   r*   Zbinary_annotations)r   Spanr4   r   r   r   r5      s    zZipkinDataBuilder.build_spanNc                 C   s<   t | tr| d} t | ts"ttt d d | |S )Nzutf-8i  )
isinstancestrencodebytesAssertionErrorr   Z
Annotationtime)r   r   r   r   r   r      s    

 z"ZipkinDataBuilder.build_annotationc                 C   s   t jj}t | |||S r   )r   ZAnnotationTypeSTRINGZBinaryAnnotation)r   r   r   Zannotation_typer   r   r   r      s    z)ZipkinDataBuilder.build_binary_annotationc                 C   s2   | d k	rt | } |d kr"t  }tj| ||dS )Nipv4portservice_name)r   _ipv4_to_int_get_script_namer   ZEndpointrD   r   r   r   build_endpoint   s    
z ZipkinDataBuilder.build_endpointc                 C   s   t dt| d S )Nz!ir   )structunpacksocket	inet_aton)rE   r   r   r   rH      s    zZipkinDataBuilder._ipv4_to_intc                   C   s   t jtjd S )Nr   )ospathbasenamesysargvr   r   r   r   rI      s    z"ZipkinDataBuilder._get_script_name)N)N)NNN)
r8   r9   r:   staticmethodr5   r   r   rJ   rH   rI   r   r   r   r   r      s   

r   )N)N)rO   rR   rB   rK   rM   r   Zeventlet.greenr   Z"eventlet.zipkin._thrift.zipkinCorer   Z,eventlet.zipkin._thrift.zipkinCore.constantsr   r6   localr   r   r   r   r   r   r   r   r!   r#   r$   r%   r   r   r   r   r   <module>   s,   


3