@@ -4,9 +4,8 @@ use opentelemetry_sdk::{
44 metrics:: {
55 data:: { self , Gauge , Histogram , Sum } ,
66 reader:: MetricReader ,
7- InstrumentKind , ManualReader , MeterProviderBuilder , MetricError , SdkMeterProvider ,
7+ InstrumentKind , ManualReader , MeterProviderBuilder , SdkMeterProvider ,
88 } ,
9- Resource ,
109} ;
1110
1211use std:: { fmt:: Debug , sync:: Arc } ;
@@ -548,10 +547,7 @@ impl MetricReader for TestReader {
548547 self . inner . register_pipeline ( pipeline) ;
549548 }
550549
551- fn collect (
552- & self ,
553- rm : & mut data:: ResourceMetrics ,
554- ) -> opentelemetry_sdk:: metrics:: MetricResult < ( ) > {
550+ fn collect ( & self , rm : & mut data:: ResourceMetrics ) -> OTelSdkResult {
555551 self . inner . collect ( rm)
556552 }
557553
@@ -566,6 +562,10 @@ impl MetricReader for TestReader {
566562 fn temporality ( & self , kind : InstrumentKind ) -> opentelemetry_sdk:: metrics:: Temporality {
567563 self . inner . temporality ( kind)
568564 }
565+
566+ fn shutdown_with_timeout ( & self , timeout : std:: time:: Duration ) -> OTelSdkResult {
567+ self . inner . shutdown_with_timeout ( timeout)
568+ }
569569}
570570
571571struct TestExporter < T > {
@@ -577,78 +577,105 @@ struct TestExporter<T> {
577577 _meter_provider : SdkMeterProvider ,
578578}
579579
580+ trait AsAny {
581+ fn as_any ( & self ) -> & dyn std:: any:: Any ;
582+ }
583+
584+ impl AsAny for data:: AggregatedMetrics {
585+ fn as_any ( & self ) -> & dyn std:: any:: Any {
586+ match self {
587+ data:: AggregatedMetrics :: F64 ( x) => match x {
588+ data:: MetricData :: Gauge ( x) => x as & dyn std:: any:: Any ,
589+ data:: MetricData :: Sum ( x) => x as & dyn std:: any:: Any ,
590+ data:: MetricData :: Histogram ( x) => x as & dyn std:: any:: Any ,
591+ data:: MetricData :: ExponentialHistogram ( x) => x as & dyn std:: any:: Any ,
592+ } ,
593+ data:: AggregatedMetrics :: U64 ( x) => match x {
594+ data:: MetricData :: Gauge ( x) => x as & dyn std:: any:: Any ,
595+ data:: MetricData :: Sum ( x) => x as & dyn std:: any:: Any ,
596+ data:: MetricData :: Histogram ( x) => x as & dyn std:: any:: Any ,
597+ data:: MetricData :: ExponentialHistogram ( x) => x as & dyn std:: any:: Any ,
598+ } ,
599+ data:: AggregatedMetrics :: I64 ( x) => match x {
600+ data:: MetricData :: Gauge ( x) => x as & dyn std:: any:: Any ,
601+ data:: MetricData :: Sum ( x) => x as & dyn std:: any:: Any ,
602+ data:: MetricData :: Histogram ( x) => x as & dyn std:: any:: Any ,
603+ data:: MetricData :: ExponentialHistogram ( x) => x as & dyn std:: any:: Any ,
604+ } ,
605+ }
606+ }
607+ }
608+
580609impl < T > TestExporter < T >
581610where
582611 T : Debug + PartialEq + Copy + std:: iter:: Sum + ' static ,
583612{
584- fn export ( & self ) -> Result < ( ) , MetricError > {
585- let mut rm = data:: ResourceMetrics {
586- resource : Resource :: builder ( ) . build ( ) ,
587- scope_metrics : Vec :: new ( ) ,
588- } ;
613+ fn export ( & self ) -> OTelSdkResult {
614+ let mut rm = data:: ResourceMetrics :: default ( ) ;
589615 self . reader . collect ( & mut rm) ?;
590616
591- assert ! ( !rm. scope_metrics. is_empty( ) ) ;
617+ let mut scope_metrics = rm. scope_metrics ( ) . peekable ( ) ;
618+
619+ assert ! ( scope_metrics. peek( ) . is_some( ) ) ;
592620
593- rm . scope_metrics . into_iter ( ) . for_each ( |scope_metrics| {
594- assert_eq ! ( scope_metrics. scope. name( ) , INSTRUMENTATION_LIBRARY_NAME ) ;
595- assert_eq ! ( scope_metrics. scope. version( ) . unwrap( ) , CARGO_PKG_VERSION ) ;
621+ scope_metrics. for_each ( |scope_metrics| {
622+ assert_eq ! ( scope_metrics. scope( ) . name( ) , INSTRUMENTATION_LIBRARY_NAME ) ;
623+ assert_eq ! ( scope_metrics. scope( ) . version( ) . unwrap( ) , CARGO_PKG_VERSION ) ;
596624
597- scope_metrics. metrics . into_iter ( ) . for_each ( |metric| {
598- assert_eq ! ( metric. name, self . expected_metric_name) ;
625+ scope_metrics. metrics ( ) . for_each ( |metric| {
626+ assert_eq ! ( metric. name( ) , self . expected_metric_name) ;
599627
600628 match self . expected_instrument_kind {
601629 InstrumentKind :: Counter | InstrumentKind :: UpDownCounter => {
602- let sum = metric. data . as_any ( ) . downcast_ref :: < Sum < T > > ( ) . unwrap ( ) ;
630+ let sum = metric. data ( ) . as_any ( ) . downcast_ref :: < Sum < T > > ( ) . unwrap ( ) ;
603631 assert_eq ! (
604632 self . expected_value,
605- sum. data_points
606- . iter( )
607- . map( |data_point| data_point. value)
608- . sum( )
633+ sum. data_points( ) . map( |data_point| data_point. value( ) ) . sum( )
609634 ) ;
610635
611636 if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
612- sum. data_points . iter ( ) . for_each ( |data_point| {
637+ sum. data_points ( ) . for_each ( |data_point| {
613638 assert ! ( compare_attributes(
614639 expected_attributes,
615- & data_point. attributes,
640+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
616641 ) )
617642 } ) ;
618643 }
619644 }
620645 InstrumentKind :: Gauge => {
621- let gauge = metric. data . as_any ( ) . downcast_ref :: < Gauge < T > > ( ) . unwrap ( ) ;
646+ let gauge = metric. data ( ) . as_any ( ) . downcast_ref :: < Gauge < T > > ( ) . unwrap ( ) ;
622647 assert_eq ! (
623648 self . expected_value,
624649 gauge
625- . data_points
626- . iter( )
627- . map( |data_point| data_point. value)
628- . next_back( )
650+ . data_points( )
651+ . map( |data_point| data_point. value( ) )
652+ . last( )
629653 . unwrap( )
630654 ) ;
631655
632656 if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
633- gauge. data_points . iter ( ) . for_each ( |data_point| {
657+ gauge. data_points ( ) . for_each ( |data_point| {
634658 assert ! ( compare_attributes(
635659 expected_attributes,
636- & data_point. attributes,
660+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
637661 ) )
638662 } ) ;
639663 }
640664 }
641665 InstrumentKind :: Histogram => {
642- let histogram =
643- metric. data . as_any ( ) . downcast_ref :: < Histogram < T > > ( ) . unwrap ( ) ;
644- let histogram_data = histogram. data_points . first ( ) . unwrap ( ) ;
645- assert ! ( histogram_data. count > 0 ) ;
646- assert_eq ! ( histogram_data. sum, self . expected_value) ;
666+ let histogram = metric
667+ . data ( )
668+ . as_any ( )
669+ . downcast_ref :: < Histogram < T > > ( )
670+ . unwrap ( ) ;
671+ let histogram_data = histogram. data_points ( ) . next ( ) . unwrap ( ) ;
672+ assert ! ( histogram_data. count( ) > 0 ) ;
673+ assert_eq ! ( histogram_data. sum( ) , self . expected_value) ;
647674
648675 if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
649676 assert ! ( compare_attributes(
650677 expected_attributes,
651- & histogram_data. attributes
678+ histogram_data. attributes( ) . cloned ( ) . collect ( ) ,
652679 ) )
653680 }
654681 }
@@ -666,7 +693,7 @@ where
666693// After sorting the KeyValue vec, compare them.
667694// Return true if they are equal.
668695#[ allow( clippy:: ptr_arg) ]
669- fn compare_attributes ( expected : & Vec < KeyValue > , actual : & Vec < KeyValue > ) -> bool {
696+ fn compare_attributes ( expected : & Vec < KeyValue > , actual : Vec < KeyValue > ) -> bool {
670697 let mut expected = expected. clone ( ) ;
671698 let mut actual = actual. clone ( ) ;
672699
0 commit comments