From 6013becaabc5dc175f01923a1587f79992d0683c Mon Sep 17 00:00:00 2001 From: Lalit Date: Sat, 11 May 2024 12:30:35 -0700 Subject: [PATCH 1/3] make add_attributes interface itrable --- opentelemetry-sdk/src/logs/mod.rs | 78 ++++++++++++++++++++++++++-- opentelemetry-sdk/src/logs/record.rs | 11 +++- opentelemetry/src/logs/noop.rs | 8 ++- opentelemetry/src/logs/record.rs | 6 ++- 4 files changed, 96 insertions(+), 7 deletions(-) diff --git a/opentelemetry-sdk/src/logs/mod.rs b/opentelemetry-sdk/src/logs/mod.rs index 22157b7a59..1cf58fc4aa 100644 --- a/opentelemetry-sdk/src/logs/mod.rs +++ b/opentelemetry-sdk/src/logs/mod.rs @@ -20,6 +20,8 @@ mod tests { use opentelemetry::logs::LogRecord; use opentelemetry::logs::{Logger, LoggerProvider as _, Severity}; use opentelemetry::{logs::AnyValue, Key, KeyValue}; + use std::collections::HashMap; + #[test] fn logging_sdk_test() { @@ -34,10 +36,32 @@ mod tests { let mut log_record = logger.create_log_record(); log_record.set_severity_number(Severity::Error); log_record.set_severity_text("Error".into()); + + // Adding attributes using a vector with explicitly constructed Key and AnyValue objects. log_record.add_attributes(vec![ - (Key::new("key1"), "value1".into()), - (Key::new("key2"), "value2".into()), + (Key::new("key1"), AnyValue::from("value1")), + (Key::new("key2"), AnyValue::from("value2")), ]); + + // Adding attributes using an array with explicitly constructed Key and AnyValue objects. + log_record.add_attributes([ + (Key::new("key3"), AnyValue::from("value3")), + (Key::new("key4"), AnyValue::from("value4")), + ]); + + // Adding attributes using a vector with tuple auto-conversion to Key and AnyValue. + log_record.add_attributes(vec![("key5", "value5"), ("key6", "value6")]); + + // Adding attributes using an array with tuple auto-conversion to Key and AnyValue. + log_record.add_attributes([("key7", "value7"), ("key8", "value8")]); + + // Adding Attributes from a HashMap + let mut attributes_map = HashMap::new(); + attributes_map.insert("user_id", "12345"); + attributes_map.insert("session_id", "abcde"); + + log_record.add_attributes(attributes_map); + logger.emit(log_record); // Assert @@ -55,7 +79,55 @@ mod tests { .attributes .clone() .expect("Attributes are expected"); - assert_eq!(attributes.len(), 2); + assert_eq!(attributes.len(), 8); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key1"), AnyValue::String("value1".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key2"), AnyValue::String("value2".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key3"), AnyValue::String("value3".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key4"), AnyValue::String("value4".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key5"), AnyValue::String("value5".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key6"), AnyValue::String("value6".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key5"), AnyValue::String("value7".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key6"), AnyValue::String("value8".into())))); } #[test] diff --git a/opentelemetry-sdk/src/logs/record.rs b/opentelemetry-sdk/src/logs/record.rs index e5048d2c03..5158fde713 100644 --- a/opentelemetry-sdk/src/logs/record.rs +++ b/opentelemetry-sdk/src/logs/record.rs @@ -62,8 +62,15 @@ impl opentelemetry::logs::LogRecord for LogRecord { self.body = Some(body); } - fn add_attributes(&mut self, attributes: Vec<(Key, AnyValue)>) { - self.attributes = Some(attributes); + fn add_attributes(&mut self, attributes: I) + where + I: IntoIterator, + K: Into, + V: Into, + { + for (key, value) in attributes.into_iter() { + self.add_attribute(key, value); + } } fn add_attribute(&mut self, key: K, value: V) diff --git a/opentelemetry/src/logs/noop.rs b/opentelemetry/src/logs/noop.rs index 1308338edb..b2b9e8ebad 100644 --- a/opentelemetry/src/logs/noop.rs +++ b/opentelemetry/src/logs/noop.rs @@ -57,7 +57,13 @@ impl LogRecord for NoopLogRecord { #[inline] fn set_body(&mut self, _body: AnyValue) {} #[inline] - fn add_attributes(&mut self, _attributes: Vec<(Key, AnyValue)>) {} + fn add_attributes(&mut self, _attributes: I) + where + I: IntoIterator, + K: Into, + V: Into, + { + } #[inline] fn add_attribute(&mut self, _key: K, _value: V) where diff --git a/opentelemetry/src/logs/record.rs b/opentelemetry/src/logs/record.rs index 3dceacfd7e..1dc80c937e 100644 --- a/opentelemetry/src/logs/record.rs +++ b/opentelemetry/src/logs/record.rs @@ -26,7 +26,11 @@ pub trait LogRecord { fn set_body(&mut self, body: AnyValue); /// Adds multiple attributes. - fn add_attributes(&mut self, attributes: Vec<(Key, AnyValue)>); + fn add_attributes(&mut self, attributes: I) + where + I: IntoIterator, + K: Into, + V: Into; /// Adds a single attribute. fn add_attribute(&mut self, key: K, value: V) From 1e7087ffc66b0a59de42b56d48fdc1da64a3b2ea Mon Sep 17 00:00:00 2001 From: Lalit Date: Sat, 11 May 2024 12:37:23 -0700 Subject: [PATCH 2/3] fix test --- opentelemetry-sdk/src/logs/mod.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/opentelemetry-sdk/src/logs/mod.rs b/opentelemetry-sdk/src/logs/mod.rs index 1cf58fc4aa..abe5f260d4 100644 --- a/opentelemetry-sdk/src/logs/mod.rs +++ b/opentelemetry-sdk/src/logs/mod.rs @@ -22,7 +22,6 @@ mod tests { use opentelemetry::{logs::AnyValue, Key, KeyValue}; use std::collections::HashMap; - #[test] fn logging_sdk_test() { // Arrange @@ -57,8 +56,8 @@ mod tests { // Adding Attributes from a HashMap let mut attributes_map = HashMap::new(); - attributes_map.insert("user_id", "12345"); - attributes_map.insert("session_id", "abcde"); + attributes_map.insert("key9", "value9"); + attributes_map.insert("key10", "value10"); log_record.add_attributes(attributes_map); @@ -79,7 +78,7 @@ mod tests { .attributes .clone() .expect("Attributes are expected"); - assert_eq!(attributes.len(), 8); + assert_eq!(attributes.len(), 10); assert!(log .record .attributes @@ -121,13 +120,25 @@ mod tests { .attributes .clone() .unwrap() - .contains(&(Key::new("key5"), AnyValue::String("value7".into())))); + .contains(&(Key::new("key7"), AnyValue::String("value7".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key8"), AnyValue::String("value8".into())))); + assert!(log + .record + .attributes + .clone() + .unwrap() + .contains(&(Key::new("key9"), AnyValue::String("value9".into())))); assert!(log .record .attributes .clone() .unwrap() - .contains(&(Key::new("key6"), AnyValue::String("value8".into())))); + .contains(&(Key::new("key10"), AnyValue::String("value10".into())))); } #[test] From 397905a039f7f0f4eda5ce10cc75e6ee6dd8b417 Mon Sep 17 00:00:00 2001 From: Lalit Date: Sat, 11 May 2024 12:44:44 -0700 Subject: [PATCH 3/3] make test validation concise --- opentelemetry-sdk/src/logs/mod.rs | 66 +++---------------------------- 1 file changed, 6 insertions(+), 60 deletions(-) diff --git a/opentelemetry-sdk/src/logs/mod.rs b/opentelemetry-sdk/src/logs/mod.rs index abe5f260d4..f5cd2e2da7 100644 --- a/opentelemetry-sdk/src/logs/mod.rs +++ b/opentelemetry-sdk/src/logs/mod.rs @@ -79,66 +79,12 @@ mod tests { .clone() .expect("Attributes are expected"); assert_eq!(attributes.len(), 10); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key1"), AnyValue::String("value1".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key2"), AnyValue::String("value2".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key3"), AnyValue::String("value3".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key4"), AnyValue::String("value4".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key5"), AnyValue::String("value5".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key6"), AnyValue::String("value6".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key7"), AnyValue::String("value7".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key8"), AnyValue::String("value8".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key9"), AnyValue::String("value9".into())))); - assert!(log - .record - .attributes - .clone() - .unwrap() - .contains(&(Key::new("key10"), AnyValue::String("value10".into())))); + for i in 1..=10 { + assert!(log.record.attributes.clone().unwrap().contains(&( + Key::new(format!("key{}", i)), + AnyValue::String(format!("value{}", i).into()) + ))); + } } #[test]