From 6a606e350d74e5486fa4d43d9c48ed41f8f29b31 Mon Sep 17 00:00:00 2001 From: Marat Komarov Date: Thu, 26 Oct 2017 12:34:59 -0700 Subject: [PATCH 1/9] FAM-1236 Bubble requests ConnectionError --- influxdb/client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/influxdb/client.py b/influxdb/client.py index a8adb915..98762698 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -235,6 +235,7 @@ def request(self, url, method='GET', params=None, data=None, # Try to send the request more than once by default (see #103) retry = True _try = 0 + _last_exc = None while retry: try: response = self._session.request( @@ -249,12 +250,15 @@ def request(self, url, method='GET', params=None, data=None, timeout=self._timeout ) break - except requests.exceptions.ConnectionError: + except requests.exceptions.ConnectionError as e: + _last_exc = e _try += 1 if self._retries != 0: retry = _try < self._retries else: + if _last_exc: + raise _last_exc raise requests.exceptions.ConnectionError if 500 <= response.status_code < 600: From fb3d77c163edf87f93579b78f32828ec5aaf107f Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 30 Oct 2017 11:51:52 +0200 Subject: [PATCH 2/9] FAM-1291 make request.Session connection pool configurable for influxdbClient --- influxdb/client.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/influxdb/client.py b/influxdb/client.py index a8adb915..3c7bde67 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -12,6 +12,7 @@ import socket import requests import requests.exceptions +import requests.adapters from influxdb.line_protocol import make_lines, quote_ident, quote_literal from influxdb.resultset import ResultSet @@ -28,6 +29,8 @@ else: from urlparse import urlparse +DEFAULT_POOL_SIZE = 10 + class InfluxDBClient(object): """InfluxDBClient primary client object to connect InfluxDB. @@ -71,6 +74,7 @@ def __init__(self, port=8086, username='root', password='root', + pool_size=DEFAULT_POOL_SIZE, database=None, ssl=False, verify_ssl=False, @@ -94,6 +98,9 @@ def __init__(self, self.__use_udp = use_udp self.__udp_port = udp_port self._session = requests.Session() + adapter = requests.adapters.HTTPAdapter(pool_connections=pool_size, pool_maxsize=pool_size) + self._session.mount('http://', adapter) + self._session.mount('https://', adapter) if use_udp: self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) From 2c13f559e7e3b8889681ab6ece7a5e870b10e39a Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 30 Oct 2017 12:11:18 +0200 Subject: [PATCH 3/9] FAM-1163 escape tag values that ends with backslash --- influxdb/line_protocol.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index c399a1d5..78699876 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -57,6 +57,13 @@ def _escape_tag(tag): ) +def _escape_tag_value(value): + ret = _escape_tag(value) + if ret.endswith('\\ ') + ret += ' ' + return ret + + def quote_ident(value): """Indent the quotes.""" return "\"{}\"".format(value @@ -135,7 +142,7 @@ def make_lines(data, precision=None): # tags should be sorted client-side to take load off server for tag_key, tag_value in sorted(iteritems(tags)): key = _escape_tag(tag_key) - value = _escape_tag(tag_value) + value = _escape_tag_value(tag_value) if key != '' and value != '': key_values.append(key + "=" + value) From f98c29d3012313509c353d412fee66024ec3d55b Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 30 Oct 2017 12:17:58 +0200 Subject: [PATCH 4/9] FAM-1163 typo fix --- influxdb/line_protocol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index 78699876..e8816fc0 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -59,7 +59,7 @@ def _escape_tag(tag): def _escape_tag_value(value): ret = _escape_tag(value) - if ret.endswith('\\ ') + if ret.endswith('\\'): ret += ' ' return ret From ea9339305859371168e02708588a9802865d248e Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Tue, 31 Oct 2017 12:20:24 +0200 Subject: [PATCH 5/9] FAM-1291 code review changes --- influxdb/client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 3c7bde67..bb9a2206 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -29,8 +29,6 @@ else: from urlparse import urlparse -DEFAULT_POOL_SIZE = 10 - class InfluxDBClient(object): """InfluxDBClient primary client object to connect InfluxDB. @@ -47,6 +45,8 @@ class InfluxDBClient(object): :type username: str :param password: password of the user, defaults to 'root' :type password: str + :param pool_size: urllib3 connection pool size, defaults to 10. + :type pool_size: int :param database: database name to connect to, defaults to None :type database: str :param ssl: use https instead of http to connect to InfluxDB, defaults to @@ -74,7 +74,7 @@ def __init__(self, port=8086, username='root', password='root', - pool_size=DEFAULT_POOL_SIZE, + pool_size=10, database=None, ssl=False, verify_ssl=False, @@ -99,16 +99,16 @@ def __init__(self, self.__udp_port = udp_port self._session = requests.Session() adapter = requests.adapters.HTTPAdapter(pool_connections=pool_size, pool_maxsize=pool_size) - self._session.mount('http://', adapter) - self._session.mount('https://', adapter) + if use_udp: self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._scheme = "http" - if ssl is True: self._scheme = "https" + self._session.mount(self._scheme, adapter) + if proxies is None: self._proxies = {} else: From 051da0c9d3d5657a98e124efcafbceb19b310318 Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 30 Oct 2017 12:11:18 +0200 Subject: [PATCH 6/9] FAM-1163 escape tag values that ends with backslash --- influxdb/line_protocol.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index c399a1d5..e8816fc0 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -57,6 +57,13 @@ def _escape_tag(tag): ) +def _escape_tag_value(value): + ret = _escape_tag(value) + if ret.endswith('\\'): + ret += ' ' + return ret + + def quote_ident(value): """Indent the quotes.""" return "\"{}\"".format(value @@ -135,7 +142,7 @@ def make_lines(data, precision=None): # tags should be sorted client-side to take load off server for tag_key, tag_value in sorted(iteritems(tags)): key = _escape_tag(tag_key) - value = _escape_tag(tag_value) + value = _escape_tag_value(tag_value) if key != '' and value != '': key_values.append(key + "=" + value) From 4585a13b6ca103b79965d70a50af9ab84c11b8db Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 20 Nov 2017 17:11:13 +0200 Subject: [PATCH 7/9] Add tests for _escape_tag_value func --- influxdb/tests/test_line_protocol.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/influxdb/tests/test_line_protocol.py b/influxdb/tests/test_line_protocol.py index dbee8cda..44b46bc3 100644 --- a/influxdb/tests/test_line_protocol.py +++ b/influxdb/tests/test_line_protocol.py @@ -22,6 +22,7 @@ def test_make_lines(self): "tags": { "empty_tag": "", "none_tag": None, + "backslash_tag": "C:\\", "integer_tag": 2, "string_tag": "hello" }, @@ -39,9 +40,10 @@ def test_make_lines(self): ] } + print(line_protocol.make_lines(data)) self.assertEqual( line_protocol.make_lines(data), - 'test,integer_tag=2,string_tag=hello ' + 'test,backslash_tag=C:\\\\ ,integer_tag=2,string_tag=hello ' 'bool_val=True,float_val=1.1,int_val=1i,string_val="hello!"\n' ) From 56432bc3a42c7d7d304cb9fcfd8d616d1a836124 Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Tue, 21 Nov 2017 11:56:12 +0200 Subject: [PATCH 8/9] Remove print statement --- influxdb/tests/test_line_protocol.py | 1 - 1 file changed, 1 deletion(-) diff --git a/influxdb/tests/test_line_protocol.py b/influxdb/tests/test_line_protocol.py index 44b46bc3..a3d84793 100644 --- a/influxdb/tests/test_line_protocol.py +++ b/influxdb/tests/test_line_protocol.py @@ -40,7 +40,6 @@ def test_make_lines(self): ] } - print(line_protocol.make_lines(data)) self.assertEqual( line_protocol.make_lines(data), 'test,backslash_tag=C:\\\\ ,integer_tag=2,string_tag=hello ' From 2d9da80d3c7afaa7e02d551fd241e75d66c7f302 Mon Sep 17 00:00:00 2001 From: Ivan Kovalkovskyi Date: Mon, 26 Mar 2018 11:17:21 +0300 Subject: [PATCH 9/9] Fix wrong session mount --- influxdb/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/client.py b/influxdb/client.py index e38d4b78..f5c0b55b 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -103,7 +103,7 @@ def __init__(self, if ssl is True: self._scheme = "https" - self._session.mount(self._scheme, adapter) + self._session.mount(self._scheme + '://', adapter) if proxies is None: self._proxies = {}