influxlp-tools 0.1.0

Influx Line Protocol Tools is a simple parser and builder for InfluxDB v2 line protocol
Documentation
influxlp-tools-0.1.0 has been yanked.

InfluxDB V2 Line Protocol Tools

Influx line protocol tools on crates.io


[dependencies]

influxlp-tools = "1.0"

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=value field=value 1729270461612452700

Read more about it here!

Usage

Builder

The builder allows you to create valid line protocol strings

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

// Output: measurement,tag=value field="value"
let line_protocol = LineProtocol::new("measurement")
    .add_field("field","{\"test\": \"hello\"}")
    .build()
    .unwrap();

// measurement field="{\"test\": \"hello\"}"
let line_protocol = LineProtocol::new("measurement")
    .add_tag("tag", "value")
    .add_tag("tag2", "value")
    .add_field("field", "value")
    .add_field("field2", "{\"test\": \"hello\"}")
    .with_timestamp(1729270461612452700i64)
    .build()
    .unwrap();

// Output: measurement,tag2=value,tag=value field="value",field2="{\"test\": \"hello\"}" 1729270461612452700

Parser

The parser allows you to parse valid line protocol strings to the LineProtocol struct

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

// Output: LineProtocol {
//     measurement: Measurement(
//         "measurement",
//     ),
//     tags: Some(
//         {
//             TagKey(
//                 "tag2",
//             ): TagValue(
//                 "value",
//             ),
//             TagKey(
//                 "tag",
//             ): TagValue(
//                 "value",
//             ),
//         },
//     ),
//     fields: {
//         FieldKey(
//             "field2",
//         ): String(
//             "{\"test\": \"hello\"}",
//         ),
//         FieldKey(
//             "field",
//         ): String(
//             "value",
//         ),
//     },
//     timestamp: Some(
//         1729270461612452700,
//     ),
// }

You can also parse multiple lines

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);

// Output: [
//     LineProtocol {
//         measurement: Measurement(
//             "measurement",
//         ),
//         tags: Some(
//             {
//                 TagKey(
//                     "tag",
//                 ): TagValue(
//                     "value",
//                 ),
//             },
//         ),
//         fields: {
//             FieldKey(
//                 "field",
//             ): String(
//                 "value",
//             ),
//         },
//         timestamp: None,
//     },
//     LineProtocol {
//         measurement: Measurement(
//             "measurement",
//         ),
//         tags: None,
//         fields: {
//             FieldKey(
//                 "field",
//             ): String(
//                 "{\"test\": \"hello\"}",
//             ),
//         },
//         timestamp: None,
//     },
//     LineProtocol {
//         measurement: Measurement(
//             "measurement",
//         ),
//         tags: Some(
//             {
//                 TagKey(
//                     "tag2",
//                 ): TagValue(
//                     "value",
//                 ),
//                 TagKey(
//                     "tag",
//                 ): TagValue(
//                     "value",
//                 ),
//             },
//         ),
//         fields: {
//             FieldKey(
//                 "field2",
//             ): String(
//                 "{\"test\": \"hello\"}",
//             ),
//             FieldKey(
//                 "field",
//             ): String(
//                 "value",
//             ),
//         },
//         timestamp: Some(
//             1729270461612452700,
//         ),
//     },
// ]