Browse Source

adds v, utc shortcut syntax to measure!

- `v`: adds a tag "version" based on the expression, e.g. `v(VERSION)`
-> `t("version", VERSION)`
- `utc`: like `tm`, but converts a `DateTime<Utc>` to `i64`
automatically
master
Jonathan Strong 6 years ago
parent
commit
d39902c494
2 changed files with 27 additions and 1 deletions
  1. +1
    -1
      Cargo.toml
  2. +26
    -0
      src/influx.rs

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package] [package]
name = "logging" name = "logging"
version = "0.4.3"
version = "0.4.4"
authors = ["Jonathan Strong <jstrong@legis.io>"] authors = ["Jonathan Strong <jstrong@legis.io>"]


[[example]] [[example]]


+ 26
- 0
src/influx.rs View File

@@ -143,6 +143,8 @@ macro_rules! measure {
(@kv $t:tt, $meas:ident, $k:tt, $v:expr) => { measure!(@ea $t, $meas, stringify!($k), $v) }; (@kv $t:tt, $meas:ident, $k:tt, $v:expr) => { measure!(@ea $t, $meas, stringify!($k), $v) };
(@kv time, $meas:ident, $tm:expr) => { $meas = $meas.set_timestamp(AsI64::as_i64($tm)) }; (@kv time, $meas:ident, $tm:expr) => { $meas = $meas.set_timestamp(AsI64::as_i64($tm)) };
(@kv tm, $meas:ident, $tm:expr) => { $meas = $meas.set_timestamp(AsI64::as_i64($tm)) }; (@kv tm, $meas:ident, $tm:expr) => { $meas = $meas.set_timestamp(AsI64::as_i64($tm)) };
(@kv utc, $meas:ident, $tm:expr) => { $meas = $meas.set_timestamp(AsI64::as_i64($crate::nanos($tm))) };
(@kv v, $meas:ident, $k:expr) => { measure!(@ea tag, $meas, "version", $k) };
(@kv $t:tt, $meas:ident, $k:tt) => { measure!(@ea $t, $meas, stringify!($k), measure!(@as_expr $k)) }; (@kv $t:tt, $meas:ident, $k:tt) => { measure!(@ea $t, $meas, stringify!($k), measure!(@as_expr $k)) };
(@ea tag, $meas:ident, $k:expr, $v:expr) => { $meas = $meas.add_tag($k, $v); }; (@ea tag, $meas:ident, $k:expr, $v:expr) => { $meas = $meas.add_tag($k, $v); };
(@ea t, $meas:ident, $k:expr, $v:expr) => { $meas = $meas.add_tag($k, $v); }; (@ea t, $meas:ident, $k:expr, $v:expr) => { $meas = $meas.add_tag($k, $v); };
@@ -701,6 +703,30 @@ mod tests {
use super::*; use super::*;
use test::{black_box, Bencher}; use test::{black_box, Bencher};


#[test]
fn it_uses_the_utc_shortcut_to_convert_a_datetime_utc() {
const VERSION: &str = "0.3.90";
let tag_value = "one";
let color = "red";
let time = Utc::now();
let m = measure!(@make_meas test, i(n, 1), t(color), v(VERSION), utc(time));
assert_eq!(m.get_tag("color"), Some("red"));
assert_eq!(m.get_tag("version"), Some(VERSION));
assert_eq!(m.timestamp, Some(nanos(time) as i64));
}

#[test]
fn it_uses_the_v_for_version_shortcut() {
const VERSION: &str = "0.3.90";
let tag_value = "one";
let color = "red";
let time = now();
let m = measure!(@make_meas test, i(n, 1), t(color), v(VERSION), tm(time));
assert_eq!(m.get_tag("color"), Some("red"));
assert_eq!(m.get_tag("version"), Some(VERSION));
assert_eq!(m.timestamp, Some(time));
}

#[test] #[test]
fn it_uses_the_new_tag_k_only_shortcut() { fn it_uses_the_new_tag_k_only_shortcut() {
let tag_value = "one"; let tag_value = "one";


Loading…
Cancel
Save