def decrstat(stat, count=1): if not _is_setup(): log.info('STATSD_HOST or STATSD_PORT not set up') return client = Client(settings.STATSD_HOST, settings.STATSD_PORT) client.decrement(_statname(stat), count) log.info('decrstat: %s %s' % (_statname(stat), count))
#!/usr/bin/env python import time from multiprocessing import Process from pystatsd import Client, Server def worker(): srvr = Server(debug=True, flush_interval=500) srvr.serve() p = Process(target=worker, daemon=False) p.start() time.sleep(1) sc = Client('localhost', 8125) sc.timing('python_test.time', 500) sc.increment('python_test.inc_int') sc.decrement('python_test.decr_int') sc.gauge('python_test.gauge', 42) time.sleep(2) p.terminate()
#!/usr/bin/env python from pystatsd import Client, Server sc = Client('localhost', 8125) sc.timing('python_test.time', 500) sc.increment('python_test.inc_int') sc.decrement('python_test.decr_int') srvr = Server(debug=True) srvr.serve()
#!/usr/bin/env python from pystatsd import Client, Server sc = Client("localhost", 8125) sc.timing("python_test.time", 500) sc.increment("python_test.inc_int") sc.decrement("python_test.decr_int") sc.gauge("python_test.gauge", 42) srvr = Server(debug=True) srvr.serve()
class ClientBasicsTestCase(unittest.TestCase): """ Tests the basic operations of the client """ def setUp(self): self.patchers = [] socket_patcher = mock.patch('pystatsd.statsd.socket.socket') self.mock_socket = socket_patcher.start() self.patchers.append(socket_patcher) self.client = Client() self.addr = (socket.gethostbyname(self.client.host), self.client.port) def test_client_create(self): host, port = ('example.com', 8888) client = Client(host=host, port=port, prefix='pystatsd.tests') self.assertEqual(client.host, host) self.assertEqual(client.port, port) self.assertEqual(client.prefix, 'pystatsd.tests') self.assertEqual(client.addr, (socket.gethostbyname(host), port)) def test_basic_client_incr(self): stat = 'pystatsd.unittests.test_basic_client_incr' stat_str = stat + ':1|c' self.client.increment(stat) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_decr(self): stat = 'pystatsd.unittests.test_basic_client_decr' stat_str = stat + ':-1|c' self.client.decrement(stat) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_update_stats(self): stat = 'pystatsd.unittests.test_basic_client_update_stats' stat_str = stat + ':5|c' self.client.update_stats(stat, 5) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_update_stats_multi(self): stats = [ 'pystatsd.unittests.test_basic_client_update_stats', 'pystatsd.unittests.test_basic_client_update_stats_multi' ] data = dict((stat, "%s|c" % '5') for stat in stats) self.client.update_stats(stats, 5) for stat, value in data.items(): stat_str = stat + value # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_call_any( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_timing(self): stat = 'pystatsd.unittests.test_basic_client_timing.time' stat_str = stat + ':5.000000|ms' self.client.timing(stat, 5) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_timing_since(self): ts = (1971, 6, 29, 4, 13, 0, 0, 0, -1) now = time.mktime(ts) # add 5 seconds ts = (1971, 6, 29, 4, 13, 5, 0, 0, -1) then = time.mktime(ts) mock_time_patcher = mock.patch('time.time', return_value=now) mock_time_patcher.start() stat = 'pystatsd.unittests.test_basic_client_timing_since.time' stat_str = stat + ':-5000000.000000|ms' self.client.timing_since(stat, then) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) mock_time_patcher.stop() def tearDown(self): for patcher in self.patchers: patcher.stop()
class ClientBasicsTestCase(unittest.TestCase): """ Tests the basic operations of the client """ def setUp(self): self.patchers = [] socket_patcher = mock.patch('pystatsd.statsd.socket.socket') self.mock_socket = socket_patcher.start() self.patchers.append(socket_patcher) self.client = Client() self.addr = (socket.gethostbyname(self.client.host), self.client.port) def test_client_create(self): host, port = ('example.com', 8888) client = Client( host=host, port=port, prefix='pystatsd.tests') self.assertEqual(client.host, host) self.assertEqual(client.port, port) self.assertEqual(client.prefix, 'pystatsd.tests') self.assertEqual(client.addr, (socket.gethostbyname(host), port)) def test_basic_client_incr(self): stat = 'pystatsd.unittests.test_basic_client_incr' stat_str = stat + ':1|c' self.client.increment(stat) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_decr(self): stat = 'pystatsd.unittests.test_basic_client_decr' stat_str = stat + ':-1|c' self.client.decrement(stat) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_update_stats(self): stat = 'pystatsd.unittests.test_basic_client_update_stats' stat_str = stat + ':5|c' self.client.update_stats(stat, 5) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_update_stats_multi(self): stats = [ 'pystatsd.unittests.test_basic_client_update_stats', 'pystatsd.unittests.test_basic_client_update_stats_multi' ] data = dict((stat, "%s|c" % '5') for stat in stats) self.client.update_stats(stats, 5) for stat, value in data.items(): stat_str = stat + value # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_call_any( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_timing(self): stat = 'pystatsd.unittests.test_basic_client_timing.time' stat_str = stat + ':5.000000|ms' self.client.timing(stat, 5) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) def test_basic_client_timing_since(self): ts = (1971, 6, 29, 4, 13, 0, 0, 0, -1) now = time.mktime(ts) # add 5 seconds ts = (1971, 6, 29, 4, 13, 5, 0, 0, -1) then = time.mktime(ts) mock_time_patcher = mock.patch('time.time', return_value=now) mock_time_patcher.start() stat = 'pystatsd.unittests.test_basic_client_timing_since.time' stat_str = stat + ':-5000000.000000|ms' self.client.timing_since(stat, then) # thanks tos9 in #python for 'splaining the return_value bit. self.mock_socket.return_value.sendto.assert_called_with( bytes(stat_str, 'utf-8'), self.addr) mock_time_patcher.stop() def tearDown(self): for patcher in self.patchers: patcher.stop()