@@ -11,9 +11,9 @@ use std::{borrow::Cow, time::SystemTime};
11
11
// capacity for attributes to avoid reallocation in common scenarios.
12
12
const PREALLOCATED_ATTRIBUTE_CAPACITY : usize = 8 ;
13
13
14
- /// A vector of `(Key, AnyValue)` with default capacity.
14
+ /// A vector of `Option< (Key, AnyValue)> ` with default capacity.
15
15
pub ( crate ) type AttributesGrowableArray =
16
- GrowableArray < ( Key , AnyValue ) , PREALLOCATED_ATTRIBUTE_CAPACITY > ;
16
+ GrowableArray < Option < ( Key , AnyValue ) > , PREALLOCATED_ATTRIBUTE_CAPACITY > ;
17
17
18
18
#[ derive( Debug , Default , Clone , PartialEq ) ]
19
19
#[ non_exhaustive]
@@ -99,14 +99,14 @@ impl opentelemetry::logs::LogRecord for LogRecord {
99
99
K : Into < Key > ,
100
100
V : Into < AnyValue > ,
101
101
{
102
- self . attributes . push ( ( key. into ( ) , value. into ( ) ) ) ;
102
+ self . attributes . push ( Some ( ( key. into ( ) , value. into ( ) ) ) ) ;
103
103
}
104
104
}
105
105
106
106
impl LogRecord {
107
107
/// Provides an iterator over the attributes.
108
108
pub fn attributes_iter ( & self ) -> impl Iterator < Item = & ( Key , AnyValue ) > {
109
- self . attributes . iter ( )
109
+ self . attributes . iter ( ) . filter_map ( |opt| opt . as_ref ( ) )
110
110
}
111
111
112
112
/// Returns the number of attributes in the `LogRecord`.
@@ -116,7 +116,13 @@ impl LogRecord {
116
116
117
117
/// Checks if the `LogRecord` contains the specified attribute.
118
118
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
+ } )
120
126
}
121
127
}
122
128
@@ -212,7 +218,7 @@ mod tests {
212
218
213
219
let mut expected_attributes = AttributesGrowableArray :: new ( ) ;
214
220
for ( key, value) in attributes {
215
- expected_attributes. push ( ( key, value) ) ;
221
+ expected_attributes. push ( Some ( ( key, value) ) ) ;
216
222
}
217
223
assert_eq ! ( log_record. attributes, expected_attributes) ;
218
224
}
@@ -224,7 +230,7 @@ mod tests {
224
230
225
231
let expected_attributes = {
226
232
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 ( ) ) ) ) ) ;
228
234
hybrid_vec
229
235
} ;
230
236
assert_eq ! ( log_record. attributes, expected_attributes) ;
@@ -263,7 +269,7 @@ mod tests {
263
269
body : Some ( AnyValue :: String ( "Test body" . into ( ) ) ) ,
264
270
attributes : {
265
271
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 ( ) ) ) ) ) ;
267
273
hybrid_vec
268
274
} ,
269
275
trace_context : Some ( TraceContext {
0 commit comments