3
3
use opentelemetry:: InstrumentationScope ;
4
4
5
5
use 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
+ } ;
7
11
8
12
use super :: {
9
- data:: AggregatedMetrics ,
10
- reader :: { MetricsData , ResourceMetricsData , ScopeMetricsData } ,
13
+ data:: { AggregatedMetrics , Sum } ,
14
+ pipeline :: InstrumentSync ,
11
15
InstrumentInfo , Temporality ,
12
16
} ;
13
17
@@ -23,7 +27,7 @@ pub struct ResourceMetrics<'a> {
23
27
/// Iterator over libraries instrumentation scopes ([`InstrumentationScope`]) together with metrics.
24
28
/// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
25
29
pub struct ScopeMetricsLendingIter < ' a > {
26
- iter : Iter < ' a , ScopeMetricsData > ,
30
+ iter : std :: collections :: hash_map :: Iter < ' a , InstrumentationScope , Vec < InstrumentSync > > ,
27
31
}
28
32
29
33
/// A collection of metrics produced by a [`InstrumentationScope`] meter.
@@ -38,7 +42,9 @@ pub struct ScopeMetrics<'a> {
38
42
/// Iterator over aggregations created by the meter.
39
43
/// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
40
44
pub struct MetricsLendingIter < ' a > {
41
- iter : Iter < ' a , MetricsData > ,
45
+ // for optimization purposes
46
+ aggr : AggregatedMetrics ,
47
+ iter : Iter < ' a , InstrumentSync > ,
42
48
}
43
49
44
50
/// A collection of one or more aggregated time series from an [Instrument].
@@ -53,23 +59,13 @@ pub struct Metric<'a> {
53
59
}
54
60
55
61
impl < ' 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 {
68
66
Self {
69
- scope : & sm. scope ,
70
- metrics : MetricsLendingIter {
71
- iter : sm. metrics . iter ( ) ,
72
- } ,
67
+ resource,
68
+ scope_metrics : ScopeMetricsLendingIter { iter } ,
73
69
}
74
70
}
75
71
}
@@ -83,17 +79,40 @@ impl Debug for ScopeMetricsLendingIter<'_> {
83
79
impl ScopeMetricsLendingIter < ' _ > {
84
80
/// Advances the iterator and returns the next value.
85
81
pub fn next ( & mut self ) -> Option < ScopeMetrics < ' _ > > {
86
- self . iter . next ( ) . map ( ScopeMetrics :: new)
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
+ } )
87
97
}
88
98
}
89
99
90
100
impl MetricsLendingIter < ' _ > {
91
101
/// Advances the iterator and returns the next value.
92
102
pub fn next ( & mut self ) -> Option < Metric < ' _ > > {
93
- self . iter . next ( ) . map ( |metric| Metric {
94
- instrument : & metric. instrument ,
95
- data : & metric. data ,
96
- } )
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
+ }
97
116
}
98
117
}
99
118
0 commit comments