summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Socol <me@jamessocol.com>2013-08-27 07:15:49 -0700
committerJames Socol <me@jamessocol.com>2013-08-27 07:15:49 -0700
commit08a0102d6ad81ad41da827366cc6bba165c993bd (patch)
treeb4cb04fcaa0a530dd9deb429dd27c10f287ac2e2
parent4d70ac43710d2045e3cd23127ac88b1837497949 (diff)
parent6d470fdebc3abdacdc1b2c0ccc150968c8082702 (diff)
downloadpystatsd-08a0102d6ad81ad41da827366cc6bba165c993bd.tar.gz
Merge pull request #32 from jsocol/big-numbers
Handle big numbers correctly.
-rw-r--r--statsd/client.py6
-rw-r--r--statsd/tests.py41
2 files changed, 33 insertions, 14 deletions
diff --git a/statsd/client.py b/statsd/client.py
index 951fd81..fd5e43f 100644
--- a/statsd/client.py
+++ b/statsd/client.py
@@ -70,10 +70,8 @@ class StatsClient(object):
def gauge(self, stat, value, rate=1, delta=False):
"""Set a gauge value."""
- if delta:
- value = '%+g|g' % value
- else:
- value = '%g|g' % value
+ prefix = '+' if delta and value >= 0 else ''
+ value = '%s%s|g' % (prefix, value)
data = self._prepare(stat, value, rate)
if data is not None:
self._after(data)
diff --git a/statsd/tests.py b/statsd/tests.py
index 1e6f6e0..75bee3d 100644
--- a/statsd/tests.py
+++ b/statsd/tests.py
@@ -132,18 +132,20 @@ def test_gauge():
def test_gauge_delta():
- sc = _client()
- sc.gauge('foo', 12, delta=True)
- _sock_check(sc, 1, 'foo:+12|g')
-
- sc.gauge('foo', -13, delta=True)
- _sock_check(sc, 2, 'foo:-13|g')
+ tests = (
+ (12, '+12'),
+ (-13, '-13'),
+ (1.2, '+1.2'),
+ (-1.3, '-1.3'),
+ )
- sc.gauge('foo', 1.2, delta=True)
- _sock_check(sc, 3, 'foo:+1.2|g')
+ def _check(num, result):
+ sc = _client()
+ sc.gauge('foo', num, delta=True)
+ _sock_check(sc, 1, 'foo:%s|g' % result)
- sc.gauge('foo', -1.3, delta=True)
- _sock_check(sc, 4, 'foo:-1.3|g')
+ for num, result in tests:
+ yield _check, num, result
@mock.patch.object(random, 'random', lambda: -1)
@@ -350,3 +352,22 @@ def test_pipeline_packet_size():
eq_(2, sc._sock.sendto.call_count)
assert len(sc._sock.sendto.call_args_list[0][0][0]) <= 512
assert len(sc._sock.sendto.call_args_list[1][0][0]) <= 512
+
+
+def test_big_numbers():
+ num = 1234568901234
+ result = 'foo:1234568901234|%s'
+ tests = (
+ # Explicitly create strings so we avoid the bug we're trying to test.
+ ('gauge', 'g'),
+ ('incr', 'c'),
+ ('timing', 'ms'),
+ )
+
+ def _check(method, suffix):
+ sc = _client()
+ getattr(sc, method)('foo', num)
+ _sock_check(sc, 1, result % suffix)
+
+ for method, suffix in tests:
+ yield _check, method, suffix