From 6d0312aea333a93185a37e5bab57590cff3864a2 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 25 Jun 2020 12:40:49 +0200 Subject: [PATCH 1/2] fix: serialization of `\n`, `\r` and `\t` to Line Protocol --- CHANGELOG.md | 1 + influxdb_client/client/write/point.py | 2 +- tests/test_point.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d415d1d..6f1b6274 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp ### Bug Fixes +1. [#115](https://github.com/influxdata/influxdb-client-python/pull/115): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol 1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point ## 1.8.0 [2020-06-19] diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index d50117c0..af8c22eb 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -12,7 +12,7 @@ EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) DEFAULT_WRITE_PRECISION = WritePrecision.NS -_ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': ''}) +_ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': '\\n', '\t': '\\t', '\r': '\\r'}) _ESCAPE_STRING = str.maketrans({'\"': r"\"", "\\": r"\\"}) diff --git a/tests/test_point.py b/tests/test_point.py index 698af330..8e91d3c9 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -36,6 +36,16 @@ def test_TagEmptyValue(self): self.assertEqual("h2o,location=europe level=2i", point.to_line_protocol()) + def test_TagEscapingKeyAndValue(self): + + point = Point.measurement("h\n2\ro\t_data") \ + .tag("new\nline", "new\nline") \ + .tag("carriage\rreturn", "carriage\nreturn") \ + .tag("t\tab", "t\tab") \ + .field("level", 2) + + self.assertEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.to_line_protocol()) + def test_OverrideTagField(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ From 1833b14d21f534abcf3f3965e663a1fe842bf8e5 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 26 Jun 2020 10:14:11 +0200 Subject: [PATCH 2/2] fix: `=` is valid sign for measurement name --- CHANGELOG.md | 2 +- influxdb_client/client/write/point.py | 9 ++++++--- tests/test_point.py | 8 ++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1b6274..20e87db0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ 1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp ### Bug Fixes -1. [#115](https://github.com/influxdata/influxdb-client-python/pull/115): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol 1. [#117](https://github.com/influxdata/influxdb-client-python/pull/117): Fixed appending default tags for single Point +1. [#115](https://github.com/influxdata/influxdb-client-python/pull/115): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name ## 1.8.0 [2020-06-19] diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index af8c22eb..1141f17b 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -12,6 +12,7 @@ EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) DEFAULT_WRITE_PRECISION = WritePrecision.NS +_ESCAPE_MEASUREMENT = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '\n': '\\n', '\t': '\\t', '\r': '\\r'}) _ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': '\\n', '\t': '\\t', '\r': '\\r'}) _ESCAPE_STRING = str.maketrans({'\"': r"\"", "\\": r"\\"}) @@ -75,7 +76,7 @@ def field(self, field, value): return self def to_line_protocol(self): - _measurement = _escape_key(self._name) + _measurement = _escape_key(self._name, _ESCAPE_MEASUREMENT) _tags = _append_tags(self._tags) _fields = _append_fields(self._fields) if not _fields: @@ -133,8 +134,10 @@ def _append_time(time, write_precision): return f" {int(_convert_timestamp(time, write_precision))}" -def _escape_key(tag): - return str(tag).translate(_ESCAPE_KEY) +def _escape_key(tag, escape_list=None): + if escape_list is None: + escape_list = _ESCAPE_KEY + return str(tag).translate(escape_list) def _escape_tag_value(value): diff --git a/tests/test_point.py b/tests/test_point.py index 8e91d3c9..f5d71317 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -46,6 +46,14 @@ def test_TagEscapingKeyAndValue(self): self.assertEqual("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i", point.to_line_protocol()) + def test_EqualSignEscaping(self): + + point = Point.measurement("h=2o") \ + .tag("l=ocation", "e=urope") \ + .field("l=evel", 2) + + self.assertEqual("h=2o,l\\=ocation=e\\=urope l\\=evel=2i", point.to_line_protocol()) + def test_OverrideTagField(self): point = Point.measurement("h2o") \ .tag("location", "europe") \