<s> import math <EOL> import time <EOL> import random <EOL> from zope . interface import implements <EOL> from tryfer . interfaces import ITrace , IAnnotation , IEndpoint <EOL> from tryfer . tracers import get_tracers <EOL> from tryfer . _thrift . zipkinCore import constants <EOL> def _uniq_id ( ) : <EOL> """<STR_LIT>""" <EOL> return random . randint ( <NUM_LIT:0> , ( <NUM_LIT:2> ** <NUM_LIT> ) - <NUM_LIT:1> ) <EOL> class Trace ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ITrace ) <EOL> def __init__ ( self , name , trace_id = None , span_id = None , <EOL> parent_span_id = None , tracers = None ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . trace_id = trace_id or _uniq_id ( ) <EOL> self . span_id = span_id or _uniq_id ( ) <EOL> self . parent_span_id = parent_span_id <EOL> self . _tracers = tracers or get_tracers ( ) <EOL> self . _endpoint = None <EOL> def __eq__ ( self , other ) : <EOL> return ITrace . providedBy ( other ) and ( <EOL> ( self . trace_id , self . span_id , self . parent_span_id ) == <EOL> ( other . trace_id , other . span_id , other . parent_span_id ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __repr__ ( self ) : <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( self ) <EOL> def child ( self , name ) : <EOL> """<STR_LIT>""" <EOL> trace = self . __class__ ( <EOL> name , trace_id = self . trace_id , parent_span_id = self . span_id ) <EOL> trace . set_endpoint ( self . _endpoint ) <EOL> return trace <EOL> def record ( self , * annotations ) : <EOL> for annotation in annotations : <EOL> if annotation . endpoint is None and self . _endpoint is not None : <EOL> annotation . endpoint = self . _endpoint <EOL> for tracer in self . _tracers : <EOL> tracer . record ( [ ( self , annotations ) ] ) <EOL> def set_endpoint ( self , endpoint ) : <EOL> """<STR_LIT>""" <EOL> self . _endpoint = endpoint <EOL> class Endpoint ( object ) : <EOL> implements ( IEndpoint ) <EOL> def __init__ ( self , ipv4 , port , service_name ) : <EOL> """<STR_LIT>""" <EOL> self . ipv4 = ipv4 <EOL> self . port = port <EOL> self . service_name = service_name <EOL> def __eq__ ( self , other ) : <EOL> if other is None : <EOL> return False <EOL> return IEndpoint . providedBy ( other ) and ( <EOL> ( self . ipv4 , self . port , self . service_name ) == <EOL> ( other . ipv4 , other . port , other . service_name ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __repr__ ( self ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( self ) <EOL> class Annotation ( object ) : <EOL> implements ( IAnnotation ) <EOL> def __init__ ( self , name , value , annotation_type , endpoint = None ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . value = value <EOL> self . annotation_type = annotation_type <EOL> self . endpoint = endpoint <EOL> def __eq__ ( self , other ) : <EOL> return IAnnotation . providedBy ( other ) and ( <EOL> ( self . name , self . value , self . annotation_type , self . endpoint ) == <EOL> ( other . name , other . value , other . annotation_type , other . endpoint ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __repr__ ( self ) : <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( self ) <EOL> @ classmethod <EOL> def timestamp ( cls , name , timestamp = None ) : <EOL> if timestamp is None : <EOL> timestamp = math . trunc ( time . time ( ) * <NUM_LIT:1000> * <NUM_LIT:1000> ) <EOL> return cls ( name , timestamp , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def client_send ( cls , timestamp = None ) : <EOL> return cls . timestamp ( constants . CLIENT_SEND , timestamp ) <EOL> @ classmethod <EOL> def client_recv ( cls , timestamp = None ) : <EOL> return cls . timestamp ( constants . CLIENT_RECV , timestamp ) <EOL> @ classmethod <EOL> def server_send ( cls , timestamp = None ) : <EOL> return cls . timestamp ( constants . SERVER_SEND , timestamp ) <EOL> @ classmethod <EOL> def server_recv ( cls , timestamp = None ) : <EOL> return cls . timestamp ( constants . SERVER_RECV , timestamp ) <EOL> @ classmethod <EOL> def string ( cls , name , value ) : <EOL> return cls ( name , value , '<STR_LIT:string>' ) <EOL> @ classmethod <EOL> def bytes ( cls , name , value ) : <EOL> return cls ( name , value , '<STR_LIT>' ) </s>
