Skip to main content

LineProtocol

Struct LineProtocol 

Source
pub struct LineProtocol {
    pub measurement: Measurement,
    pub tags: Option<HashMap<TagKey, TagValue>>,
    pub fields: HashMap<FieldKey, FieldValue>,
    pub timestamp: Option<i64>,
}

Fields§

§measurement: Measurement

The data point measurement name

§tags: Option<HashMap<TagKey, TagValue>>

The data point tag set

§fields: HashMap<FieldKey, FieldValue>

The data point field set

§timestamp: Option<i64>

To ensure a data point includes the time a metric is observed (not received by InfluxDB), include a timestamp if not defined

By default the timestamp is defined in nanoseconds. If you are using any other form of precision it needs to be defined when making the insert request

Implementations§

Source§

impl LineProtocol

Source

pub fn new<T>(measurement: T) -> Self
where T: Into<Measurement>,

Create a new LineProtocol for building a single data point

§Args
  • measurement - A valid measurement name
Source

pub fn measurement<T>(self, measurement: T) -> Self
where T: Into<Measurement>,

Overwrite the measurement name with a new name

§Example
let mut line_protocol = LineProtocol::new("measurement").add_field("key", "value");

line_protocol = line_protocol.measurement("new_measurement");
§Args
  • measurement - A valid measurement name
Source

pub fn measurement_ref<T>(&mut self, measurement: T)
where T: Into<Measurement>,

Overwrite the measurement name with a new name

§Example
let mut line_protocol = LineProtocol::new("measurement").add_field("key", "value");

line_protocol.measurement_ref("new_measurement");
§Args
  • measurement - A valid measurement name
Source

pub fn add_tag<K, V>(self, key: K, value: V) -> Self
where K: Into<TagKey>, V: Into<TagValue>,

Add or update a tag key-value pair to the data point

This function is useful if you want to follow a builder pattern

§Example
let line_protocol = LineProtocol::new("measurement").add_tag("key", "value");
§Args
Source

pub fn add_tag_ref<K, V>(&mut self, key: K, value: V)
where K: Into<TagKey>, V: Into<TagValue>,

Add or update a tag key-value pair to the data point

This function is useful if you want to build a data point dynamically

§Example
let line_protocol = LineProtocol::new("measurement");

for (key, value) in tags {
    line_protocol.add_tag_ref(key, value);
}
§Args
Source

pub fn delete_tag<K>(self, key: K) -> Self
where K: Into<TagKey>,

Delete a tag from the data point

§Args
Source

pub fn delete_tag_ref<K>(&mut self, key: K)
where K: Into<TagKey>,

Delete a tag from the data point

§Args
Source

pub fn add_field<K, V>(self, key: K, value: V) -> Self
where K: Into<FieldKey>, V: Into<FieldValue>,

Add or update a field key-value pair to the data point

This function is useful if you want to follow a builder pattern

§Example
let line_protocol = LineProtocol::new("measurement").add_field("key", "value");
§Args
  • key - A valid field key
  • value - A valid field value
Source

pub fn add_field_ref<K, V>(&mut self, key: K, value: V)
where K: Into<FieldKey>, V: Into<FieldValue>,

Add or update a field key-value pair to the data point

This function is useful if you want to build a data point dynamically

§Example
let line_protocol = LineProtocol::new("measurement");

for (key, value) in fields {
    line_protocol.add_field_ref(key, value);
}
§Args
  • key - A valid field key
  • value - A valid field value
Source

pub fn delete_field<K>(self, key: K) -> Self
where K: Into<FieldKey>,

Delete a field from the data point

§Args
Source

pub fn delete_field_ref<K>(&mut self, key: K)
where K: Into<FieldKey>,

Delete a field from the data point

§Args
Source

pub fn with_timestamp<T>(self, timestamp: T) -> Self
where T: Into<i64>,

Set the timestamp for the data point

It is recommend to set a timestamp. By default InfluxDB v2 expects the timestamp to be in nanosecond precision. If you are using any other form of precision it needs to be explicitly set when making the query

§Example
let line_protocol = LineProtocol::new("measurement");
    .with_timestamp(1729270461612452700i64);
§Args
  • timestamp - A unix timestamp
Source

pub fn with_timestamp_ref<T>(&mut self, timestamp: T)
where T: Into<i64>,

Set the timestamp for the data point

It is recommend to set a timestamp. By default InfluxDB v2 expects the timestamp to be in nanosecond precision. If you are using any other form of precision it needs to be explicitly set when making the query

§Example
let line_protocol = LineProtocol::new("measurement");
line_protocol.with_timestamp_ref(1729270461612452700i64);
§Args
  • timestamp - A unix timestamp
Source

pub fn delete_timestamp(self) -> Self

Delete the set timestamp

§Example
let mut line_protocol = LineProtocol::new("measurement")
    .add_field("key", "value")
    .with_timestamp_ref(1729270461612452700i64);

line_protocol = line_protocol.delete_timestamp();
Source

pub fn delete_timestamp_ref(&mut self)

Delete the set timestamp

§Example
let mut line_protocol = LineProtocol::new("measurement")
    .add_field("key", "value")
    .with_timestamp_ref(1729270461612452700i64);

line_protocol.delete_timestamp();
Source

pub fn build(&self) -> Result<String, LineProtocolError>

Builds an InfluxDB v2 data point using the previously defined measurement name, optional tags, fields, and an optional timestamp

In addition validation checks are performed on the individual parts

Source§

impl LineProtocol

Source

pub fn parse_line(line: &str) -> Result<Self, LineProtocolError>

Parse a single line protocol line into the LineProtocol struct

Allows for modifying the line protocol by adding or removing fields/tags and rebuilding

§Example
let line = "measurement,tag=value field=true 1729270461612452700"
let parsed_line = LineProtocol::parse_line(line).unwrap();

parsed_line.delete_tag("tag");
parsed_line.add_field("field2", "hello");
parsed_line.with_timestamp(1729270461612452800i64)

let line = parsed_line.build().unwrap();
// Output: measurement field=true,field2="hello" 1729270461612452800
§Args
  • line - A InfluxDB line protocol line
Source

pub fn parse_vec(lines: Vec<&str>) -> Result<Vec<Self>, LineProtocolError>

Parse a vector of lines

Empty lines and comment lines are silently ignored

§Example
let lines = vec![
    "measurement,tag=value field=\"value\"",
    "measurement,tag=value field=true 1729270461612452700",
    ...
];

let parsed = LineProtocol::parse_vec(lines).unwrap();
§Args
  • lines - An array of InfluxDB line protocol lines
Source

pub fn parse_lines(lines: &str) -> Result<Vec<Self>, LineProtocolError>

Parse multiple lines seprated by a newline (\n)

Empty lines and comment lines are silently ignored

§Example
let lines = vec![
    "measurement,tag=value field=\"value\"",
    "measurement,tag=value field=true 1729270461612452700",
    ...
];

let parsed = LineProtocol::parse_lines(lines.join("\n")).unwrap();
§Args
  • lines - Multiple InfluxDB line protocol lines seperated by a newline
Source§

impl LineProtocol

Source

pub fn get_measurement(&self) -> Measurement

Get a cloned version of the measurement

Source

pub fn get_measurement_ref(&self) -> &Measurement

Get a reference of the measurement

Source

pub fn get_measurement_mut(&mut self) -> &mut Measurement

Get a mutable reference of the measurement

Source

pub fn get_tag<K>(&self, key: K) -> Option<TagValue>
where K: Into<TagKey>,

Get the tag value associated with the provided tag key

§Args
Source

pub fn get_tag_ref<K>(&self, key: K) -> Option<&TagValue>
where K: Into<TagKey>,

Get a reference to the tag value associated with the provided tag key

§Args
Source

pub fn get_tag_mut<K>(&mut self, key: K) -> Option<&mut TagValue>
where K: Into<TagKey>,

Get a mutable reference to the tag value associated with the provided tag key

§Args
Source

pub fn get_field<K>(&self, key: K) -> Option<FieldValue>
where K: Into<FieldKey>,

Get the field value associated with the provided field key

§Args
  • key - A valid field key
Source

pub fn get_field_ref<K>(&self, key: K) -> Option<&FieldValue>
where K: Into<FieldKey>,

Get a reference to the field value associated with the provided field key

§Args
  • key - A valid field key
Source

pub fn get_field_mut<K>(&mut self, key: K) -> Option<&mut FieldValue>
where K: Into<FieldKey>,

Get a mutable reference to the field value associated with the provided field key

§Args
  • key - A valid field key
Source

pub fn get_timestamp(&self) -> Option<i64>

Get a cloned version of the timestamp

Source

pub fn get_timestamp_ref(&self) -> Option<&i64>

Get a reference of the timestamp

Source

pub fn get_timestamp_mut(&mut self) -> Option<&mut i64>

Get a mutable reference of the timestamp

Trait Implementations§

Source§

impl Clone for LineProtocol

Source§

fn clone(&self) -> LineProtocol

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LineProtocol

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for LineProtocol

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for LineProtocol

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.