@@ -20,6 +20,8 @@ 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 ;
24
+
23
25
24
26
#[ test]
25
27
fn logging_sdk_test ( ) {
@@ -34,10 +36,32 @@ mod tests {
34
36
let mut log_record = logger. create_log_record ( ) ;
35
37
log_record. set_severity_number ( Severity :: Error ) ;
36
38
log_record. set_severity_text ( "Error" . into ( ) ) ;
39
+
40
+ // Adding attributes using a vector with explicitly constructed Key and AnyValue objects.
37
41
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" ) ) ,
40
44
] ) ;
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
+
41
65
logger. emit ( log_record) ;
42
66
43
67
// Assert
@@ -55,7 +79,55 @@ mod tests {
55
79
. attributes
56
80
. clone ( )
57
81
. 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( ) ) ) ) ) ;
59
131
}
60
132
61
133
#[ test]
0 commit comments