Skip to content

Commit 6013bec

Browse files
committed
make add_attributes interface itrable
1 parent 5e02f38 commit 6013bec

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

opentelemetry-sdk/src/logs/mod.rs

+75-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ mod tests {
2020
use opentelemetry::logs::LogRecord;
2121
use opentelemetry::logs::{Logger, LoggerProvider as _, Severity};
2222
use opentelemetry::{logs::AnyValue, Key, KeyValue};
23+
use std::collections::HashMap;
24+
2325

2426
#[test]
2527
fn logging_sdk_test() {
@@ -34,10 +36,32 @@ mod tests {
3436
let mut log_record = logger.create_log_record();
3537
log_record.set_severity_number(Severity::Error);
3638
log_record.set_severity_text("Error".into());
39+
40+
// Adding attributes using a vector with explicitly constructed Key and AnyValue objects.
3741
log_record.add_attributes(vec![
38-
(Key::new("key1"), "value1".into()),
39-
(Key::new("key2"), "value2".into()),
42+
(Key::new("key1"), AnyValue::from("value1")),
43+
(Key::new("key2"), AnyValue::from("value2")),
4044
]);
45+
46+
// Adding attributes using an array with explicitly constructed Key and AnyValue objects.
47+
log_record.add_attributes([
48+
(Key::new("key3"), AnyValue::from("value3")),
49+
(Key::new("key4"), AnyValue::from("value4")),
50+
]);
51+
52+
// Adding attributes using a vector with tuple auto-conversion to Key and AnyValue.
53+
log_record.add_attributes(vec![("key5", "value5"), ("key6", "value6")]);
54+
55+
// Adding attributes using an array with tuple auto-conversion to Key and AnyValue.
56+
log_record.add_attributes([("key7", "value7"), ("key8", "value8")]);
57+
58+
// Adding Attributes from a HashMap
59+
let mut attributes_map = HashMap::new();
60+
attributes_map.insert("user_id", "12345");
61+
attributes_map.insert("session_id", "abcde");
62+
63+
log_record.add_attributes(attributes_map);
64+
4165
logger.emit(log_record);
4266

4367
// Assert
@@ -55,7 +79,55 @@ mod tests {
5579
.attributes
5680
.clone()
5781
.expect("Attributes are expected");
58-
assert_eq!(attributes.len(), 2);
82+
assert_eq!(attributes.len(), 8);
83+
assert!(log
84+
.record
85+
.attributes
86+
.clone()
87+
.unwrap()
88+
.contains(&(Key::new("key1"), AnyValue::String("value1".into()))));
89+
assert!(log
90+
.record
91+
.attributes
92+
.clone()
93+
.unwrap()
94+
.contains(&(Key::new("key2"), AnyValue::String("value2".into()))));
95+
assert!(log
96+
.record
97+
.attributes
98+
.clone()
99+
.unwrap()
100+
.contains(&(Key::new("key3"), AnyValue::String("value3".into()))));
101+
assert!(log
102+
.record
103+
.attributes
104+
.clone()
105+
.unwrap()
106+
.contains(&(Key::new("key4"), AnyValue::String("value4".into()))));
107+
assert!(log
108+
.record
109+
.attributes
110+
.clone()
111+
.unwrap()
112+
.contains(&(Key::new("key5"), AnyValue::String("value5".into()))));
113+
assert!(log
114+
.record
115+
.attributes
116+
.clone()
117+
.unwrap()
118+
.contains(&(Key::new("key6"), AnyValue::String("value6".into()))));
119+
assert!(log
120+
.record
121+
.attributes
122+
.clone()
123+
.unwrap()
124+
.contains(&(Key::new("key5"), AnyValue::String("value7".into()))));
125+
assert!(log
126+
.record
127+
.attributes
128+
.clone()
129+
.unwrap()
130+
.contains(&(Key::new("key6"), AnyValue::String("value8".into()))));
59131
}
60132

61133
#[test]

opentelemetry-sdk/src/logs/record.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ impl opentelemetry::logs::LogRecord for LogRecord {
6262
self.body = Some(body);
6363
}
6464

65-
fn add_attributes(&mut self, attributes: Vec<(Key, AnyValue)>) {
66-
self.attributes = Some(attributes);
65+
fn add_attributes<I, K, V>(&mut self, attributes: I)
66+
where
67+
I: IntoIterator<Item = (K, V)>,
68+
K: Into<Key>,
69+
V: Into<AnyValue>,
70+
{
71+
for (key, value) in attributes.into_iter() {
72+
self.add_attribute(key, value);
73+
}
6774
}
6875

6976
fn add_attribute<K, V>(&mut self, key: K, value: V)

opentelemetry/src/logs/noop.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ impl LogRecord for NoopLogRecord {
5757
#[inline]
5858
fn set_body(&mut self, _body: AnyValue) {}
5959
#[inline]
60-
fn add_attributes(&mut self, _attributes: Vec<(Key, AnyValue)>) {}
60+
fn add_attributes<I, K, V>(&mut self, _attributes: I)
61+
where
62+
I: IntoIterator<Item = (K, V)>,
63+
K: Into<Key>,
64+
V: Into<AnyValue>,
65+
{
66+
}
6167
#[inline]
6268
fn add_attribute<K, V>(&mut self, _key: K, _value: V)
6369
where

opentelemetry/src/logs/record.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ pub trait LogRecord {
2626
fn set_body(&mut self, body: AnyValue);
2727

2828
/// Adds multiple attributes.
29-
fn add_attributes(&mut self, attributes: Vec<(Key, AnyValue)>);
29+
fn add_attributes<I, K, V>(&mut self, attributes: I)
30+
where
31+
I: IntoIterator<Item = (K, V)>,
32+
K: Into<Key>,
33+
V: Into<AnyValue>;
3034

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

0 commit comments

Comments
 (0)