Skip to content

chore: add warning for measurement name starts with hash #495

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

Merged
merged 3 commits into from
Aug 23, 2022
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 @@ -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
Expand Down
16 changes: 12 additions & 4 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand Down
7 changes: 7 additions & 0 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()