Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit ed276e2

Browse files
mdhausmanxginn8
authored andcommitted
Remove UDP Precision Restrictions (#557)
- address issue #554 - UDP writes can now convert timestamps to the desired precision - add time_precision argument to `InfluxDBClient.send_packet`, defaults to None - add tests for udp precision - remove old udp precision failure tests
1 parent b47362c commit ed276e2

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

influxdb/client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,6 @@ def _write_points(self,
497497
"Invalid time precision is given. "
498498
"(use 'n', 'u', 'ms', 's', 'm' or 'h')")
499499

500-
if self._use_udp and time_precision and time_precision != 's':
501-
raise ValueError(
502-
"InfluxDB only supports seconds precision for udp writes"
503-
)
504-
505500
if protocol == 'json':
506501
data = {
507502
'points': points
@@ -523,7 +518,9 @@ def _write_points(self,
523518
params['rp'] = retention_policy
524519

525520
if self._use_udp:
526-
self.send_packet(data, protocol=protocol)
521+
self.send_packet(
522+
data, protocol=protocol, time_precision=time_precision
523+
)
527524
else:
528525
self.write(
529526
data=data,
@@ -864,17 +861,19 @@ def get_list_privileges(self, username):
864861
text = "SHOW GRANTS FOR {0}".format(quote_ident(username))
865862
return list(self.query(text).get_points())
866863

867-
def send_packet(self, packet, protocol='json'):
864+
def send_packet(self, packet, protocol='json', time_precision=None):
868865
"""Send an UDP packet.
869866
870867
:param packet: the packet to be sent
871868
:type packet: (if protocol is 'json') dict
872869
(if protocol is 'line') list of line protocol strings
873870
:param protocol: protocol of input data, either 'json' or 'line'
874871
:type protocol: str
872+
:param time_precision: Either 's', 'm', 'ms' or 'u', defaults to None
873+
:type time_precision: str
875874
"""
876875
if protocol == 'json':
877-
data = make_lines(packet).encode('utf-8')
876+
data = make_lines(packet, time_precision).encode('utf-8')
878877
elif protocol == 'line':
879878
data = ('\n'.join(packet) + '\n').encode('utf-8')
880879
self.udp_socket.sendto(data, (self._host, self._udp_port))

influxdb/tests/client_test.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,6 @@ def test_write_points_udp(self):
259259
received_data.decode()
260260
)
261261

262-
def test_write_bad_precision_udp(self):
263-
"""Test write bad precision in UDP for TestInfluxDBClient object."""
264-
cli = InfluxDBClient(
265-
'localhost', 8086, 'root', 'root',
266-
'test', use_udp=True, udp_port=4444
267-
)
268-
269-
with self.assertRaisesRegexp(
270-
Exception,
271-
"InfluxDB only supports seconds precision for udp writes"
272-
):
273-
cli.write_points(
274-
self.dummy_points,
275-
time_precision='ms'
276-
)
277-
278262
@raises(Exception)
279263
def test_write_points_fails(self):
280264
"""Test write points fail for TestInfluxDBClient object."""
@@ -335,6 +319,65 @@ def test_write_points_with_precision(self):
335319
m.last_request.body,
336320
)
337321

322+
def test_write_points_with_precision_udp(self):
323+
"""Test write points with precision for TestInfluxDBClient object."""
324+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
325+
port = random.randint(4000, 8000)
326+
s.bind(('0.0.0.0', port))
327+
328+
cli = InfluxDBClient(
329+
'localhost', 8086, 'root', 'root',
330+
'test', use_udp=True, udp_port=port
331+
)
332+
333+
cli.write_points(self.dummy_points, time_precision='n')
334+
received_data, addr = s.recvfrom(1024)
335+
self.assertEqual(
336+
b'cpu_load_short,host=server01,region=us-west '
337+
b'value=0.64 1257894000123456000\n',
338+
received_data,
339+
)
340+
341+
cli.write_points(self.dummy_points, time_precision='u')
342+
received_data, addr = s.recvfrom(1024)
343+
self.assertEqual(
344+
b'cpu_load_short,host=server01,region=us-west '
345+
b'value=0.64 1257894000123456\n',
346+
received_data,
347+
)
348+
349+
cli.write_points(self.dummy_points, time_precision='ms')
350+
received_data, addr = s.recvfrom(1024)
351+
self.assertEqual(
352+
b'cpu_load_short,host=server01,region=us-west '
353+
b'value=0.64 1257894000123\n',
354+
received_data,
355+
)
356+
357+
cli.write_points(self.dummy_points, time_precision='s')
358+
received_data, addr = s.recvfrom(1024)
359+
self.assertEqual(
360+
b"cpu_load_short,host=server01,region=us-west "
361+
b"value=0.64 1257894000\n",
362+
received_data,
363+
)
364+
365+
cli.write_points(self.dummy_points, time_precision='m')
366+
received_data, addr = s.recvfrom(1024)
367+
self.assertEqual(
368+
b'cpu_load_short,host=server01,region=us-west '
369+
b'value=0.64 20964900\n',
370+
received_data,
371+
)
372+
373+
cli.write_points(self.dummy_points, time_precision='h')
374+
received_data, addr = s.recvfrom(1024)
375+
self.assertEqual(
376+
b'cpu_load_short,host=server01,region=us-west '
377+
b'value=0.64 349415\n',
378+
received_data,
379+
)
380+
338381
def test_write_points_bad_precision(self):
339382
"""Test write points w/bad precision TestInfluxDBClient object."""
340383
cli = InfluxDBClient()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy