From 8fc60e9ca85cd9c8f87663826cee533479bfcb67 Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Fri, 23 Dec 2022 17:22:52 +0530 Subject: [PATCH] [feature] Allowed specifying additional options for InfluxDB backend #458 - Additional options can be specified to the TIMESERIES_DATABASE setting which are passed to the underlying backend library. This allows using UDP for writing to InfluxDB. - "openwisp_monitoring.db.backends.influxdb.client.DatabaseClient.write" will use "InfluxDBClient.write_points" instead of "InfluxDBClient.write" because the former allows writing data using the UDP protocol. Closes #458 --- README.rst | 7 +++++++ docker-compose.yml | 2 ++ .../db/backends/influxdb/client.py | 1 + tests/influxdb.conf | 15 +++++++++++++++ tests/openwisp2/settings.py | 8 ++++++++ 5 files changed, 33 insertions(+) create mode 100644 tests/influxdb.conf diff --git a/README.rst b/README.rst index 97148a553..3aee706d3 100644 --- a/README.rst +++ b/README.rst @@ -333,6 +333,13 @@ Follow the setup instructions of `openwisp-controller 'NAME': 'openwisp2', 'HOST': 'localhost', 'PORT': '8086', + 'OPTIONS': { + # Specify additional options to be used while initializing + # database connection. + # Note: These options may differ based on the backend used. + 'use_udp': True, + 'udp_port': 8088, + } } ``urls.py``: diff --git a/docker-compose.yml b/docker-compose.yml index 6386355ed..c6add592b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,8 +16,10 @@ services: image: influxdb:1.8-alpine volumes: - influxdb-data:/var/lib/influxdb + - ./tests/influxdb.conf:/etc/influxdb/influxdb.conf ports: - "8086:8086" + - "8088:8088/udp" environment: INFLUXDB_DB: openwisp2 INFLUXDB_USER: openwisp diff --git a/openwisp_monitoring/db/backends/influxdb/client.py b/openwisp_monitoring/db/backends/influxdb/client.py index 1d9a47e83..0b60cabb7 100644 --- a/openwisp_monitoring/db/backends/influxdb/client.py +++ b/openwisp_monitoring/db/backends/influxdb/client.py @@ -81,6 +81,7 @@ def db(self): TIMESERIES_DB['USER'], TIMESERIES_DB['PASSWORD'], self.db_name, + **TIMESERIES_DB.get('OPTIONS', {}), ) @retry diff --git a/tests/influxdb.conf b/tests/influxdb.conf new file mode 100644 index 000000000..8d323d99e --- /dev/null +++ b/tests/influxdb.conf @@ -0,0 +1,15 @@ +[meta] + dir = "/var/lib/influxdb/meta" + +[data] + dir = "/var/lib/influxdb/data" + engine = "tsm1" + wal-dir = "/var/lib/influxdb/wal" + +[[udp]] + enabled = true + bind-address = ":8088" + database = "openwisp2" + batch-size = 0c + batch-pending = 0 + batch-timeout = "0s" diff --git a/tests/openwisp2/settings.py b/tests/openwisp2/settings.py index fe18f388b..653ae2f54 100644 --- a/tests/openwisp2/settings.py +++ b/tests/openwisp2/settings.py @@ -28,7 +28,15 @@ 'NAME': 'openwisp2', 'HOST': os.getenv('INFLUXDB_HOST', 'localhost'), 'PORT': '8086', + 'OPTIONS': {'use_udp': True, 'udp_port': 8088}, } +if TESTING: + # Some automated tests queries InfluxDB just after + # writing the data. Occasionally, the read operation + # is performed before InfluxDB has processed the UDP + # packet which leads to failing test cases. Therefore, + # we always run test suite without UDP support. + TIMESERIES_DATABASE['OPTIONS']['use_udp'] = False SECRET_KEY = 'fn)t*+$)ugeyip6-#txyy$5wf2ervc0d2n#h)qb)y5@ly$t*@w'