Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name #115

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Bug Fixes
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]

Expand Down
11 changes: 7 additions & 4 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
DEFAULT_WRITE_PRECISION = WritePrecision.NS
_ESCAPE_KEY = str.maketrans({'\\': '\\\\', ',': r'\,', ' ': r'\ ', '=': r'\=', '\n': ''})
_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"\\"})


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ 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_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") \
Expand Down