33use opentelemetry:: InstrumentationScope ;
44
55use crate :: { error:: OTelSdkResult , Resource } ;
6- use std:: { fmt:: Debug , slice:: Iter , time:: Duration } ;
6+ use std:: {
7+ fmt:: Debug ,
8+ slice:: Iter ,
9+ time:: { Duration , SystemTime } ,
10+ } ;
711
812use super :: {
9- data:: AggregatedMetrics ,
10- reader :: { MetricsData , ResourceMetricsData , ScopeMetricsData } ,
13+ data:: { AggregatedMetrics , Sum } ,
14+ pipeline :: InstrumentSync ,
1115 InstrumentInfo , Temporality ,
1216} ;
1317
@@ -23,7 +27,7 @@ pub struct ResourceMetrics<'a> {
2327/// Iterator over libraries instrumentation scopes ([`InstrumentationScope`]) together with metrics.
2428/// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
2529pub struct ScopeMetricsLendingIter < ' a > {
26- iter : Iter < ' a , ScopeMetricsData > ,
30+ iter : std :: collections :: hash_map :: Iter < ' a , InstrumentationScope , Vec < InstrumentSync > > ,
2731}
2832
2933/// A collection of metrics produced by a [`InstrumentationScope`] meter.
@@ -38,7 +42,9 @@ pub struct ScopeMetrics<'a> {
3842/// Iterator over aggregations created by the meter.
3943/// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
4044pub struct MetricsLendingIter < ' a > {
41- iter : Iter < ' a , MetricsData > ,
45+ // for optimization purposes
46+ aggr : AggregatedMetrics ,
47+ iter : Iter < ' a , InstrumentSync > ,
4248}
4349
4450/// A collection of one or more aggregated time series from an [Instrument].
@@ -53,23 +59,13 @@ pub struct Metric<'a> {
5359}
5460
5561impl < ' a > ResourceMetrics < ' a > {
56- pub ( crate ) fn new ( rm : & ' a ResourceMetricsData ) -> Self {
57- Self {
58- resource : & rm. resource ,
59- scope_metrics : ScopeMetricsLendingIter {
60- iter : rm. scope_metrics . iter ( ) ,
61- } ,
62- }
63- }
64- }
65-
66- impl < ' a > ScopeMetrics < ' a > {
67- fn new ( sm : & ' a ScopeMetricsData ) -> Self {
62+ pub ( crate ) fn new (
63+ resource : & ' a Resource ,
64+ iter : std:: collections:: hash_map:: Iter < ' a , InstrumentationScope , Vec < InstrumentSync > > ,
65+ ) -> Self {
6866 Self {
69- scope : & sm. scope ,
70- metrics : MetricsLendingIter {
71- iter : sm. metrics . iter ( ) ,
72- } ,
67+ resource,
68+ scope_metrics : ScopeMetricsLendingIter { iter } ,
7369 }
7470 }
7571}
@@ -81,19 +77,42 @@ impl Debug for ScopeMetricsLendingIter<'_> {
8177}
8278
8379impl ScopeMetricsLendingIter < ' _ > {
84- /// Advances the iterator and returns the next value.
85- pub fn next ( & mut self ) -> Option < ScopeMetrics < ' _ > > {
86- self . iter . next ( ) . map ( ScopeMetrics :: new)
80+ /// Advances the iterator and returns the next value.
81+ pub fn next_scope_metric ( & mut self ) -> Option < ScopeMetrics < ' _ > > {
82+ self . iter . next ( ) . map ( |( scope, instruments) | ScopeMetrics {
83+ scope,
84+ metrics : MetricsLendingIter {
85+ // doesn't matter what we initialize this with,
86+ // it's purpose is to be reused between collections
87+ aggr : AggregatedMetrics :: F64 ( super :: data:: MetricData :: Sum ( Sum {
88+ is_monotonic : true ,
89+ data_points : Vec :: new ( ) ,
90+ start_time : SystemTime :: now ( ) ,
91+ time : SystemTime :: now ( ) ,
92+ temporality : Temporality :: Cumulative ,
93+ } ) ) ,
94+ iter : instruments. iter ( ) ,
95+ } ,
96+ } )
8797 }
8898}
8999
90100impl MetricsLendingIter < ' _ > {
91- /// Advances the iterator and returns the next value.
92- pub fn next ( & mut self ) -> Option < Metric < ' _ > > {
93- self . iter . next ( ) . map ( |metric| Metric {
94- instrument : & metric. instrument ,
95- data : & metric. data ,
96- } )
101+ /// Advances the iterator and returns the next value.
102+ pub fn next_metric ( & mut self ) -> Option < Metric < ' _ > > {
103+ loop {
104+ let inst = self . iter . next ( ) ?;
105+ let ( len, data) = inst. comp_agg . call ( Some ( & mut self . aggr ) ) ;
106+ if len > 0 {
107+ if let Some ( new_aggr) = data {
108+ self . aggr = new_aggr;
109+ }
110+ return Some ( Metric {
111+ instrument : & inst. info ,
112+ data : & self . aggr ,
113+ } ) ;
114+ }
115+ }
97116 }
98117}
99118
0 commit comments