@@ -20,6 +20,7 @@ mod tests {
20
20
use opentelemetry:: logs:: LogRecord ;
21
21
use opentelemetry:: logs:: { Logger , LoggerProvider as _, Severity } ;
22
22
use opentelemetry:: { logs:: AnyValue , Key , KeyValue } ;
23
+ use std:: collections:: HashMap ;
23
24
24
25
#[ test]
25
26
fn logging_sdk_test ( ) {
@@ -34,10 +35,32 @@ mod tests {
34
35
let mut log_record = logger. create_log_record ( ) ;
35
36
log_record. set_severity_number ( Severity :: Error ) ;
36
37
log_record. set_severity_text ( "Error" . into ( ) ) ;
38
+
39
+ // Adding attributes using a vector with explicitly constructed Key and AnyValue objects.
37
40
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" ) ) ,
40
49
] ) ;
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
+
41
64
logger. emit ( log_record) ;
42
65
43
66
// Assert
@@ -55,7 +78,13 @@ mod tests {
55
78
. attributes
56
79
. clone ( )
57
80
. 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
+ }
59
88
}
60
89
61
90
#[ test]
0 commit comments