8000 Coerce floating-point timestamps into `EventTime`. · yyc/fluent-logger-python@82040c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82040c8

Browse files
committed
Coerce floating-point timestamps into EventTime.
1 parent b4e69fc commit 82040c8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

README.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,15 @@ fluentd, with tag 'app.follow' and the attributes 'from' and 'to'.
9090
cur_time = int(time.time())
9191
logger.emit_with_time('follow', cur_time, {'from': 'userA', 'to':'userB'})
9292
93-
To sending events with nanosecond-precision timestamps (Fluent 0.14 and up),
94-
specify `nanosecond_precision` on `FluentSender` or use `sender.EventTime`.
93+
To send events with nanosecond-precision timestamps (Fluent 0.14 and up),
94+
specify `nanosecond_precision` on `FluentSender`.
9595

9696
.. code:: python
9797
98-
# Use current time
98+
# Use nanosecond
9999
logger = sender.FluentSender('app', nanosecond_precision=True)
100100
logger.emit('follow', {'from': 'userA', 'to': 'userB'})
101-
102-
# Specify optional time
103-
cur_time = sender.EventTime(time.time())
104-
logger.emit_with_time('follow', cur_time, {'from': 'userA', 'to':'userB'})
101+
logger.emit_with_time('follow', time.time(), {'from': 'userA', 'to': 'userB'})
105102
106103
You can detect an error via return value of `emit`. If an error happens in `emit`, `emit` returns `False` and get an error object using `last_error` method.
107104

fluent/sender.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def emit(self, label, data):
8383
return self.emit_with_time(label, cur_time, data)
8484

8585
def emit_with_time(self, label, timestamp, data):
86+
if self.nanosecond_precision and isinstance(timestamp, float):
87+
timestamp = EventTime(timestamp)
8688
try:
8789
bytes_ = self._make_packet(label, timestamp, data)
8890
except Exception as e:

tests/test_sender.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def test_nanosecond(self):
7979
self.assertTrue(isinstance(data[0][1], msgpack.ExtType))
8080
eq(data[0][1].code, 0)
8181

82+
def test_nanosecond_coerce_float(self):
83+
time = 1490061367.8616468906402588
84+
sender = self._sender
85+
sender.nanosecond_precision = True
86+
sender.emit_with_time('foo', time, {'bar': 'baz'})
87+
sender._close()
88+
data = self.get_data()
89+
eq = self.assertEqual
90+
eq(1, len(data))
91+
eq(3, len(data[0]))
92+
eq('test.foo', data[0][0])
93+
eq({'bar': 'baz'}, data[0][2])
94+
self.assertTrue(isinstance(data[0][1], msgpack.ExtType))
95+
eq(data[0][1].code, 0)
96+
eq(data[0][1].data, b'X\xd0\x8873[\xb0*')
97+
8298
def test_no_last_error_on_successful_emit(self):
8399
sender = self._sender
84100
sender.emit('foo', {'bar': 'baz'})

0 commit comments

Comments
 (0)
0