influxdb2 0.5.2

Influxdb 2 client library for rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Data point building and writing

use snafu::{ensure, Snafu};
use std::{collections::BTreeMap, io};

/// Errors that occur while building `DataPoint`s
#[derive(Debug, Snafu)]
pub enum DataPointError {
    /// Returned when calling `build` on a `DataPointBuilder` that has no
    /// fields.
    #[snafu(display(
        "All `DataPoints` must have at least one field. Builder contains: {:?}",
        data_point_builder
    ))]
    AtLeastOneFieldRequired {
        /// The current state of the `DataPointBuilder`
        data_point_builder: DataPointBuilder,
    },
}

/// Incrementally constructs a `DataPoint`.
///
/// Create this via `DataPoint::builder`.
#[derive(Debug)]
pub struct DataPointBuilder {
    measurement: String,
    // Keeping the tags sorted improves performance on the server side
    tags: BTreeMap<String, String>,
    fields: BTreeMap<String, FieldValue>,
    timestamp: Option<i64>,
}

impl DataPointBuilder {
    fn new(measurement: impl Into<String>) -> Self {
        Self {
            measurement: measurement.into(),
            tags: Default::default(),
            fields: Default::default(),
            timestamp: Default::default(),
        }
    }

    /// Sets a tag, replacing any existing tag of the same name.
    pub fn tag(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.tags.insert(name.into(), value.into());
        self
    }

    /// Sets a field, replacing any existing field of the same name.
    pub fn field(mut self, name: impl Into<String>, value: impl Into<FieldValue>) -> Self {
        self.fields.insert(name.into(), value.into());
        self
    }

    /// Sets the timestamp, replacing any existing timestamp.
    ///
    /// When using write, the value is treated as the number of nanoseconds since the
    /// UNIX epoch.
    ///
    /// When using write_with_precision, the value is interpreted according to the configured precision.
    pub fn timestamp(mut self, value: i64) -> Self {
        self.timestamp = Some(value);
        self
    }

    /// Constructs the data point
    pub fn build(self) -> Result<DataPoint, DataPointError> {
        ensure!(
            !self.fields.is_empty(),
            AtLeastOneFieldRequired {
                data_point_builder: self
            }
        );

        let Self {
            measurement,
            tags,
            fields,
            timestamp,
        } = self;

        Ok(DataPoint {
            measurement,
            tags,
            fields,
            timestamp,
        })
    }
}

/// A single point of information to send to InfluxDB.
// TODO: If we want to support non-UTF-8 data, all `String`s stored in `DataPoint` would need
// to be `Vec<u8>` instead, the API for creating a `DataPoint` would need some more consideration,
// and there would need to be more `Write*` trait implementations. Because the `Write*` traits work
// on a writer of bytes, that part of the design supports non-UTF-8 data now.
#[derive(Clone, Debug)]
pub struct DataPoint {
    measurement: String,
    tags: BTreeMap<String, String>,
    fields: BTreeMap<String, FieldValue>,
    timestamp: Option<i64>,
}

impl DataPoint {
    /// Create a builder to incrementally construct a `DataPoint`.
    pub fn builder(measurement: impl Into<String>) -> DataPointBuilder {
        DataPointBuilder::new(measurement)
    }
}

impl WriteDataPoint for DataPoint {
    fn write_data_point_to<W>(&self, mut w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        self.measurement.write_measurement_to(&mut w)?;

        for (k, v) in &self.tags {
            w.write_all(b",")?;
            k.write_tag_key_to(&mut w)?;
            w.write_all(b"=")?;
            v.write_tag_value_to(&mut w)?;
        }

        for (i, (k, v)) in self.fields.iter().enumerate() {
            let d = if i == 0 { b" " } else { b"," };

            w.write_all(d)?;
            k.write_field_key_to(&mut w)?;
            w.write_all(b"=")?;
            v.write_field_value_to(&mut w)?;
        }

        if let Some(ts) = self.timestamp {
            w.write_all(b" ")?;
            ts.write_timestamp_to(&mut w)?;
        }

        w.write_all(b"\n")?;

        Ok(())
    }
}

/// Possible value types
#[derive(Debug, Clone, PartialEq)]
pub enum FieldValue {
    /// A true or false value
    Bool(bool),
    /// A 64-bit floating point number
    F64(f64),
    /// A 64-bit signed integer number
    I64(i64),
    /// A string value
    String(String),
}

impl From<bool> for FieldValue {
    fn from(other: bool) -> Self {
        Self::Bool(other)
    }
}

impl From<f64> for FieldValue {
    fn from(other: f64) -> Self {
        Self::F64(other)
    }
}

impl From<i64> for FieldValue {
    fn from(other: i64) -> Self {
        Self::I64(other)
    }
}

impl From<&str> for FieldValue {
    fn from(other: &str) -> Self {
        Self::String(other.into())
    }
}

impl From<String> for FieldValue {
    fn from(other: String) -> Self {
        Self::String(other)
    }
}

/// Transform a type into valid line protocol lines
///
/// This trait is to enable the conversion of `DataPoint`s to line protocol; it
/// is unlikely that you would need to implement this trait. In the future, a
/// `derive` crate may exist that would facilitate the generation of
/// implementations of this trait on custom types to help uphold the
/// responsibilities for escaping and producing complete lines.
pub trait WriteDataPoint {
    /// Write this data point as line protocol. The implementor is responsible
    /// for properly escaping the data and ensuring that complete lines
    /// are generated.
    fn write_data_point_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

// The following are traits rather than free functions so that we can limit
// their implementations to only the data types supported for each of
// measurement, tag key, tag value, field key, field value, and timestamp. They
// are a private implementation detail and any custom implementations
// of these traits would be generated by a future derive trait.
trait WriteMeasurement {
    fn write_measurement_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteMeasurement for str {
    fn write_measurement_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        escape_and_write_value(self, MEASUREMENT_DELIMITERS, w)
    }
}

trait WriteTagKey {
    fn write_tag_key_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteTagKey for str {
    fn write_tag_key_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        escape_and_write_value(self, TAG_KEY_DELIMITERS, w)
    }
}

trait WriteTagValue {
    fn write_tag_value_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteTagValue for str {
    fn write_tag_value_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        escape_and_write_value(self, TAG_VALUE_DELIMITERS, w)
    }
}

trait WriteFieldKey {
    fn write_field_key_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteFieldKey for str {
    fn write_field_key_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        escape_and_write_value(self, FIELD_KEY_DELIMITERS, w)
    }
}

trait WriteFieldValue {
    fn write_field_value_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteFieldValue for FieldValue {
    fn write_field_value_to<W>(&self, mut w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        use FieldValue::*;

        match self {
            Bool(v) => write!(w, "{}", if *v { "t" } else { "f" }),
            F64(v) => write!(w, "{}", v),
            I64(v) => write!(w, "{}i", v),
            String(v) => {
                w.write_all(br#"""#)?;
                escape_and_write_value(v, FIELD_VALUE_STRING_DELIMITERS, &mut w)?;
                w.write_all(br#"""#)
            }
        }
    }
}

trait WriteTimestamp {
    fn write_timestamp_to<W>(&self, w: W) -> io::Result<()>
    where
        W: io::Write;
}

impl WriteTimestamp for i64 {
    fn write_timestamp_to<W>(&self, mut w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        write!(w, "{}", self)
    }
}

const MEASUREMENT_DELIMITERS: &[char] = &[',', ' '];
const TAG_KEY_DELIMITERS: &[char] = &[',', '=', ' '];
const TAG_VALUE_DELIMITERS: &[char] = TAG_KEY_DELIMITERS;
const FIELD_KEY_DELIMITERS: &[char] = TAG_KEY_DELIMITERS;
const FIELD_VALUE_STRING_DELIMITERS: &[char] = &['"'];

fn escape_and_write_value<W>(
    value: &str,
    escaping_specification: &[char],
    mut w: W,
) -> io::Result<()>
where
    W: io::Write,
{
    let mut last = 0;

    for (idx, delim) in value.match_indices(escaping_specification) {
        let s = &value[last..idx];
        write!(w, r#"{}\{}"#, s, delim)?;
        last = idx + delim.len();
    }

    w.write_all(value[last..].as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::str;

    fn assert_utf8_strings_eq(left: &[u8], right: &[u8]) {
        assert_eq!(
            left,
            right,
            "\n\nleft string value:  `{}`,\nright string value: `{}`",
            str::from_utf8(left).unwrap(),
            str::from_utf8(right).unwrap(),
        );
    }

    #[test]
    fn point_builder_allows_setting_tags_and_fields() {
        let point = DataPoint::builder("swap")
            .tag("host", "server01")
            .tag("name", "disk0")
            .field("in", 3_i64)
            .field("out", 4_i64)
            .timestamp(1)
            .build()
            .unwrap();

        assert_utf8_strings_eq(
            &point.data_point_to_vec().unwrap(),
            b"swap,host=server01,name=disk0 in=3i,out=4i 1\n".as_ref(),
        );
    }

    #[test]
    fn no_tags_or_timestamp() {
        let point = DataPoint::builder("m0")
            .field("f0", 1.0)
            .field("f1", 2_i64)
            .build()
            .unwrap();

        assert_utf8_strings_eq(
            &point.data_point_to_vec().unwrap(),
            b"m0 f0=1,f1=2i\n".as_ref(),
        );
    }

    #[test]
    fn no_timestamp() {
        let point = DataPoint::builder("m0")
            .tag("t0", "v0")
            .tag("t1", "v1")
            .field("f1", 2_i64)
            .build()
            .unwrap();

        assert_utf8_strings_eq(
            &point.data_point_to_vec().unwrap(),
            b"m0,t0=v0,t1=v1 f1=2i\n".as_ref(),
        );
    }

    #[test]
    fn no_field() {
        let point_result = DataPoint::builder("m0").build();

        assert!(point_result.is_err());
    }

    const ALL_THE_DELIMITERS: &str = r#"alpha,beta=delta gamma"epsilon"#;

    #[test]
    fn special_characters_are_escaped_in_measurements() {
        assert_utf8_strings_eq(
            &ALL_THE_DELIMITERS.measurement_to_vec().unwrap(),
            br#"alpha\,beta=delta\ gamma"epsilon"#.as_ref(),
        );
    }

    #[test]
    fn special_characters_are_escaped_in_tag_keys() {
        assert_utf8_strings_eq(
            &ALL_THE_DELIMITERS.tag_key_to_vec().unwrap(),
            br#"alpha\,beta\=delta\ gamma"epsilon"#.as_ref(),
        );
    }

    #[test]
    fn special_characters_are_escaped_in_tag_values() {
        assert_utf8_strings_eq(
            &ALL_THE_DELIMITERS.tag_value_to_vec().unwrap(),
            br#"alpha\,beta\=delta\ gamma"epsilon"#.as_ref(),
        );
    }

    #[test]
    fn special_characters_are_escaped_in_field_keys() {
        assert_utf8_strings_eq(
            &ALL_THE_DELIMITERS.field_key_to_vec().unwrap(),
            br#"alpha\,beta\=delta\ gamma"epsilon"#.as_ref(),
        );
    }

    #[test]
    fn special_characters_are_escaped_in_field_values_of_strings() {
        assert_utf8_strings_eq(
            &FieldValue::from(ALL_THE_DELIMITERS)
                .field_value_to_vec()
                .unwrap(),
            br#""alpha,beta=delta gamma\"epsilon""#.as_ref(),
        );
    }

    #[test]
    fn field_value_of_bool() {
        let e = FieldValue::from(true);
        assert_utf8_strings_eq(&e.field_value_to_vec().unwrap(), b"t");

        let e = FieldValue::from(false);
        assert_utf8_strings_eq(&e.field_value_to_vec().unwrap(), b"f");
    }

    #[test]
    fn field_value_of_float() {
        let e = FieldValue::from(42_f64);
        assert_utf8_strings_eq(&e.field_value_to_vec().unwrap(), b"42");
    }

    #[test]
    fn field_value_of_integer() {
        let e = FieldValue::from(42_i64);
        assert_utf8_strings_eq(&e.field_value_to_vec().unwrap(), b"42i");
    }

    #[test]
    fn field_value_of_string() {
        let e = FieldValue::from("hello");
        assert_utf8_strings_eq(&e.field_value_to_vec().unwrap(), br#""hello""#);
    }

    // Clears up the boilerplate of writing to a vector from the tests
    macro_rules! test_extension_traits {
        ($($ext_name:ident :: $ext_fn_name:ident -> $base_name:ident :: $base_fn_name:ident,)*) => {
            $(
                trait $ext_name: $base_name {
                    fn $ext_fn_name(&self) -> io::Result<Vec<u8>> {
                        let mut v = Vec::new();
                        self.$base_fn_name(&mut v)?;
                        Ok(v)
                    }
                }
                impl<T: $base_name + ?Sized> $ext_name for T {}
            )*
        }
    }

    test_extension_traits! {
        WriteDataPointExt::data_point_to_vec -> WriteDataPoint::write_data_point_to,
        WriteMeasurementExt::measurement_to_vec -> WriteMeasurement::write_measurement_to,
        WriteTagKeyExt::tag_key_to_vec -> WriteTagKey::write_tag_key_to,
        WriteTagValueExt::tag_value_to_vec -> WriteTagValue::write_tag_value_to,
        WriteFieldKeyExt::field_key_to_vec -> WriteFieldKey::write_field_key_to,
        WriteFieldValueExt::field_value_to_vec -> WriteFieldValue::write_field_value_to,
    }
}