Skip to content

Commit 7c7d58e

Browse files
committed
Merge branch 'cijothomas/metric-shutdown-remove' of https://github.com/cijothomas/opentelemetry-rust into cijothomas/metric-shutdown-remove
2 parents d69cbcc + 2faee10 commit 7c7d58e

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

opentelemetry-sdk/src/logs/mod.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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;
2324

2425
#[test]
2526
fn logging_sdk_test() {
@@ -34,10 +35,32 @@ mod tests {
3435
let mut log_record = logger.create_log_record();
3536
log_record.set_severity_number(Severity::Error);
3637
log_record.set_severity_text("Error".into());
38+
39+
// Adding attributes using a vector with explicitly constructed Key and AnyValue objects.
3740
log_record.add_attributes(vec![
38-
(Key::new("key1"), "value1".into()),
39-
(Key::new("key2"), "value2".into()),
41+
(Key::new("key1"), AnyValue::from("value1")),
42+
(Key::new("key2"), AnyValue::from("value2")),
43+
]);
44+
45+
// Adding attributes using an array with explicitly constructed Key and AnyValue objects.
46+
log_record.add_attributes([
47+
(Key::new("key3"), AnyValue::from("value3")),
48+
(Key::new("key4"), AnyValue::from("value4")),
4049
]);
50+
51+
// Adding attributes using a vector with tuple auto-conversion to Key and AnyValue.
52+
log_record.add_attributes(vec![("key5", "value5"), ("key6", "value6")]);
53+
54+
// Adding attributes using an array with tuple auto-conversion to Key and AnyValue.
55+
log_record.add_attributes([("key7", "value7"), ("key8", "value8")]);
56+
57+
// Adding Attributes from a HashMap
58+
let mut attributes_map = HashMap::new();
59+
attributes_map.insert("key9", "value9");
60+
attributes_map.insert("key10", "value10");
61+
62+
log_record.add_attributes(attributes_map);
63+
4164
logger.emit(log_record);
4265

4366
// Assert
@@ -55,7 +78,13 @@ mod tests {
5578
.attributes
5679
.clone()
5780
.expect("Attributes are expected");
58-
assert_eq!(attributes.len(), 2);
81+
assert_eq!(attributes.len(), 10);
82+
for i in 1..=10 {
83+
assert!(log.record.attributes.clone().unwrap().contains(&(
84+
Key::new(format!("key{}", i)),
85+
AnyValue::String(format!("value{}", i).into())
86+
)));
87+
}
5988
}
6089

6190
#[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)