<s> import datetime <EOL> import traceback <EOL> import sys <EOL> import uuid <EOL> from applicationinsights import channel <EOL> NULL_CONSTANT_STRING = '<STR_LIT>' <EOL> class TelemetryClient ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instrumentation_key , telemetry_channel = None ) : <EOL> """<STR_LIT>""" <EOL> if instrumentation_key : <EOL> if isinstance ( instrumentation_key , channel . TelemetryChannel ) : <EOL> telemetry_channel = instrumentation_key <EOL> instrumentation_key = None <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . _context = channel . TelemetryContext ( ) <EOL> self . _context . instrumentation_key = instrumentation_key <EOL> self . _channel = telemetry_channel or channel . TelemetryChannel ( ) <EOL> @ property <EOL> def context ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _context <EOL> @ property <EOL> def channel ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _channel <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _channel . flush ( ) <EOL> def track_pageview ( self , name , url , duration = <NUM_LIT:0> , properties = None , measurements = None ) : <EOL> """<STR_LIT>""" <EOL> data = channel . contracts . PageViewData ( ) <EOL> data . name = name or NULL_CONSTANT_STRING <EOL> data . url = url <EOL> data . duration = duration <EOL> if properties : <EOL> data . properties = properties <EOL> if measurements : <EOL> data . measurements = measurements <EOL> self . _channel . write ( data , self . _context ) <EOL> def track_exception ( self , type = None , value = None , tb = None , properties = None , measurements = None ) : <EOL> """<STR_LIT>""" <EOL> if not type or not value or not tb : <EOL> type , value , tb = sys . exc_info ( ) <EOL> if not type or not value or not tb : <EOL> try : <EOL> raise Exception ( NULL_CONSTANT_STRING ) <EOL> except : <EOL> type , value , tb = sys . exc_info ( ) <EOL> details = channel . contracts . ExceptionDetails ( ) <EOL> details . id = <NUM_LIT:1> <EOL> details . outer_id = <NUM_LIT:0> <EOL> details . type_name = type . __name__ <EOL> details . message = str ( value ) <EOL> details . has_full_stack = True <EOL> counter = <NUM_LIT:0> <EOL> for tb_frame_file , tb_frame_line , tb_frame_function , tb_frame_text in traceback . extract_tb ( tb ) : <EOL> frame = channel . contracts . StackFrame ( ) <EOL> frame . assembly = '<STR_LIT>' <EOL> frame . file_name = tb_frame_file <EOL> frame . level = counter <EOL> frame . line = tb_frame_line <EOL> frame . method = tb_frame_function <EOL> details . parsed_stack . append ( frame ) <EOL> counter += <NUM_LIT:1> <EOL> details . parsed_stack . reverse ( ) <EOL> data = channel . contracts . ExceptionData ( ) <EOL> data . handled_at = '<STR_LIT>' <EOL> data . exceptions . append ( details ) <EOL> if properties : <EOL> data . properties = properties <EOL> if measurements : <EOL> data . measurements = measurements <EOL> self . _channel . write ( data , self . _context ) <EOL> def track_event ( self , name , properties = None , measurements = None ) : <EOL> """<STR_LIT>""" <EOL> data = channel . contracts . EventData ( ) <EOL> data . name = name or NULL_CONSTANT_STRING <EOL> if properties : <EOL> data . properties = properties <EOL> if measurements : <EOL> data . measurements = measurements <EOL> self . _channel . write ( data , self . _context ) <EOL> def track_metric ( self , name , value , type = None , count = None , min = None , max = None , std_dev = None , properties = None ) : <EOL> """<STR_LIT>""" <EOL> dataPoint = channel . contracts . DataPoint ( ) <EOL> dataPoint . name = name or NULL_CONSTANT_STRING <EOL> dataPoint . value = value or <NUM_LIT:0> <EOL> dataPoint . kind = type or channel . contracts . DataPointType . aggregation <EOL> dataPoint . count = count <EOL> dataPoint . min = min <EOL> dataPoint . max = max <EOL> dataPoint . std_dev = std_dev <EOL> data = channel . contracts . MetricData ( ) <EOL> data . metrics . append ( dataPoint ) <EOL> if properties : <EOL> data . properties = properties <EOL> self . _channel . write ( data , self . _context ) <EOL> def track_trace ( self , name , properties = None ) : <EOL> """<STR_LIT>""" <EOL> data = channel . contracts . MessageData ( ) <EOL> data . message = name or NULL_CONSTANT_STRING <EOL> if properties : <EOL> data . properties = properties <EOL> self . _channel . write ( data , self . _context ) <EOL> def track_request ( self , name , url , success , start_time = None , duration = None , response_code = None , http_method = None , properties = None , measurements = None ) : <EOL> """<STR_LIT>""" <EOL> data = channel . contracts . RequestData ( ) <EOL> data . id = str ( uuid . uuid4 ( ) ) <EOL> data . name = name <EOL> data . start_time = start_time or datetime . datetime . utcnow ( ) . isoformat ( ) + '<STR_LIT>' <EOL> local_duration = duration or <NUM_LIT:0> <EOL> duration_parts = [ ] <EOL> for multiplier in [ <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> duration_parts . append ( local_duration % multiplier ) <EOL> local_duration //= multiplier <EOL> duration_parts . reverse ( ) <EOL> data . duration = '<STR_LIT>' % tuple ( duration_parts ) <EOL> if local_duration : <EOL> data . duration = '<STR_LIT>' % ( local_duration , data . duration ) <EOL> data . response_code = response_code or '<STR_LIT>' <EOL> data . success = success <EOL> data . http_method = http_method or '<STR_LIT:GET>' <EOL> data . url = url <EOL> if properties : <EOL> data . properties = properties <EOL> if measurements : <EOL> data . measurements = measurements <EOL> self . channel . write ( data , self . _context ) </s>
