diff --git a/opentelemetry-sdk/src/logs/mod.rs b/opentelemetry-sdk/src/logs/mod.rs index 22157b7a59..f5cd2e2da7 100644 --- a/opentelemetry-sdk/src/logs/mod.rs +++ b/opentelemetry-sdk/src/logs/mod.rs @@ -20,6 +20,7 @@ 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 +35,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("key9", "value9"); + attributes_map.insert("key10", "value10"); + + log_record.add_attributes(attributes_map); + logger.emit(log_record); // Assert @@ -55,7 +78,13 @@ mod tests { .attributes .clone() .expect("Attributes are expected"); - assert_eq!(attributes.len(), 2); + assert_eq!(attributes.len(), 10); + for i in 1..=10 { + assert!(log.record.attributes.clone().unwrap().contains(&( + Key::new(format!("key{}", i)), + AnyValue::String(format!("value{}", i).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)