Skip to content

Commit 877996e

Browse files
committed
revert back to option
1 parent e52d002 commit 877996e

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

opentelemetry-sdk/src/logs/log_processor.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,10 @@ mod tests {
822822
}
823823

824824
// Add attribute
825-
data.record.attributes.push((
825+
data.record.attributes.push(Some((
826826
Key::from_static_str("processed_by"),
827827
AnyValue::String("FirstProcessor".into()),
828-
));
828+
)));
829829

830830
// Update body
831831
data.record.body = Some(AnyValue::String("Updated by FirstProcessor".into()));
@@ -854,9 +854,13 @@ mod tests {
854854

855855
impl LogProcessor for SecondProcessor {
856856
fn emit(&self, data: &mut LogData) {
857-
assert!(data.record.attributes.iter().any(|(key, value)| {
858-
key.as_str() == "processed_by"
859-
&& *value == AnyValue::String("FirstProcessor".into())
857+
assert!(data.record.attributes.iter().any(|attr| {
858+
if let Some((key, value)) = attr {
859+
key.as_str() == "processed_by"
860+
&& *value == AnyValue::String("FirstProcessor".into())
861+
} else {
862+
false
863+
}
860864
}));
861865
assert!(
862866
data.record.body.clone().unwrap()
@@ -907,12 +911,22 @@ mod tests {
907911
let first_log = &first_processor_logs.lock().unwrap()[0];
908912
let second_log = &second_processor_logs.lock().unwrap()[0];
909913

910-
assert!(first_log.record.attributes.iter().any(|(key, value)| {
911-
key.as_str() == "processed_by" && *value == AnyValue::String("FirstProcessor".into())
914+
assert!(first_log.record.attributes.iter().any(|attr| {
915+
if let Some((key, value)) = attr {
916+
key.as_str() == "processed_by"
917+
&& *value == AnyValue::String("FirstProcessor".into())
918+
} else {
919+
false
920+
}
912921
}));
913922

914-
assert!(second_log.record.attributes.iter().any(|(key, value)| {
915-
key.as_str() == "processed_by" && *value == AnyValue::String("FirstProcessor".into())
923+
assert!(second_log.record.attributes.iter().any(|attr| {
924+
if let Some((key, value)) = attr {
925+
key.as_str() == "processed_by"
926+
&& *value == AnyValue::String("FirstProcessor".into())
927+
} else {
928+
false
929+
}
916930
}));
917931

918932
assert!(

opentelemetry-sdk/src/logs/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ mod tests {
8585
.record
8686
.attributes
8787
.iter()
88-
.map(|kv| (kv.0.clone(), kv.1.clone()))
88+
.filter_map(|kv| kv.as_ref().map(|(k, v)| (k.clone(), v.clone())))
8989
.collect();
9090
assert_eq!(attributes.len(), 10);
9191
for i in 1..=10 {
92-
assert!(log.record.attributes.iter().any(|(key, value)| {
93-
key.as_str() == format!("key{}", i)
94-
&& *value == AnyValue::String(format!("value{}", i).into())
92+
assert!(log.record.attributes.iter().any(|kv| {
93+
if let Some((key, value)) = kv {
94+
key.as_str() == format!("key{}", i)
95+
&& *value == AnyValue::String(format!("value{}", i).into())
96+
} else {
97+
false
98+
}
9599
}));
96100
}
97101

opentelemetry-sdk/src/logs/record.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use std::{borrow::Cow, time::SystemTime};
1111
// capacity for attributes to avoid reallocation in common scenarios.
1212
const PREALLOCATED_ATTRIBUTE_CAPACITY: usize = 8;
1313

14-
/// A vector of `(Key, AnyValue)` with default capacity.
14+
/// A vector of `Option<(Key, AnyValue)>` with default capacity.
1515
pub(crate) type AttributesGrowableArray =
16-
GrowableArray<(Key, AnyValue), PREALLOCATED_ATTRIBUTE_CAPACITY>;
16+
GrowableArray<Option<(Key, AnyValue)>, PREALLOCATED_ATTRIBUTE_CAPACITY>;
1717

1818
#[derive(Debug, Default, Clone, PartialEq)]
1919
#[non_exhaustive]
@@ -99,14 +99,14 @@ impl opentelemetry::logs::LogRecord for LogRecord {
9999
K: Into<Key>,
100100
V: Into<AnyValue>,
101101
{
102-
self.attributes.push((key.into(), value.into()));
102+
self.attributes.push(Some((key.into(), value.into())));
103103
}
104104
}
105105

106106
impl LogRecord {
107107
/// Provides an iterator over the attributes.
108108
pub fn attributes_iter(&self) -> impl Iterator<Item = &(Key, AnyValue)> {
109-
self.attributes.iter()
109+
self.attributes.iter().filter_map(|opt| opt.as_ref())
110110
}
111111

112112
/// Returns the number of attributes in the `LogRecord`.
@@ -116,7 +116,13 @@ impl LogRecord {
116116

117117
/// Checks if the `LogRecord` contains the specified attribute.
118118
pub fn attributes_contains(&self, key: &Key, value: &AnyValue) -> bool {
119-
self.attributes.iter().any(|(k, v)| k == key && v == value)
119+
self.attributes.iter().any(|opt| {
120+
if let Some((k, v)) = opt {
121+
k == key && v == value
122+
} else {
123+
false
124+
}
125+
})
120126
}
121127
}
122128

@@ -212,7 +218,7 @@ mod tests {
212218

213219
let mut expected_attributes = AttributesGrowableArray::new();
214220
for (key, value) in attributes {
215-
expected_attributes.push((key, value));
221+
expected_attributes.push(Some((key, value)));
216222
}
217223
assert_eq!(log_record.attributes, expected_attributes);
218224
}
@@ -224,7 +230,7 @@ mod tests {
224230

225231
let expected_attributes = {
226232
let mut hybrid_vec = AttributesGrowableArray::new();
227-
hybrid_vec.push((Key::new("key"), AnyValue::String("value".into())));
233+
hybrid_vec.push(Some((Key::new("key"), AnyValue::String("value".into()))));
228234
hybrid_vec
229235
};
230236
assert_eq!(log_record.attributes, expected_attributes);
@@ -263,7 +269,7 @@ mod tests {
263269
body: Some(AnyValue::String("Test body".into())),
264270
attributes: {
265271
let mut hybrid_vec = AttributesGrowableArray::new();
266-
hybrid_vec.push((Key::new("key"), AnyValue::String("value".into())));
272+
hybrid_vec.push(Some((Key::new("key"), AnyValue::String("value".into()))));
267273
hybrid_vec
268274
},
269275
trace_context: Some(TraceContext {

opentelemetry/src/common.rs

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ use std::{fmt, hash};
1010
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1111
pub struct Key(OtelString);
1212

13-
impl Default for Key {
14-
fn default() -> Self {
15-
Key::from_static_str("")
16-
}
17-
}
18-
1913
impl Key {
2014
/// Create a new `Key`.
2115
///

opentelemetry/src/logs/record.rs

-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ pub enum AnyValue {
6666
Map(Box<HashMap<Key, AnyValue>>),
6767
}
6868

69-
impl Default for AnyValue {
70-
fn default() -> Self {
71-
AnyValue::Int(0)
72-
}
73-
}
74-
7569
macro_rules! impl_trivial_from {
7670
($t:ty, $variant:path) => {
7771
impl From<$t> for AnyValue {

0 commit comments

Comments
 (0)