#influx-db #builder #parser #builder-pattern #modify #line-protocol #influxdbv2

influxlp-tools

Influx Line Protocol Tools is a simple parser and builder for InfluxDB v2 line protocol

6 releases

0.2.3 Oct 21, 2024
0.2.2 Oct 19, 2024
0.1.3 Oct 19, 2024

#417 in Parser implementations

Download history 244/week @ 2024-10-13 347/week @ 2024-10-20 27/week @ 2024-10-27 62/week @ 2024-11-03

680 downloads per month

MIT/Apache

67KB
1K SLoC

InfluxDB V2 Line Protocol Tools

Build and release workflow Influx line protocol tools on crates.io


InfluxDB V2 Line Protocol Tools is a parsing and building library for InfluxDB v2's line protocol. It provides easy-to-use functionality with built-in validation, support for a builder pattern and dynamic population, and options for modifying existing line protocols


InfluxDB's line protocol is a text-based format used to represent data points. It includes the measurement, tag set, field set, and an optional timestamp.

measurement         tag set             field set              timestamp
----------- ------------------- ------------------------- -------------------
measurement,tag1=val1,tag2=val2 field1="val1",field2=true 1729270461612452700

Read more about it here!

Documenation

See docs.rs for more information

Usage

Building a line protocol string

At minimum the measurement name and a field is required to build a valid line protocol string

let line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .build()
    .unwrap();

You can overwrite the measurement name by calling the measurement method

let mut line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .build()
    .unwrap();

line_protocol = line_protocol.measurement("new_measurement");

Multiple fields can be added by calling the add_field method multiple times

let line_protocol = LineProtocol::new("measurement")
    .add_field("field1", "value")
    .add_field("field2", "value")
    .build()
    .unwrap();

Optionally tags can be added. More tags can be added as with fields

let line_protocol = LineProtocol::new("measurement")
    .add_tag("tag1", "value")
    .add_tag("tag2", "value")
    .add_field("field", "value")
    .build()
    .unwrap();

A timestamp can be added with the with_timestamp method. By default the timestamp is defined in nanosecond precision. If you are using any other precision, e.g., seconds, it needs be defined when querying influx

let line_protocol = LineProtocol::new("measurement")
    .add_field("field", "value")
    .with_timestamp(1729270461612452700i64)
    .build()
    .unwrap();

A field, tag, and timestamp can be deleted if needed. This is done by calling the respective delete function

let mut line_protocol = LineProtocol::new("measurement")
    .add_tag("tag", "value")
    .add_field("field", "value");

line_protocol.delete_tag("tag")

Note: that deleting all fields will cause the building to fail as atleast one field is required

Parsing a line protocol string

To parse a line protocol string the parse_line method can be used

let line = "measurement,tag2=value,tag=value field=\"hello\",field2=\"world\" 1729270461612452700";
let line_protocol = LineProtocol::parse_line(line).unwrap();

To parse multiple lines seperated by a newline the parse_lines method can be used instead

let lines = vec![
    "measurement,tag=value field=\"value\"",
    "measurement field=\"{\\\"test\\\": \\\"hello\\\"}\"",
    "measurement,tag2=value,tag=value field=\"value\",field2=\"{\\\"test\\\": \
            \\\"hello\\\"}\" 1729270461612452700"
].join("\n");

let result = LineProtocol::parse_lines(&lines);

Note: The parsed line can be modified and rebuilt if needed

Issues or new features

If you discover any issues to be fixed or features you'd like to be introduced you can open up a issue and I'll take a look at it whenever I have time. I am going to be maintaing this crate on and off depending on how much time I have.

License

This project is licensed under either of

at your option.

Dependencies

~2.6–4MB
~73K SLoC