From cc978df6aed7dadeae6f101f1f5b1e59dbd77bdd Mon Sep 17 00:00:00 2001 From: syhan Date: Thu, 16 May 2019 15:24:54 +0800 Subject: [PATCH] Fix newline in tag value cause partial commit When a tag value contains newline(\n), the request sent to db would be splitted into two parts and the first part would fail to write to db but the second woudl be succeed. The reason is that before sending we do serialization (make_lines) the _escape_tag method in line_protocol.py won't handle it well, we need somehow more specific on newline instead of only handling escape character (\) --- influxdb/line_protocol.py | 2 ++ influxdb/tests/test_line_protocol.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index e8816fc0..249511d3 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -54,6 +54,8 @@ def _escape_tag(tag): ",", "\\," ).replace( "=", "\\=" + ).replace( + "\n", "\\n" ) diff --git a/influxdb/tests/test_line_protocol.py b/influxdb/tests/test_line_protocol.py index a3d84793..bccd7727 100644 --- a/influxdb/tests/test_line_protocol.py +++ b/influxdb/tests/test_line_protocol.py @@ -115,6 +115,27 @@ def test_make_lines_unicode(self): 'test,unicode_tag=\'Привет!\' unicode_val="Привет!"\n' ) + def test_tag_value_newline(self): + """Test make lines with tag value contains newline.""" + data = { + "tags": { + "t1": "line1\nline2" + }, + "points": [ + { + "measurement": "test", + "fields": { + "val": "hello" + } + } + ] + } + + self.assertEqual( + line_protocol.make_lines(data), + 'test,t1=line1\\nline2 val="hello"\n' + ) + def test_quote_ident(self): """Test quote indentation in TestLineProtocol object.""" self.assertEqual(