diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c336bb5..09bbb80f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Others 1. [#472](https://github.com/influxdata/influxdb-client-python/pull/472): Drop supports for Python 3.6 +1. [#495](https://github.com/influxdata/influxdb-client-python/pull/495): Add warning for measurement name starts with `#` ### Documentation 1. [#397](https://github.com/influxdata/influxdb-client-python/pull/397): Add an example: How to use RxPY to prepare batches by maximum bytes count diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index 3b9e5439..3bee403f 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -1,6 +1,7 @@ """Point data structure to represent LineProtocol.""" import math +import warnings from builtins import int from datetime import datetime, timedelta, timezone from decimal import Decimal @@ -181,6 +182,13 @@ def to_line_protocol(self, precision=None): :param precision: required precision of LineProtocol. If it's not set then use the precision from ``Point``. """ _measurement = _escape_key(self._name, _ESCAPE_MEASUREMENT) + if _measurement.startswith("#"): + message = f"""The measurement name '{_measurement}' start with '#'. + +The output Line protocol will be interpret as a comment by InfluxDB. For more info see: + - https://docs.influxdata.com/influxdb/latest/reference/syntax/line-protocol/#comments +""" + warnings.warn(message, SyntaxWarning) _tags = _append_tags(self._tags) _fields = _append_fields(self._fields) if not _fields: @@ -249,26 +257,26 @@ def _append_fields(fields): return f"{','.join(_return)}" -def _append_time(time, write_precision): +def _append_time(time, write_precision) -> str: if time is None: return '' return f" {int(_convert_timestamp(time, write_precision))}" -def _escape_key(tag, escape_list=None): +def _escape_key(tag, escape_list=None) -> str: if escape_list is None: escape_list = _ESCAPE_KEY return str(tag).translate(escape_list) -def _escape_tag_value(value): +def _escape_tag_value(value) -> str: ret = _escape_key(value) if ret.endswith('\\'): ret += ' ' return ret -def _escape_string(value): +def _escape_string(value) -> str: return str(value).translate(_ESCAPE_STRING) diff --git a/tests/test_point.py b/tests/test_point.py index 4b08c6cb..51552f02 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -4,6 +4,7 @@ from datetime import datetime, timezone, timedelta from decimal import Decimal +import pytest from dateutil import tz from influxdb_client import Point, WritePrecision @@ -494,6 +495,12 @@ def test_static_measurement_name(self): record_field_keys=["pressure", "temperature"]) self.assertEqual("custom_sensor_id,location=warehouse_125 pressure=125i", point.to_line_protocol()) + def test_name_start_with_hash(self): + point = Point.measurement("#hash_start").tag("location", "europe").field("level", 2.2) + with pytest.warns(SyntaxWarning) as warnings: + self.assertEqual('#hash_start,location=europe level=2.2', point.to_line_protocol()) + self.assertEqual(1, len(warnings)) + if __name__ == '__main__': unittest.main()