3
Z_ð]D  ã               @   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jƒ 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eƒZG dd„ dƒZdS )é    N)Ú	threading)Úttypes)ÚSERVER_SENDc             C   s&   t ƒ r"tj| |ƒ}tƒ }|j|ƒ 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   ú7/tmp/pip-build-yz_zf6az/eventlet/eventlet/zipkin/api.pyÚput_annotation   s    	r   c             C   s(   t ƒ r$tj| ||ƒ}tƒ }|j|ƒ 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 )N)r   r   )r   r   r   r   Úset_trace_dataA   s    r   c               C   s   t ƒ r
t`d S )N)r   r   r   r   r   r   r   Úinit_trace_dataE   s    r   c               C   s   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   é   é8   é   l          l   ÿÿÿÿ)ÚrandomÚrandintr   r   r   r   Ú_uniq_idJ   s    r"   c               C   s   t ƒ S )N)r"   r   r   r   r   Úgenerate_trace_idT   s    r#   c               C   s   t ƒ S )N)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| jkr8| jƒ  d S )N)Ú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j|ƒ d S )N)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j	|ƒ 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   ZSpan)r&   r'   r(   r)   r*   r+   r   r   r   r4   �   s    zZipkinDataBuilder.build_spanNc             C   s2   t | tƒr| jdƒ} tjtjƒ d d t| ƒ|ƒS )Nzutf-8iè  )Ú
isinstanceÚunicodeÚencoder   Z
AnnotationÚtimeÚstr)r   r   r   r   r   r   œ   s    

z"ZipkinDataBuilder.build_annotationc             C   s   t jj}t j| |||ƒS )N)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 j| ƒ} |d kr"t jƒ }tj| ||d�S )N)Úipv4ÚportÚservice_name)r   Ú_ipv4_to_intÚ_get_script_namer   ZEndpoint)rA   rB   rC   r   r   r   Úbuild_endpoint¨   s    
z ZipkinDataBuilder.build_endpointc             C   s   t jdtj| ƒƒd S )Nz!ir   )ÚstructÚunpackÚsocketÚ	inet_aton)rA   r   r   r   rD   ´   s    zZipkinDataBuilder._ipv4_to_intc               C   s   t jjtjd ƒS )Nr   )ÚosÚpathÚbasenameÚsysÚargvr   r   r   r   rE   ¸   s    z"ZipkinDataBuilder._get_script_name)N)N)NNN)
r7   r8   r9   Ústaticmethodr4   r   r   rF   rD   rE   r   r   r   r   r   �   s   r   )N)N)rK   rN   r>   rG   rI   r    Zeventlet.greenr   Z"eventlet.zipkin._thrift.zipkinCorer   Z,eventlet.zipkin._thrift.zipkinCore.constantsr   r5   Úlocalr   r   r   r   r   r   r   r   r"   r#   r$   Úobjectr%   r   r   r   r   r   Ú<module>   s,   


3