Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Add support for optional metric values via Option<_> #31975

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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