11//! Interfaces for exporting metrics
22
3- use crate :: error:: OTelSdkResult ;
4- use std:: time:: Duration ;
3+ use opentelemetry:: InstrumentationScope ;
54
6- use crate :: metrics:: data:: ResourceMetrics ;
5+ use crate :: { error:: OTelSdkResult , Resource } ;
6+ use std:: { fmt:: Debug , slice:: Iter , time:: Duration } ;
77
8- use super :: Temporality ;
8+ use super :: {
9+ data:: AggregatedMetrics ,
10+ reader:: { ResourceMetricsData , ScopeMetricsData } ,
11+ InstrumentInfo , Temporality ,
12+ } ;
13+
14+ /// Stores borrowed metrics and provide a way to collect them
15+ #[ derive( Debug ) ]
16+ pub struct ScopeMetricsCollector < ' a > {
17+ iter : ScopeMetricsLendingIter < ' a > ,
18+ }
19+
20+ impl ScopeMetricsCollector < ' _ > {
21+ /// Start collecting all metrics
22+ pub fn collect ( self , process : impl FnOnce ( ScopeMetricsLendingIter < ' _ > ) ) {
23+ process ( self . iter )
24+ }
25+ }
26+
27+ /// A collection of [`ScopeMetricsCollector`] and the associated [Resource] that created them.
28+ #[ derive( Debug ) ]
29+ pub struct ResourceMetrics < ' a > {
30+ /// The entity that collected the metrics.
31+ pub resource : & ' a Resource ,
32+ /// The collection of metrics with unique [InstrumentationScope]s.
33+ pub scope_metrics : ScopeMetricsCollector < ' a > ,
34+ }
35+
36+ /// Iterator over libraries instrumentation scopes ([`InstrumentationScope`]) together with metrics.
37+ /// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
38+ pub struct ScopeMetricsLendingIter < ' a > {
39+ iter : Iter < ' a , ScopeMetricsData > ,
40+ }
41+
42+ /// A collection of metrics produced by a [`InstrumentationScope`] meter.
43+ #[ derive( Debug ) ]
44+ pub struct ScopeMetrics < ' a > {
45+ /// The [InstrumentationScope] that the meter was created with.
46+ pub scope : & ' a InstrumentationScope ,
47+ /// The list of aggregations created by the meter.
48+ pub metrics : MetricsLendingIter < ' a > ,
49+ }
50+
51+ /// Iterator over aggregations created by the meter.
52+ /// Doesn't implement standard [`Iterator`], because it returns borrowed items. AKA "LendingIterator".
53+ pub struct MetricsLendingIter < ' a > {
54+ iter : Iter < ' a , super :: reader:: MetricsData > ,
55+ }
56+
57+ /// A collection of one or more aggregated time series from an [Instrument].
58+ ///
59+ /// [Instrument]: crate::metrics::Instrument
60+ #[ derive( Debug ) ]
61+ pub struct Metric < ' a > {
62+ /// The name of the instrument that created this data.
63+ pub instrument : & ' a InstrumentInfo ,
64+ /// The aggregated data from an instrument.
65+ pub data : & ' a AggregatedMetrics ,
66+ }
67+
68+ impl < ' a > ResourceMetrics < ' a > {
69+ pub ( crate ) fn new ( data : & ' a ResourceMetricsData ) -> Self {
70+ Self {
71+ resource : & data. resource ,
72+ scope_metrics : ScopeMetricsCollector {
73+ iter : ScopeMetricsLendingIter {
74+ iter : data. scope_metrics . iter ( ) ,
75+ } ,
76+ } ,
77+ }
78+ }
79+ }
80+
81+ impl Debug for ScopeMetricsLendingIter < ' _ > {
82+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
83+ f. debug_struct ( "BatchScopeMetrics" ) . finish ( )
84+ }
85+ }
86+
87+ impl ScopeMetricsLendingIter < ' _ > {
88+ /// Advances the iterator and returns the next value.
89+ pub fn next_scope_metrics ( & mut self ) -> Option < ScopeMetrics < ' _ > > {
90+ self . iter . next ( ) . map ( |item| ScopeMetrics {
91+ scope : & item. scope ,
92+ metrics : MetricsLendingIter {
93+ iter : item. metrics . iter ( ) ,
94+ } ,
95+ } )
96+ }
97+ }
98+
99+ impl MetricsLendingIter < ' _ > {
100+ /// Advances the iterator and returns the next value.
101+ pub fn next_metric ( & mut self ) -> Option < Metric < ' _ > > {
102+ self . iter . next ( ) . map ( |item| Metric {
103+ instrument : & item. instrument ,
104+ data : & item. data ,
105+ } )
106+ }
107+ }
108+
109+ impl Debug for MetricsLendingIter < ' _ > {
110+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
111+ f. debug_struct ( "BatchMetrics" ) . finish ( )
112+ }
113+ }
9114
10115/// Exporter handles the delivery of metric data to external receivers.
11116///
@@ -18,7 +123,7 @@ pub trait PushMetricExporter: Send + Sync + 'static {
18123 /// considered unrecoverable and will be logged.
19124 fn export (
20125 & self ,
21- metrics : & ResourceMetrics ,
126+ metrics : ResourceMetrics < ' _ > ,
22127 ) -> impl std:: future:: Future < Output = OTelSdkResult > + Send ;
23128
24129 /// Flushes any metric data held by an exporter.
0 commit comments