Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance LogRecord::add_attributes interface to accept generic Iterables #1741

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 32 additions & 3 deletions opentelemetry-sdk/src/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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]
Expand Down
11 changes: 9 additions & 2 deletions opentelemetry-sdk/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, K, V>(&mut self, attributes: I)
where
I: IntoIterator<Item = (K, V)>,
K: Into<Key>,
V: Into<AnyValue>,
{
for (key, value) in attributes.into_iter() {
self.add_attribute(key, value);
}
}

fn add_attribute<K, V>(&mut self, key: K, value: V)
Expand Down
8 changes: 7 additions & 1 deletion opentelemetry/src/logs/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@
#[inline]
fn set_body(&mut self, _body: AnyValue) {}
#[inline]
fn add_attributes(&mut self, _attributes: Vec<(Key, AnyValue)>) {}
fn add_attributes<I, K, V>(&mut self, _attributes: I)
where
I: IntoIterator<Item = (K, V)>,
K: Into<Key>,
V: Into<AnyValue>,
{
}

Check warning on line 66 in opentelemetry/src/logs/noop.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry/src/logs/noop.rs#L60-L66

Added lines #L60 - L66 were not covered by tests
#[inline]
fn add_attribute<K, V>(&mut self, _key: K, _value: V)
where
Expand Down
6 changes: 5 additions & 1 deletion opentelemetry/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, K, V>(&mut self, attributes: I)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to leave a comment explaining that this API is designed to take ownership, unlike Metric APIs which just need slice/ref only to attributes.

where
I: IntoIterator<Item = (K, V)>,
K: Into<Key>,
V: Into<AnyValue>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not adding the default implementation to iteratively call add_attribute in API, let SDK decide how to add the attributes.


/// Adds a single attribute.
fn add_attribute<K, V>(&mut self, key: K, value: V)
Expand Down
Loading