Skip to content

Commit 40595a9

Browse files
mayhemamCap1712
andauthored
Fix float values (#70)
* Output floats too * Add error print * Remove quotes * Debug * Tidy up * Fix test * Remove outdated comment * Fix error logging Co-authored-by: Kartik Ohri <kartikohri13@gmail.com>
1 parent 3906b7d commit 40595a9

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

brainzutils/metrics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from functools import wraps
22
import os
33
import socket
4+
import logging
45
from time import time_ns
56
from typing import Dict
67

@@ -58,6 +59,8 @@ def set(metric_name: str, tags: Dict[str, str] = None, timestamp: int = None, **
5859
for k, v in fields.items():
5960
if type(v) == int:
6061
fields_list.append("%s=%di" % (k, v))
62+
elif type(v) == float:
63+
fields_list.append('%s=%f' % (k, v))
6164
elif type(v) == bool:
6265
val = "t" if v else "f"
6366
fields_list.append("%s=%s" % (k, val))
@@ -75,5 +78,4 @@ def set(metric_name: str, tags: Dict[str, str] = None, timestamp: int = None, **
7578
try:
7679
cache._r.rpush(REDIS_METRICS_KEY, metric)
7780
except Exception:
78-
# If we fail to push the metric to redis, so be it.
79-
pass
81+
logging.error("Cannot set redis metric:", exc_info=True)

brainzutils/test/test_metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def test_set(self, rpush):
1919
os.environ["PRIVATE_IP"] = "127.0.0.1"
2020
metrics.set("my_metric", timestamp=1619629462352960742, test_i=2, test_fl=.3, test_t=True, test_f=False, test_s="gobble")
2121
rpush.assert_called_with(metrics.REDIS_METRICS_KEY,
22-
'my_metric,dc=hetzner,server=127.0.0.1,project=listenbrainz.org test_i=2i,test_fl=0.3,test_t=t,test_f=f,test_s="gobble" 1619629462352960742')
22+
'my_metric,dc=hetzner,server=127.0.0.1,project=listenbrainz.org test_i=2i,test_fl=0.300000,test_t=t,test_f=f,test_s="gobble" 1619629462352960742')

0 commit comments

Comments
 (0)