class TwistedMetrics(object): """Twisted implementation of statsd output""" def __init__(self, statsd_host="localhost", statsd_port=8125): self.client = TwistedStatsDClient(statsd_host, statsd_port) self._metric = Metrics(connection=self.client, namespace="autopush") def start(self): protocol = StatsDClientProtocol(self.client) reactor.listenUDP(0, protocol) def increment(self, name, count=1, **kwargs): self._metric.increment(name, count) def gauge(self, name, count, **kwargs): self._metric.gauge(name, count) def timing(self, name, duration, **kwargs): self._metric.timing(name, duration)
class TestMetrics(TestCase): def setUp(self): self.connection = FakeStatsDClient() self.metrics = Metrics(self.connection, 'txstatsd.tests') def test_gauge(self): """Test reporting of a gauge metric sample.""" self.metrics.gauge('gauge', 102) self.assertEqual(self.connection.data, 'txstatsd.tests.gauge:102|g') def test_meter(self): """Test reporting of a meter metric sample.""" self.metrics.meter('meter', 3) self.assertEqual(self.connection.data, 'txstatsd.tests.meter:3|m') def test_counter(self): """Test the increment and decrement operations.""" self.metrics.increment('counter', 18) self.assertEqual(self.connection.data, 'txstatsd.tests.counter:18|c') self.metrics.decrement('counter', 9) self.assertEqual(self.connection.data, 'txstatsd.tests.counter:-9|c') def test_timing(self): """Test the timing operation.""" self.metrics.timing('timing', 101.1234) self.assertEqual(self.connection.data, 'txstatsd.tests.timing:101123.4|ms') def test_timing_automatic(self): """Test the automatic timing operation with explicit reset""" start_time = time.time() self.metrics.reset_timing() time.sleep(.1) self.metrics.timing('timing') elapsed = time.time() - start_time label, val, units = re.split(":|\|", self.connection.data) self.assertEqual(label, 'txstatsd.tests.timing') self.assertEqual(units, 'ms') self.assertTrue(100 <= float(val) <= elapsed * 1000) def test_timing_automatic_implicit_reset(self): """Test the automatic timing operation with implicit reset""" start_time = time.time() self.metrics.timing('something_else') time.sleep(.1) self.metrics.timing('timing') elapsed = time.time() - start_time label, val, units = re.split(":|\|", self.connection.data) self.assertEqual(label, 'txstatsd.tests.timing') self.assertEqual(units, 'ms') self.assertTrue(100 <= float(val) <= elapsed * 1000) def test_generic(self): """Test the GenericMetric class.""" self.metrics.report('users', "pepe", "pd") self.assertEqual(self.connection.data, 'txstatsd.tests.users:pepe|pd') def test_generic_extra(self): """Test the GenericMetric class.""" self.metrics.report('users', "pepe", "pd", 100) self.assertEqual(self.connection.data, 'txstatsd.tests.users:pepe|pd|100') def test_empty_namespace(self): """Test reporting of an empty namespace.""" self.metrics.namespace = None self.metrics.gauge('gauge', 213) self.assertEqual(self.connection.data, 'gauge:213|g') self.metrics.namespace = '' self.metrics.gauge('gauge', 413) self.assertEqual(self.connection.data, 'gauge:413|g')
class TestMetrics(TestCase): def setUp(self): self.connection = FakeStatsDClient() self.metrics = Metrics(self.connection, 'txstatsd.tests') def test_gauge(self): """Test reporting of a gauge metric sample.""" self.metrics.gauge('gauge', 102) self.assertEqual(self.connection.data, b'txstatsd.tests.gauge:102|g') def test_gauge_with_tags(self): """Test reporting of a gauge metric sample w/ tags.""" self.metrics.gauge('gauge', 102, tags=["foo:bar"]) self.assertEqual(self.connection.data, b'txstatsd.tests.gauge:102|g|#foo:bar') def test_meter(self): """Test reporting of a meter metric sample.""" self.metrics.meter('meter', 3) self.assertEqual(self.connection.data, b'txstatsd.tests.meter:3|m') def test_counter(self): """Test the increment and decrement operations.""" self.metrics.increment('counter', 18) self.assertEqual(self.connection.data, b'txstatsd.tests.counter:18|c') self.metrics.decrement('counter', 9) self.assertEqual(self.connection.data, b'txstatsd.tests.counter:-9|c') def test_counter_with_tags(self): """Test the increment and decrement operations w/ tags.""" self.metrics.increment('counter', 18, tags=["foo:bar"]) self.assertEqual(self.connection.data, b'txstatsd.tests.counter:18|c|#foo:bar') self.metrics.decrement('counter', 9, tags=["foo:bar", "baz:quux"]) self.assertEqual(self.connection.data, b'txstatsd.tests.counter:-9|c|#foo:bar,baz:quux') def test_timing(self): """Test the timing operation.""" self.metrics.timing('timing', 101.1234) # to support both 2 and 3, can't use assertRegex match = re.match(b'txstatsd.tests.timing:101123.4[0-9]*|ms', self.connection.data) self.assertFalse(match is None) def test_timing_with_tags(self): """Test the timing operation w/ tags.""" self.metrics.timing('timing', 101.1234, tags=["foo:bar"]) # to support both 2 and 3, can't use assertRegex match = re.match(b'txstatsd.tests.timing:101123.4[0-9]*|ms|#foo:bar', self.connection.data) self.assertFalse(match is None) def test_timing_automatic(self): """Test the automatic timing operation with explicit reset""" start_time = time.time() self.metrics.reset_timing() time.sleep(.1) self.metrics.timing('timing') elapsed = time.time() - start_time label, val, units = re.split(b":|\|", self.connection.data) self.assertEqual(label, b'txstatsd.tests.timing') self.assertEqual(units, b'ms') self.assertTrue(100 <= float(val) <= elapsed * 1000) def test_timing_automatic_implicit_reset(self): """Test the automatic timing operation with implicit reset""" start_time = time.time() self.metrics.timing('something_else') time.sleep(.1) self.metrics.timing('timing') elapsed = time.time() - start_time label, val, units = re.split(b":|\|", self.connection.data) self.assertEqual(label, b'txstatsd.tests.timing') self.assertEqual(units, b'ms') self.assertTrue(100 <= float(val) <= elapsed * 1000) def test_generic(self): """Test the GenericMetric class.""" self.metrics.report('users', "pepe", "pd") self.assertEqual(self.connection.data, b'txstatsd.tests.users:pepe|pd') def test_generic_extra(self): """Test the GenericMetric class.""" self.metrics.report('users', "pepe", "pd", 100) self.assertEqual(self.connection.data, b'txstatsd.tests.users:pepe|pd|100') def test_empty_namespace(self): """Test reporting of an empty namespace.""" self.metrics.namespace = None self.metrics.gauge('gauge', 213) self.assertEqual(self.connection.data, b'gauge:213|g') self.metrics.namespace = '' self.metrics.gauge('gauge', 413) self.assertEqual(self.connection.data, b'gauge:413|g')