Skip to content

Commit 2b330e1

Browse files
committed
chore: add warning for measurement name starts with hash
1 parent fade9e0 commit 2b330e1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

influxdb_client/client/write/point.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Point data structure to represent LineProtocol."""
22

33
import math
4+
import warnings
45
from builtins import int
56
from datetime import datetime, timedelta, timezone
67
from decimal import Decimal
@@ -181,6 +182,13 @@ def to_line_protocol(self, precision=None):
181182
:param precision: required precision of LineProtocol. If it's not set then use the precision from ``Point``.
182183
"""
183184
_measurement = _escape_key(self._name, _ESCAPE_MEASUREMENT)
185+
if _measurement.startswith("#"):
186+
message = f"""The measurement name '{_measurement}' start with '#'.
187+
188+
The output Line protocol will be interpret as a comment by InfluxDB. For more info see:
189+
- https://docs.influxdata.com/influxdb/latest/reference/syntax/line-protocol/#comments
190+
"""
191+
warnings.warn(message, SyntaxWarning)
184192
_tags = _append_tags(self._tags)
185193
_fields = _append_fields(self._fields)
186194
if not _fields:
@@ -249,26 +257,26 @@ def _append_fields(fields):
249257
return f"{','.join(_return)}"
250258

251259

252-
def _append_time(time, write_precision):
260+
def _append_time(time, write_precision) -> str:
253261
if time is None:
254262
return ''
255263
return f" {int(_convert_timestamp(time, write_precision))}"
256264

257265

258-
def _escape_key(tag, escape_list=None):
266+
def _escape_key(tag, escape_list=None) -> str:
259267
if escape_list is None:
260268
escape_list = _ESCAPE_KEY
261269
return str(tag).translate(escape_list)
262270

263271

264-
def _escape_tag_value(value):
272+
def _escape_tag_value(value) -> str:
265273
ret = _escape_key(value)
266274
if ret.endswith('\\'):
267275
ret += ' '
268276
return ret
269277

270278

271-
def _escape_string(value):
279+
def _escape_string(value) -> str:
272280
return str(value).translate(_ESCAPE_STRING)
273281

274282

tests/test_point.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, timezone, timedelta
55
from decimal import Decimal
66

7+
import pytest
78
from dateutil import tz
89

910
from influxdb_client import Point, WritePrecision
@@ -494,6 +495,12 @@ def test_static_measurement_name(self):
494495
record_field_keys=["pressure", "temperature"])
495496
self.assertEqual("custom_sensor_id,location=warehouse_125 pressure=125i", point.to_line_protocol())
496497

498+
def test_name_start_with_hash(self):
499+
point = Point.measurement("#hash_start").tag("location", "europe").field("level", 2.2)
500+
with pytest.warns(SyntaxWarning) as warnings:
501+
self.assertEqual('#hash_start,location=europe level=2.2', point.to_line_protocol())
502+
self.assertEqual(1, len(warnings))
503+
497504

498505
if __name__ == '__main__':
499506
unittest.main()

0 commit comments

Comments
 (0)