@@ -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,100 @@ 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 ( data:: MetricData :: Sum ( sum) ) => sum as & dyn std:: any:: Any ,
588
+ data:: AggregatedMetrics :: U64 ( data:: MetricData :: Sum ( sum) ) => sum as & dyn std:: any:: Any ,
589
+ data:: AggregatedMetrics :: I64 ( data:: MetricData :: Sum ( sum) ) => sum as & dyn std:: any:: Any ,
590
+ data:: AggregatedMetrics :: F64 ( data:: MetricData :: Histogram ( histogram) ) => {
591
+ histogram as & dyn std:: any:: Any
592
+ }
593
+ data:: AggregatedMetrics :: U64 ( data:: MetricData :: Histogram ( histogram) ) => {
594
+ histogram as & dyn std:: any:: Any
595
+ }
596
+ data:: AggregatedMetrics :: I64 ( data:: MetricData :: Histogram ( histogram) ) => {
597
+ histogram as & dyn std:: any:: Any
598
+ }
599
+ _ => unreachable ! ( "expected a Sum<T> or Histogram<T>" ) ,
600
+ }
601
+ }
602
+ }
603
+
580
604
impl < T > TestExporter < T >
581
605
where
582
606
T : Debug + PartialEq + Copy + std:: iter:: Sum + ' static ,
583
607
{
584
- fn export ( & self ) -> Result < ( ) , MetricError > {
585
- let mut rm = data:: ResourceMetrics {
586
- resource : Resource :: builder ( ) . build ( ) ,
587
- scope_metrics : Vec :: new ( ) ,
588
- } ;
608
+ fn export ( & self ) -> OTelSdkResult {
609
+ let mut rm = data:: ResourceMetrics :: default ( ) ;
589
610
self . reader . collect ( & mut rm) ?;
590
611
591
- assert ! ( !rm. scope_metrics. is_empty( ) ) ;
612
+ let mut scope_metrics = rm. scope_metrics ( ) . peekable ( ) ;
613
+
614
+ assert ! ( scope_metrics. peek( ) . is_some( ) ) ;
592
615
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 ) ;
616
+ scope_metrics. for_each ( |scope_metrics| {
617
+ assert_eq ! ( scope_metrics. scope( ) . name( ) , INSTRUMENTATION_LIBRARY_NAME ) ;
618
+ assert_eq ! ( scope_metrics. scope( ) . version( ) . unwrap( ) , CARGO_PKG_VERSION ) ;
596
619
597
- scope_metrics. metrics . into_iter ( ) . for_each ( |metric| {
598
- assert_eq ! ( metric. name, self . expected_metric_name) ;
620
+ scope_metrics. metrics ( ) . for_each ( |metric| {
621
+ assert_eq ! ( metric. name( ) , self . expected_metric_name) ;
599
622
600
623
match self . expected_instrument_kind {
601
624
InstrumentKind :: Counter | InstrumentKind :: UpDownCounter => {
602
- let sum = metric. data . as_any ( ) . downcast_ref :: < Sum < T > > ( ) . unwrap ( ) ;
625
+ let sum = metric. data ( ) . as_any ( ) . downcast_ref :: < Sum < T > > ( ) . unwrap ( ) ;
603
626
assert_eq ! (
604
627
self . expected_value,
605
- sum. data_points
606
- . iter( )
607
- . map( |data_point| data_point. value)
608
- . sum( )
628
+ sum. data_points( ) . map( |data_point| data_point. value( ) ) . sum( )
609
629
) ;
610
630
611
631
if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
612
- sum. data_points . iter ( ) . for_each ( |data_point| {
632
+ sum. data_points ( ) . for_each ( |data_point| {
613
633
assert ! ( compare_attributes(
614
634
expected_attributes,
615
- & data_point. attributes,
635
+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
616
636
) )
617
637
} ) ;
618
638
}
619
639
}
620
640
InstrumentKind :: Gauge => {
621
- let gauge = metric. data . as_any ( ) . downcast_ref :: < Gauge < T > > ( ) . unwrap ( ) ;
641
+ let gauge = metric. data ( ) . as_any ( ) . downcast_ref :: < Gauge < T > > ( ) . unwrap ( ) ;
622
642
assert_eq ! (
623
643
self . expected_value,
624
644
gauge
625
- . data_points
626
- . iter( )
627
- . map( |data_point| data_point. value)
628
- . next_back( )
645
+ . data_points( )
646
+ . map( |data_point| data_point. value( ) )
647
+ . last( )
629
648
. unwrap( )
630
649
) ;
631
650
632
651
if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
633
- gauge. data_points . iter ( ) . for_each ( |data_point| {
652
+ gauge. data_points ( ) . for_each ( |data_point| {
634
653
assert ! ( compare_attributes(
635
654
expected_attributes,
636
- & data_point. attributes,
655
+ data_point. attributes( ) . cloned ( ) . collect ( ) ,
637
656
) )
638
657
} ) ;
639
658
}
640
659
}
641
660
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) ;
661
+ let histogram = metric
662
+ . data ( )
663
+ . as_any ( )
664
+ . downcast_ref :: < Histogram < T > > ( )
665
+ . unwrap ( ) ;
666
+ let histogram_data = histogram. data_points ( ) . next ( ) . unwrap ( ) ;
667
+ assert ! ( histogram_data. count( ) > 0 ) ;
668
+ assert_eq ! ( histogram_data. sum( ) , self . expected_value) ;
647
669
648
670
if let Some ( expected_attributes) = self . expected_attributes . as_ref ( ) {
649
671
assert ! ( compare_attributes(
650
672
expected_attributes,
651
- & histogram_data. attributes
673
+ histogram_data. attributes( ) . cloned ( ) . collect ( ) ,
652
674
) )
653
675
}
654
676
}
@@ -666,7 +688,7 @@ where
666
688
// After sorting the KeyValue vec, compare them.
667
689
// Return true if they are equal.
668
690
#[ allow( clippy:: ptr_arg) ]
669
- fn compare_attributes ( expected : & Vec < KeyValue > , actual : & Vec < KeyValue > ) -> bool {
691
+ fn compare_attributes ( expected : & Vec < KeyValue > , actual : Vec < KeyValue > ) -> bool {
670
692
let mut expected = expected. clone ( ) ;
671
693
let mut actual = actual. clone ( ) ;
672
694
0 commit comments