3
Z_ð]¤  ã               @   s\   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dlm	Z	 dZ
G dd„ deƒZdS )	é    N)Úscribe)Ú
TTransportÚTSocket)ÚTBinaryProtocol)Ú	GreenPileZzipkinc               @   s>   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )ÚZipkinClientú	127.0.0.1éÂ$  c             C   s"   || _ || _tdƒ| _| jƒ  dS )z‹
        :param host: zipkin collector IP addoress (default '127.0.0.1')
        :param port: zipkin collector port (default 9410)
        é   N)ÚhostÚportr   ÚpileÚ_connect)Úselfr   r   © r   ú:/tmp/pip-build-yz_zf6az/eventlet/eventlet/zipkin/client.pyÚ__init__   s    
zZipkinClient.__init__c             C   s|   t j | j| jƒ}tj|ƒ| _tj| jddƒ}tj|ƒ| _	y| jj
ƒ  W n0 tjk
rv } ztj|jƒ W Y d d }~X nX d S )NF)r   r   r   r   ZTFramedTransportÚ	transportr   r   ZClientÚscribe_clientÚopenZTTransportExceptionÚwarningsÚwarnÚmessage)r   ÚsocketÚprotocolÚer   r   r   r      s    zZipkinClient._connectc             C   s,   t jƒ }tj|d�}|j|ƒ tj|jƒ ƒS )N)Útrans)r   ZTMemoryBufferr   ZTBinaryProtocolAcceleratedÚwriteÚbase64Ú	b64encodeÚgetvalue)r   Z
thrift_objr   r   r   r   r   Ú_build_message%   s    
zZipkinClient._build_messagec             C   s   | j j| j|ƒ d S )N)r   ÚspawnÚ_send)r   Úspanr   r   r   Úsend_to_collector+   s    zZipkinClient.send_to_collectorc             C   sj   t jt| j|ƒƒ}y| jj|gƒ W n@ tk
rd } z$dt|ƒ }tj	|ƒ | j
ƒ  W Y d d }~X nX d S )NzZipkinClient send error %s)r   ZLogEntryÚCATEGORYr!   r   ÚLogÚ	ExceptionÚstrr   r   r   )r   r$   Z	log_entryr   Úmsgr   r   r   r#   .   s    
zZipkinClient._sendc             C   s   | j jƒ  d S )N)r   Úclose)r   r   r   r   r+   7   s    zZipkinClient.closeN)r   r	   )	Ú__name__Ú
__module__Ú__qualname__r   r   r!   r%   r#   r+   r   r   r   r   r      s   

	r   )r   r   r   Zthrift.transportr   r   Zthrift.protocolr   Zeventletr   r&   Úobjectr   r   r   r   r   Ú<module>   s   