Skip to content

Commit

Permalink
Add support for optional metric values via Option<_> (#31975)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiseda authored Jun 5, 2023
1 parent 8fbada4 commit 6371240
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions metrics/src/datapoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ macro_rules! create_datapoint {

(@fields $point:ident) => {};

// process optional fields
(@fields $point:ident ($name:expr, $value:expr, Option<$type:ident>) , $($rest:tt)*) => {
if let Some(value) = $value {
$crate::create_datapoint!(@field $point $name, value, $type);
}
$crate::create_datapoint!(@fields $point $($rest)*);
};
(@fields $point:ident ($name:expr, $value:expr, Option<$type:ident>) $(,)?) => {
if let Some(value) = $value {
$crate::create_datapoint!(@field $point $name, value, $type);
}
};

// process tags
(@fields $point:ident $tag_name:expr => $tag_value:expr, $($rest:tt)*) => {
$crate::create_datapoint!(@tag $point $tag_name, $tag_value);
Expand Down Expand Up @@ -261,6 +274,42 @@ mod test {
assert_eq!(point.fields[3], ("bool", "true".to_string()));
}

#[test]
fn test_optional_datapoint() {
datapoint_debug!("name", ("field name", Some("test"), Option<String>));
datapoint_info!("name", ("field name", Some(12.34_f64), Option<f64>));
datapoint_trace!("name", ("field name", Some(true), Option<bool>));
datapoint_warn!("name", ("field name", Some(1), Option<i64>));
datapoint_error!("name", ("field name", Some(1), Option<i64>),);
datapoint_debug!("name", ("field name", None::<String>, Option<String>));
datapoint_info!("name", ("field name", None::<f64>, Option<f64>));
datapoint_trace!("name", ("field name", None::<bool>, Option<bool>));
datapoint_warn!("name", ("field name", None::<i64>, Option<i64>));
datapoint_error!("name", ("field name", None::<i64>, Option<i64>),);

let point = create_datapoint!(
@point "name",
("some_i64", Some(1), Option<i64>),
("no_i64", None::<i64>, Option<i64>),
("some_String", Some("string space string"), Option<String>),
("no_String", None::<String>, Option<String>),
("some_f64", Some(12.34_f64), Option<f64>),
("no_f64", None::<f64>, Option<f64>),
("some_bool", Some(true), Option<bool>),
("no_bool", None::<bool>, Option<bool>),
);
assert_eq!(point.name, "name");
assert_eq!(point.tags.len(), 0);
assert_eq!(point.fields[0], ("some_i64", "1i".to_string()));
assert_eq!(
point.fields[1],
("some_String", "\"string space string\"".to_string())
);
assert_eq!(point.fields[2], ("some_f64", "12.34".to_string()));
assert_eq!(point.fields[3], ("some_bool", "true".to_string()));
assert_eq!(point.fields.len(), 4);
}

#[test]
fn test_datapoint_with_tags() {
datapoint_debug!("name", "tag" => "tag-value", ("field name", "test", String));
Expand Down

0 comments on commit 6371240

Please sign in to comment.