@@ -4,9 +4,8 @@ use opentelemetry_sdk::{
4
4
metrics:: {
5
5
data:: { self , Gauge , Histogram , Sum } ,
6
6
reader:: MetricReader ,
7
- InstrumentKind , ManualReader , MeterProviderBuilder , MetricError , SdkMeterProvider ,
7
+ InstrumentKind , ManualReader , MeterProviderBuilder , SdkMeterProvider ,
8
8
} ,
9
- Resource ,
10
9
} ;
11
10
12
11
use std:: { fmt:: Debug , sync:: Arc } ;
@@ -548,10 +547,7 @@ impl MetricReader for TestReader {
548
547
self . inner . register_pipeline ( pipeline) ;
549
548
}
550
549
551
- fn collect (
552
- & self ,
553
- rm : & mut data:: ResourceMetrics ,
554
- ) -> opentelemetry_sdk:: metrics:: MetricResult < ( ) > {
550
+ fn collect ( & self , rm : & mut data:: ResourceMetrics ) -> OTelSdkResult {
555
551
self . inner . collect ( rm)
556
552
}
557
553
@@ -566,6 +562,10 @@ impl MetricReader for TestReader {
566
562
fn temporality ( & self , kind : InstrumentKind ) -> opentelemetry_sdk:: metrics:: Temporality {
567
563
self . inner . temporality ( kind)
568
564
}
565
+
566
+ fn shutdown_with_timeout ( & self , timeout : std:: time:: Duration ) -> OTelSdkResult {
567
+ self . inner . shutdown_with_timeout ( timeout)
568
+ }
569
569
}
570
570
571
571
struct TestExporter < T > {
@@ -577,78 +577,105 @@ struct TestExporter<T> {
577
577
_meter_provider : SdkMeterProvider ,
578
578
}
579
579
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
+
580
609
impl < T > TestExporter < T >
581
610
where
582
611
T : Debug + PartialEq + Copy + std:: iter:: Sum + ' static ,
583
612
{
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 ( ) ;
589
615
self . reader . collect ( & mut rm) ?;
590
616
591
- assert ! ( !rm. scope_metrics. is_empty( ) ) ;
617
+ let mut scope_metrics = rm. scope_metrics ( ) . peekable ( ) ;
618
+
619
+ assert ! ( scope_metrics. peek( ) . is_some( ) ) ;
592
620
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 ) ;
596
624
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) ;
599
627
600
628
match self . expected_instrument_kind {
601
629
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 ( ) ;
603
631
assert_eq ! (
604
632
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( )
609
634
) ;
610
635
611
636
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| {
613
638
assert ! ( compare_attributes(
614
639
expected_attributes,
615
- & data_point. attributes,
640
+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
616
641
) )
617
642
} ) ;
618
643
}
619
644
}
620
645
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 ( ) ;
622
647
assert_eq ! (
623
648
self . expected_value,
624
649
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( )
629
653
. unwrap( )
630
654
) ;
631
655
632
656
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| {
634
658
assert ! ( compare_attributes(
635
659
expected_attributes,
636
- & data_point. attributes,
660
+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
637
661
) )
638
662
} ) ;
639
663
}
640
664
}
641
665
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) ;
647
674
648
675
if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
649
676
assert ! ( compare_attributes(
650
677
expected_attributes,
651
- & histogram_data. attributes
678
+ histogram_data. attributes( ) . cloned ( ) . collect ( ) ,
652
679
) )
653
680
}
654
681
}
@@ -666,7 +693,7 @@ where
666
693
// After sorting the KeyValue vec, compare them.
667
694
// Return true if they are equal.
668
695
#[ 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 {
670
697
let mut expected = expected. clone ( ) ;
671
698
let mut actual = actual. clone ( ) ;
672
699
0 commit comments