<s> """<STR_LIT>""" <EOL> from ConfigParser import NoOptionError , NoSectionError <EOL> import logging <EOL> import random <EOL> import socket <EOL> import sys <EOL> from blueprint import cfg <EOL> try : <EOL> host , port = cfg . get ( '<STR_LIT>' , '<STR_LIT:host>' ) , cfg . getint ( '<STR_LIT>' , '<STR_LIT:port>' ) <EOL> except ( NoOptionError , NoSectionError , ValueError ) : <EOL> host = port = None <EOL> def timing ( stat , time , sample_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> _send ( { stat : '<STR_LIT>' . format ( time ) } , sample_rate ) <EOL> def increment ( stats , sample_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> update ( stats , <NUM_LIT:1> , sample_rate ) <EOL> def decrement ( stats , sample_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> update ( stats , - <NUM_LIT:1> , sample_rate ) <EOL> def update ( stats , delta = <NUM_LIT:1> , sample_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if type ( stats ) is not list : <EOL> stats = [ stats ] <EOL> _send ( dict ( [ ( stat , '<STR_LIT>' . format ( delta ) ) for stat in stats ] ) , sample_rate ) <EOL> def _send ( data , sample_rate = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if host is None or port is None : <EOL> return <EOL> sampled_data = { } <EOL> if <NUM_LIT:1> > sample_rate : <EOL> if random . random ( ) <= sample_rate : <EOL> for k , v in data . iteritems ( ) : <EOL> sampled_data [ k ] = '<STR_LIT>' . format ( v , sample_rate ) <EOL> else : <EOL> sampled_data = data <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> try : <EOL> for k , v in sampled_data . iteritems ( ) : <EOL> s . sendto ( '<STR_LIT>' . format ( k , v ) , ( host , port ) ) <EOL> except : <EOL> logging . error ( repr ( sys . exc_info ( ) ) ) </s>
