From c2d934ea6bc3b48bbe2a673a3ea7e416ced759df Mon Sep 17 00:00:00 2001 From: Patrick Hensley Date: Tue, 3 Dec 2013 15:49:42 -0500 Subject: [PATCH] Format floating point gauge values; fix tests. --- gstatsd/client.py | 6 +++++- gstatsd/client_test.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gstatsd/client.py b/gstatsd/client.py index 34bcf8a..f6194b8 100644 --- a/gstatsd/client.py +++ b/gstatsd/client.py @@ -9,6 +9,10 @@ import time E_NOSTART = 'you must call start() before stop(). ignoring.' +def _format_float(val): + return ('%f' % val).rstrip('0').rstrip('.') + + class StatsClient(object): "Simple client to exercise the statsd server." @@ -25,7 +29,7 @@ class StatsClient(object): self._send('%s:%d|ms' % (key, round(timestamp)), sample_rate) def gauge(self, key, value, sample_rate=1): - self._send('%s:%d|g' % (key, value), sample_rate) + self._send('%s:%s|g' % (key, _format_float(value)), sample_rate) def increment(self, key, sample_rate=1): return self.counter(key, 1, sample_rate) diff --git a/gstatsd/client_test.py b/gstatsd/client_test.py index 0783098..1f5e8f4 100644 --- a/gstatsd/client_test.py +++ b/gstatsd/client_test.py @@ -56,9 +56,9 @@ class StatsClientTest(unittest.TestCase): def test_gauge(self): self._cli.gauge('foo', 5) self.assertEquals(self._cli.packets[-1], ('foo:5|g', 1)) - self._cli.counter('foo', -50) + self._cli.gauge('foo', -50) self.assertEquals(self._cli.packets[-1], ('foo:-50|g', 1)) - self._cli.counter('foo', 5.9) + self._cli.gauge('foo', 5.9) self.assertEquals(self._cli.packets[-1], ('foo:5.9|g', 1))