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:: { Metric , ResourceMetrics , ScopeMetrics } ,
10+ Temporality ,
11+ } ;
12+
13+ /// A collection of [`BatchScopeMetrics`] and the associated [Resource] that created them.
14+ #[ derive( Debug ) ]
15+ pub struct ResourceMetricsRef < ' a > {
16+ /// The entity that collected the metrics.
17+ pub resource : & ' a Resource ,
18+ /// The collection of metrics with unique [InstrumentationScope]s.
19+ pub scope_metrics : BatchScopeMetrics < ' a > ,
20+ }
21+
22+ /// Iterator over libraries instrumentation scopes ([`InstrumentationScope`]) together with metrics.
23+ pub struct BatchScopeMetrics < ' a > {
24+ iter : Iter < ' a , ScopeMetrics > ,
25+ }
26+
27+ /// A collection of metrics produced by a [`InstrumentationScope`] meter.
28+ #[ derive( Debug ) ]
29+ pub struct ScopeMetricsRef < ' a > {
30+ /// The [InstrumentationScope] that the meter was created with.
31+ pub scope : & ' a InstrumentationScope ,
32+ /// The list of aggregations created by the meter.
33+ pub metrics : BatchMetrics < ' a > ,
34+ }
35+
36+ /// Iterator over aggregations created by the meter.
37+ pub struct BatchMetrics < ' a > {
38+ iter : Iter < ' a , Metric > ,
39+ }
40+
41+ impl < ' a > ResourceMetricsRef < ' a > {
42+ pub ( crate ) fn new ( rm : & ' a ResourceMetrics ) -> Self {
43+ Self {
44+ resource : & rm. resource ,
45+ scope_metrics : BatchScopeMetrics {
46+ iter : rm. scope_metrics . iter ( ) ,
47+ } ,
48+ }
49+ }
50+ }
51+
52+ impl < ' a > ScopeMetricsRef < ' a > {
53+ fn new ( sm : & ' a ScopeMetrics ) -> Self {
54+ Self {
55+ scope : & sm. scope ,
56+ metrics : BatchMetrics {
57+ iter : sm. metrics . iter ( ) ,
58+ } ,
59+ }
60+ }
61+ }
62+
63+ impl Debug for BatchScopeMetrics < ' _ > {
64+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
65+ f. debug_struct ( "BatchScopeMetrics" ) . finish ( )
66+ }
67+ }
68+
69+ impl < ' a > Iterator for BatchScopeMetrics < ' a > {
70+ type Item = ScopeMetricsRef < ' a > ;
71+
72+ fn next ( & mut self ) -> Option < Self :: Item > {
73+ self . iter . next ( ) . map ( ScopeMetricsRef :: new)
74+ }
75+ }
76+
77+ impl < ' a > Iterator for BatchMetrics < ' a > {
78+ type Item = & ' a Metric ;
79+
80+ fn next ( & mut self ) -> Option < Self :: Item > {
81+ self . iter . next ( )
82+ }
83+ }
84+
85+ impl Debug for BatchMetrics < ' _ > {
86+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
87+ f. debug_struct ( "BatchMetrics" ) . finish ( )
88+ }
89+ }
990
1091/// Exporter handles the delivery of metric data to external receivers.
1192///
@@ -18,7 +99,7 @@ pub trait PushMetricExporter: Send + Sync + 'static {
1899 /// considered unrecoverable and will be logged.
19100 fn export (
20101 & self ,
21- metrics : & mut ResourceMetrics ,
102+ metrics : ResourceMetricsRef < ' _ > ,
22103 ) -> impl std:: future:: Future < Output = OTelSdkResult > + Send ;
23104
24105 /// Flushes any metric data held by an exporter.
0 commit comments