o
    ¨Ýj[  ã                   @   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 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   úN/var/www/html/chatgpt/venv/lib/python3.10/site-packages/eventlet/zipkin/api.pyÚput_annotation   s
   	ýr   c                 C   s,   t ƒ rt | ||¡}tƒ }| |¡ dS 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 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   ó   t ƒ S r   ©r!   r   r   r   r   Úgenerate_trace_idT   ó   r$   c                   C   r"   r   r#   r   r   r   r   Úgenerate_span_idX   r%   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   sD   |j d u r	| j|_ | js| j |¡ |j| jkr |  ¡  d S d S 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 u r	| j|_ | js| j |¡ d S d S r   )r1   r   r.   r-   r2   )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   r4   ‚   s   û

zTraceData.flushN)	Ú__name__Ú
__module__Ú__qualname__r   r3   r0   r	   r   r4   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   ÚSpanr6   r   r   r   r7   �   s   úzZipkinDataBuilder.build_spanNc                 C   s<   t | tƒr
|  d¡} t | tƒsJ ‚t t ¡ d d | |¡S )Nzutf-8iè  )Ú
isinstanceÚstrÚencodeÚbytesr   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 ur	t  | ¡} |d u rt  ¡ }tj| ||d�S )N©Úipv4ÚportÚservice_name)r   Ú_ipv4_to_intÚ_get_script_namer   ZEndpointrE   r   r   r   Úbuild_endpoint©   s   
ýz ZipkinDataBuilder.build_endpointc                 C   s   t  dt | ¡¡d S )Nz!ir   )ÚstructÚunpackÚsocketÚ	inet_aton)rF   r   r   r   rI   µ   s   zZipkinDataBuilder._ipv4_to_intc                   C   s   t j tjd ¡S )Nr   )ÚosÚpathÚbasenameÚsysÚargvr   r   r   r   rJ   ¹   s   z"ZipkinDataBuilder._get_script_namer   )NNN)
r:   r;   r<   Ústaticmethodr7   r   r   rK   rI   rJ   r   r   r   r   r   �   s    

r   r   )rP   rS   rC   rL   rN   r   Zeventlet.greenr   Z"eventlet.zipkin._thrift.zipkinCorer   Z,eventlet.zipkin._thrift.zipkinCore.constantsr   r8   Úlocalr   r   r   r   r   r   r   r   r!   r$   r&   r'   r   r   r   r   r   Ú<module>   s.    


3