<s> import random <EOL> from socket import socket , AF_INET , SOCK_DGRAM <EOL> import time <EOL> import logging <EOL> __version__ = '<STR_LIT>' <EOL> STATSD_HOST = '<STR_LIT:localhost>' <EOL> STATSD_PORT = <NUM_LIT> <EOL> STATSD_SAMPLE_RATE = None <EOL> STATSD_BUCKET_PREFIX = None <EOL> def decrement ( bucket , delta = <NUM_LIT:1> , sample_rate = None ) : <EOL> _statsd . decr ( bucket , delta , sample_rate ) <EOL> def increment ( bucket , delta = <NUM_LIT:1> , sample_rate = None ) : <EOL> _statsd . incr ( bucket , delta , sample_rate ) <EOL> def gauge ( bucket , value , sample_rate = None ) : <EOL> _statsd . gauge ( bucket , value , sample_rate ) <EOL> def timing ( bucket , ms , sample_rate = None ) : <EOL> _statsd . timing ( bucket , ms , sample_rate ) <EOL> class StatsdClient ( object ) : <EOL> def __init__ ( self , host = None , port = None , prefix = None , sample_rate = None ) : <EOL> self . _host = host or STATSD_HOST <EOL> self . _port = port or STATSD_PORT <EOL> self . _sample_rate = sample_rate or STATSD_SAMPLE_RATE <EOL> self . _socket = socket ( AF_INET , SOCK_DGRAM ) <EOL> self . _prefix = prefix or STATSD_BUCKET_PREFIX <EOL> if self . _prefix and not isinstance ( self . _prefix , bytes ) : <EOL> self . _prefix = self . _prefix . encode ( '<STR_LIT:utf8>' ) <EOL> def decr ( self , bucket , delta = <NUM_LIT:1> , sample_rate = None ) : <EOL> """<STR_LIT>""" <EOL> value = str ( - <NUM_LIT:1> * delta ) . encode ( '<STR_LIT:utf8>' ) + b'<STR_LIT>' <EOL> self . _send ( bucket , value , sample_rate ) <EOL> def incr ( self , bucket , delta = <NUM_LIT:1> , sample_rate = None ) : <EOL> """<STR_LIT>""" <EOL> value = str ( delta ) . encode ( '<STR_LIT:utf8>' ) + b'<STR_LIT>' <EOL> self . _send ( bucket , value , sample_rate ) <EOL> def gauge ( self , bucket , value , sample_rate = None ) : <EOL> """<STR_LIT>""" <EOL> str_value = str ( value ) . encode ( '<STR_LIT:utf8>' ) + b'<STR_LIT>' <EOL> self . _send ( bucket , str_value , sample_rate ) <EOL> def _send ( self , bucket , value , sample_rate = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> bucket = bucket if isinstance ( bucket , bytes ) else bucket . encode ( '<STR_LIT:utf8>' ) <EOL> sample_rate = sample_rate or self . _sample_rate <EOL> if sample_rate and sample_rate < <NUM_LIT:1.0> and sample_rate > <NUM_LIT:0> : <EOL> if random . random ( ) <= sample_rate : <EOL> value = value + b'<STR_LIT>' + str ( sample_rate ) . encode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> return <EOL> stat = bucket + b'<STR_LIT::>' + value <EOL> if self . _prefix : <EOL> stat = self . _prefix + b'<STR_LIT:.>' + stat <EOL> self . _socket . sendto ( stat , ( self . _host , self . _port ) ) <EOL> except Exception as e : <EOL> _logger . error ( "<STR_LIT>" , exc_info = True ) <EOL> def timing ( self , bucket , ms , sample_rate = None ) : <EOL> """<STR_LIT>""" <EOL> value = str ( ms ) . encode ( '<STR_LIT:utf8>' ) + b'<STR_LIT>' <EOL> self . _send ( bucket , value , sample_rate ) <EOL> class StatsdCounter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bucket , host = None , port = None , prefix = None , <EOL> sample_rate = None ) : <EOL> self . _client = StatsdClient ( host = host , port = port , prefix = prefix , <EOL> sample_rate = sample_rate ) <EOL> self . _bucket = bucket if isinstance ( bucket , bytes ) else bucket . encode ( '<STR_LIT:utf8>' ) <EOL> def __add__ ( self , num ) : <EOL> self . _client . incr ( self . _bucket , delta = num ) <EOL> return self <EOL> def __sub__ ( self , num ) : <EOL> self . _client . decr ( self . _bucket , delta = num ) <EOL> return self <EOL> class StatsdTimer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bucket , host = None , port = None , prefix = None , <EOL> sample_rate = None ) : <EOL> self . _client = StatsdClient ( host = host , port = port , prefix = prefix , <EOL> sample_rate = sample_rate ) <EOL> self . _bucket = bucket if isinstance ( bucket , bytes ) else bucket . encode ( '<STR_LIT:utf8>' ) <EOL> def __enter__ ( self ) : <EOL> self . start ( ) <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> if type is not None : <EOL> self . stop ( b'<STR_LIT>' ) <EOL> else : <EOL> self . stop ( ) <EOL> def start ( self , bucket_key = b'<STR_LIT:start>' ) : <EOL> """<STR_LIT>""" <EOL> bucket_key = bucket_key if isinstance ( bucket_key , bytes ) else bucket_key . encode ( '<STR_LIT:utf8>' ) <EOL> self . _start = time . time ( ) * <NUM_LIT:1000> <EOL> self . _splits = [ ( bucket_key , self . _start ) , ] <EOL> def split ( self , bucket_key ) : <EOL> """<STR_LIT>""" <EOL> bucket_key = bucket_key if isinstance ( bucket_key , bytes ) else bucket_key . encode ( '<STR_LIT:utf8>' ) <EOL> self . _splits . append ( ( bucket_key , time . time ( ) * <NUM_LIT:1000> ) ) <EOL> self . _client . timing ( self . _bucket + b'<STR_LIT:.>' + bucket_key , <EOL> self . _splits [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - self . _splits [ - <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) <EOL> def stop ( self , bucket_key = b'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> bucket_key = bucket_key if isinstance ( bucket_key , bytes ) else bucket_key . encode ( '<STR_LIT:utf8>' ) <EOL> self . _stop = time . time ( ) * <NUM_LIT:1000> <EOL> self . _client . timing ( self . _bucket + b'<STR_LIT:.>' + bucket_key , <EOL> self . _stop - self . _start ) <EOL> @ staticmethod <EOL> def wrap ( bucket ) : <EOL> def wrapper ( func ) : <EOL> def f ( * args , ** kw ) : <EOL> with StatsdTimer ( bucket ) : <EOL> return func ( * args , ** kw ) <EOL> return f <EOL> return wrapper <EOL> def init_statsd ( settings = None ) : <EOL> """<STR_LIT>""" <EOL> global _statsd <EOL> global STATSD_HOST <EOL> global STATSD_PORT <EOL> global STATSD_SAMPLE_RATE <EOL> global STATSD_BUCKET_PREFIX <EOL> if settings : <EOL> STATSD_HOST = settings . get ( '<STR_LIT>' , STATSD_HOST ) <EOL> STATSD_PORT = settings . get ( '<STR_LIT>' , STATSD_PORT ) <EOL> STATSD_SAMPLE_RATE = settings . get ( '<STR_LIT>' , <EOL> STATSD_SAMPLE_RATE ) <EOL> STATSD_BUCKET_PREFIX = settings . get ( '<STR_LIT>' , <EOL> STATSD_BUCKET_PREFIX ) <EOL> _statsd = StatsdClient ( host = STATSD_HOST , port = STATSD_PORT , <EOL> sample_rate = STATSD_SAMPLE_RATE , prefix = STATSD_BUCKET_PREFIX ) <EOL> return _statsd <EOL> _logger = logging . getLogger ( '<STR_LIT>' ) <EOL> _statsd = init_statsd ( ) </s>
