datalogd.plugins.influxdb2_datasink module

class datalogd.plugins.influxdb2_datasink.InfluxDB2DataSink(url='http://localhost:8086', token='', org='default', bucket='default', measurement='measurement', run_id=None, field_key=None, field_value=None)[source]

Bases: datalogd.DataSink

Connection to a InfluxDB 2.x (or 1.8+) database for storing time-series data.

Note that this doesn’t actually run the InfluxDB database service, but simply connects to an existing InfluxDB database via a network (or localhost) connection. See the getting started documentation for details on configuring a new database server.

The url parameter should be a string specifying the protocol, server ip or name, and port. For example, url="http://localhost:8086".

The authentication token parameter needs to be specified to allow commits to the database. See the token documentation to see how to create and obtain tokens.

Parameters for org, bucket must correspond to a valid organisation and bucket created in the database for which the authentication token is valid. See the documentation for organisations and buckets for details.

The measurement parameter specifies the data point measurement (or “table”) the data will be entered into, and does not need to already exist. See the documentation on data elements for details.

A run_id parameter may be passed which will be added as a tag to the data points. It may be used to identify data obtained from this particular run of the data logging session. If no value is provided, a value will be generated from a YYYYMMDD-HHMMSS formatted time stamp.

The data point field key will be attempted to be determined automatically from the incoming data dictionaries. If the data dictionary contains a name or label key, then its value will be used as the database point field key. Alternatively, a field key will be generated from the values of type and id if present. Finally, a default field key of data will be used. To instead specify the data entry which should provide the field key, specify it as the field_key parameter. If the field is specified by a parameter or taken from a name or label, then those will not also be included in the entry’s database keys. However, if the field name is automatically built from type and id values, these will still be part of the entries keys.

Similarly, the data point field value will use the value from the incoming data dictionary’s value field if present. To instead specify the data entry which should provide the field value, specify it as the field_value parameter. The value won’t also appear in the database entry’s keys.

Parameters:
  • url – Protocol, host name or IP address, and port number of InfluxDB server.
  • token – API token used to authenticate with the InfluxDB server.
  • org – Name of InfluxDB organisation in which to store data.
  • bucket – Name of InfluxDB bucket in which to store data.
  • measurement – Name for the InfluxDB measurement session.
  • run_id – A tag to identify commits from this run.
  • field_key – A field from the incoming data used to determine the data point field key.
  • field_value – A field from the incoming data used to determine the data point field value.
close()[source]

Close the connection to the database.

receive(data)[source]

Commit data to the InfluxDB database.

Multiple items of data can be submitted at once if data is a list. A typical format of data would be:

[
    {'type': 'temperature', 'id': '0', 'value': 22.35},
    {'type': 'humidity', 'id': '0', 'value': 55.0},
    {'type': 'temperature', 'id': '1', 'value': 25.80},
]

In the above case (assuming the field_key and field_value parameters were not supplied when initialising the plugin), the InfluxDB data point field would be generated as <type>_<id> = <value>, and only the global run_id parameter would be entered into the data point keys.

If a name or label field is present, then it will instead be used as the InfluxDB data point field key. For example:

[
    {'name': 'Temperature', 'type': 'temperature', 'id': '0', 'value': 22.35},
    {'name': 'Humidity', 'type': 'humidity', 'id': '0', 'value': 55.0},
]

In this case, the InfluxDB data point field would be generated as <name> = <value>, and the remaining fields (type and id) would be added as data point field keys, along with the run_id.

A timestamp for the commit will be generated using the current system clock if a “timestamp” field does not already exist.

Parameters:data – Data to commit to the database.