@@ -66,71 +66,16 @@ pub use view::*;
66
66
67
67
use std:: collections:: hash_map:: DefaultHasher ;
68
68
use std:: collections:: HashSet ;
69
- use std:: {
70
- cmp:: Ordering ,
71
- hash:: { Hash , Hasher } ,
72
- } ;
69
+ use std:: hash:: { Hash , Hasher } ;
73
70
74
- use opentelemetry:: { Array , Key , KeyValue , Value } ;
75
- use ordered_float:: OrderedFloat ;
76
-
77
- #[ derive( Clone , Debug ) ]
78
- struct HashKeyValue ( KeyValue ) ;
79
-
80
- impl Hash for HashKeyValue {
81
- fn hash < H : Hasher > ( & self , state : & mut H ) {
82
- self . 0 . key . hash ( state) ;
83
- match & self . 0 . value {
84
- Value :: F64 ( f) => OrderedFloat ( * f) . hash ( state) ,
85
- Value :: Array ( a) => match a {
86
- Array :: Bool ( b) => b. hash ( state) ,
87
- Array :: I64 ( i) => i. hash ( state) ,
88
- Array :: F64 ( f) => f. iter ( ) . for_each ( |f| OrderedFloat ( * f) . hash ( state) ) ,
89
- Array :: String ( s) => s. hash ( state) ,
90
- } ,
91
- Value :: Bool ( b) => b. hash ( state) ,
92
- Value :: I64 ( i) => i. hash ( state) ,
93
- Value :: String ( s) => s. hash ( state) ,
94
- } ;
95
- }
96
- }
97
-
98
- impl PartialOrd for HashKeyValue {
99
- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
100
- Some ( self . cmp ( other) )
101
- }
102
- }
103
-
104
- impl Ord for HashKeyValue {
105
- fn cmp ( & self , other : & Self ) -> Ordering {
106
- self . 0 . key . cmp ( & other. 0 . key )
107
- }
108
- }
109
-
110
- impl PartialEq for HashKeyValue {
111
- fn eq ( & self , other : & Self ) -> bool {
112
- self . 0 . key == other. 0 . key
113
- && match ( & self . 0 . value , & other. 0 . value ) {
114
- ( Value :: F64 ( f) , Value :: F64 ( of) ) => OrderedFloat ( * f) . eq ( & OrderedFloat ( * of) ) ,
115
- ( Value :: Array ( Array :: F64 ( f) ) , Value :: Array ( Array :: F64 ( of) ) ) => {
116
- f. len ( ) == of. len ( )
117
- && f. iter ( )
118
- . zip ( of. iter ( ) )
119
- . all ( |( f, of) | OrderedFloat ( * f) . eq ( & OrderedFloat ( * of) ) )
120
- }
121
- ( non_float, other_non_float) => non_float. eq ( other_non_float) ,
122
- }
123
- }
124
- }
125
-
126
- impl Eq for HashKeyValue { }
71
+ use opentelemetry:: { Key , KeyValue , Value } ;
127
72
128
73
/// A unique set of attributes that can be used as instrument identifiers.
129
74
///
130
75
/// This must implement [Hash], [PartialEq], and [Eq] so it may be used as
131
76
/// HashMap keys and other de-duplication methods.
132
77
#[ derive( Clone , Default , Debug , PartialEq , Eq ) ]
133
- pub struct AttributeSet ( Vec < HashKeyValue > , u64 ) ;
78
+ pub struct AttributeSet ( Vec < KeyValue > , u64 ) ;
134
79
135
80
impl From < & [ KeyValue ] > for AttributeSet {
136
81
fn from ( values : & [ KeyValue ] ) -> Self {
@@ -140,7 +85,7 @@ impl From<&[KeyValue]> for AttributeSet {
140
85
. rev ( )
141
86
. filter_map ( |kv| {
142
87
if seen_keys. insert ( kv. key . clone ( ) ) {
143
- Some ( HashKeyValue ( kv. clone ( ) ) )
88
+ Some ( kv. clone ( ) )
144
89
} else {
145
90
None
146
91
}
@@ -151,7 +96,7 @@ impl From<&[KeyValue]> for AttributeSet {
151
96
}
152
97
}
153
98
154
- fn calculate_hash ( values : & [ HashKeyValue ] ) -> u64 {
99
+ fn calculate_hash ( values : & [ KeyValue ] ) -> u64 {
155
100
let mut hasher = DefaultHasher :: new ( ) ;
156
101
values. iter ( ) . fold ( & mut hasher, |mut hasher, item| {
157
102
item. hash ( & mut hasher) ;
@@ -161,7 +106,7 @@ fn calculate_hash(values: &[HashKeyValue]) -> u64 {
161
106
}
162
107
163
108
impl AttributeSet {
164
- fn new ( mut values : Vec < HashKeyValue > ) -> Self {
109
+ fn new ( mut values : Vec < KeyValue > ) -> Self {
165
110
values. sort_unstable ( ) ;
166
111
let hash = calculate_hash ( & values) ;
167
112
AttributeSet ( values, hash)
@@ -177,15 +122,15 @@ impl AttributeSet {
177
122
where
178
123
F : Fn ( & KeyValue ) -> bool ,
179
124
{
180
- self . 0 . retain ( |kv| f ( & kv . 0 ) ) ;
125
+ self . 0 . retain ( |kv| f ( kv ) ) ;
181
126
182
127
// Recalculate the hash as elements are changed.
183
128
self . 1 = calculate_hash ( & self . 0 ) ;
184
129
}
185
130
186
131
/// Iterate over key value pairs in the set
187
132
pub fn iter ( & self ) -> impl Iterator < Item = ( & Key , & Value ) > {
188
- self . 0 . iter ( ) . map ( |kv| ( & kv. 0 . key , & kv. 0 . value ) )
133
+ self . 0 . iter ( ) . map ( |kv| ( & kv. key , & kv. value ) )
189
134
}
190
135
}
191
136
@@ -209,52 +154,9 @@ mod tests {
209
154
KeyValue ,
210
155
} ;
211
156
use std:: borrow:: Cow ;
212
- use std:: hash:: DefaultHasher ;
213
- use std:: hash:: { Hash , Hasher } ;
214
157
215
158
// Run all tests in this mod
216
159
// cargo test metrics::tests --features=metrics,testing
217
-
218
- #[ test]
219
- fn equality_kv_float ( ) {
220
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
221
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
222
- assert_eq ! ( kv1, kv2) ;
223
-
224
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
225
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.01 ) ) ;
226
- assert_ne ! ( kv1, kv2) ;
227
-
228
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
229
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
230
- assert_eq ! ( kv1, kv2) ;
231
-
232
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
233
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
234
- assert_eq ! ( kv1, kv2) ;
235
- }
236
-
237
- #[ test]
238
- fn hash_kv_float ( ) {
239
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
240
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
241
- assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
242
-
243
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
244
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
245
- assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
246
-
247
- let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
248
- let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
249
- assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
250
- }
251
-
252
- fn hash_helper < T : Hash > ( item : & T ) -> u64 {
253
- let mut hasher = DefaultHasher :: new ( ) ;
254
- item. hash ( & mut hasher) ;
255
- hasher. finish ( )
256
- }
257
-
258
160
// Note for all tests from this point onwards in this mod:
259
161
// "multi_thread" tokio flavor must be used else flush won't
260
162
// be able to make progress!
0 commit comments