11//! The aggregation of metrics.
22
3+ use std:: sync:: Mutex ;
34use crate :: constants;
45use crate :: datadog:: { self , Metric as MetricToShip , Series } ;
56use crate :: errors;
@@ -357,6 +358,33 @@ fn tags_string_to_vector(tags: Option<Ustr>) -> Vec<String> {
357358 . collect ( )
358359}
359360
361+ #[ cfg( test) ]
362+ pub fn assert_value ( aggregator_mutex : & Mutex < Aggregator > , metric_id : & str , value : f64 ) {
363+ let aggregator = aggregator_mutex. lock ( ) . unwrap ( ) ;
364+ if let Some ( e) = aggregator. get_entry_by_id ( metric_id. into ( ) , None ) {
365+ let metric = e. metric_value . get_value ( ) . unwrap ( ) ;
366+ assert ! ( ( metric - value) . abs( ) < PRECISION ) ;
367+ } else {
368+ panic ! ( "{}" , format!( "{metric_id} not found" ) ) ;
369+ }
370+ }
371+
372+ #[ cfg( test) ]
373+ pub fn assert_sketch ( aggregator_mutex : & Mutex < Aggregator > , metric_id : & str , value : f64 ) {
374+ let aggregator = aggregator_mutex. lock ( ) . unwrap ( ) ;
375+ if let Some ( e) = aggregator. get_entry_by_id ( metric_id. into ( ) , None ) {
376+ let metric = e. metric_value . get_sketch ( ) . unwrap ( ) ;
377+ assert ! ( ( metric. max( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
378+ assert ! ( ( metric. min( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
379+ assert ! ( ( metric. sum( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
380+ assert ! ( ( metric. avg( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
381+ } else {
382+ panic ! ( "{}" , format!( "{metric_id} not found" ) ) ;
383+ }
384+ }
385+ #[ cfg( test) ]
386+ const PRECISION : f64 = 0.000_000_01 ;
387+
360388#[ cfg( test) ]
361389#[ allow( clippy:: unwrap_used) ]
362390pub mod tests {
@@ -366,9 +394,6 @@ pub mod tests {
366394 use datadog_protos:: metrics:: SketchPayload ;
367395 use hashbrown:: hash_table;
368396 use protobuf:: Message ;
369- use std:: sync:: Mutex ;
370-
371- const PRECISION : f64 = 0.000_000_01 ;
372397
373398 const SINGLE_METRIC_SIZE : usize = 187 ;
374399 const SINGLE_DISTRIBUTION_SIZE : u64 = 135 ;
@@ -378,29 +403,6 @@ pub mod tests {
378403 "_dd.compute_stats:1" ,
379404 ] ;
380405
381- pub fn assert_value ( aggregator_mutex : & Mutex < Aggregator > , metric_id : & str , value : f64 ) {
382- let aggregator = aggregator_mutex. lock ( ) . unwrap ( ) ;
383- if let Some ( e) = aggregator. get_entry_by_id ( metric_id. into ( ) , None ) {
384- let metric = e. metric_value . get_value ( ) . unwrap ( ) ;
385- assert ! ( ( metric - value) . abs( ) < PRECISION ) ;
386- } else {
387- panic ! ( "{}" , format!( "{metric_id} not found" ) ) ;
388- }
389- }
390-
391- pub fn assert_sketch ( aggregator_mutex : & Mutex < Aggregator > , metric_id : & str , value : f64 ) {
392- let aggregator = aggregator_mutex. lock ( ) . unwrap ( ) ;
393- if let Some ( e) = aggregator. get_entry_by_id ( metric_id. into ( ) , None ) {
394- let metric = e. metric_value . get_sketch ( ) . unwrap ( ) ;
395- assert ! ( ( metric. max( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
396- assert ! ( ( metric. min( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
397- assert ! ( ( metric. sum( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
398- assert ! ( ( metric. avg( ) . unwrap( ) - value) . abs( ) < PRECISION ) ;
399- } else {
400- panic ! ( "{}" , format!( "{metric_id} not found" ) ) ;
401- }
402- }
403-
404406 #[ test]
405407 fn insertion ( ) {
406408 let mut aggregator = Aggregator :: new ( Vec :: new ( ) , 2 ) . unwrap ( ) ;
0 commit comments