99//! use opentelemetry::{metrics::MeterProvider, KeyValue};
1010//! use opentelemetry_sdk::metrics::SdkMeterProvider;
1111//! use prometheus::{Encoder, TextEncoder};
12+ //! use opentelemetry_sdk::metrics::{
13+ //! reader::MetricReader,
14+ //! Temporality,
15+ //! };
1216//!
1317//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
1418//!
2832//! let counter = meter
2933//! .u64_counter("a.counter")
3034//! .with_description("Counts things")
31- //! .init ();
35+ //! .build ();
3236//! let histogram = meter
3337//! .u64_histogram("a.histogram")
3438//! .with_description("Records values")
35- //! .init ();
39+ //! .build ();
3640//!
3741//! counter.add(100, &[KeyValue::new("key", "value")]);
3842//! histogram.record(100, &[KeyValue::new("key", "value")]);
98102
99103use once_cell:: sync:: { Lazy , OnceCell } ;
100104use opentelemetry:: {
101- global,
102- metrics:: { MetricsError , Result } ,
103105 Key , Value ,
106+ otel_error,
107+ InstrumentationScope ,
104108} ;
105109use opentelemetry_sdk:: {
106110 metrics:: {
107- data:: { self , ResourceMetrics , Temporality } ,
108- reader:: { MetricReader , TemporalitySelector } ,
109- InstrumentKind , ManualReader , Pipeline ,
111+ MetricResult ,
112+ data:: { self , ResourceMetrics } ,
113+ reader:: MetricReader ,
114+ Temporality , InstrumentKind ,
115+ ManualReader , Pipeline ,
110116 } ,
111- Resource , Scope ,
117+ Resource
112118} ;
113119use prometheus:: {
114120 core:: Desc ,
@@ -152,28 +158,27 @@ pub struct PrometheusExporter {
152158 reader : Arc < ManualReader > ,
153159}
154160
155- impl TemporalitySelector for PrometheusExporter {
156- /// Note: Prometheus only supports cumulative temporality so this will always be
161+ impl MetricReader for PrometheusExporter {
162+
163+ /// Note: Prometheus only supports cumulative temporality, so this will always be
157164 /// [Temporality::Cumulative].
158- fn temporality ( & self , kind : InstrumentKind ) -> Temporality {
159- self . reader . temporality ( kind )
165+ fn temporality ( & self , _kind : InstrumentKind ) -> Temporality {
166+ Temporality :: Cumulative
160167 }
161- }
162168
163- impl MetricReader for PrometheusExporter {
164169 fn register_pipeline ( & self , pipeline : Weak < Pipeline > ) {
165170 self . reader . register_pipeline ( pipeline)
166171 }
167172
168- fn collect ( & self , rm : & mut ResourceMetrics ) -> Result < ( ) > {
173+ fn collect ( & self , rm : & mut ResourceMetrics ) -> MetricResult < ( ) > {
169174 self . reader . collect ( rm)
170175 }
171176
172- fn force_flush ( & self ) -> Result < ( ) > {
177+ fn force_flush ( & self ) -> MetricResult < ( ) > {
173178 self . reader . force_flush ( )
174179 }
175180
176- fn shutdown ( & self ) -> Result < ( ) > {
181+ fn shutdown ( & self ) -> MetricResult < ( ) > {
177182 self . reader . shutdown ( )
178183 }
179184}
@@ -193,7 +198,7 @@ struct Collector {
193198
194199#[ derive( Default ) ]
195200struct CollectorInner {
196- scope_infos : HashMap < Scope , MetricFamily > ,
201+ scope_infos : HashMap < InstrumentationScope , MetricFamily > ,
197202 metric_families : HashMap < String , MetricFamily > ,
198203}
199204
@@ -281,7 +286,7 @@ impl prometheus::core::Collector for Collector {
281286 let mut inner = match self . inner . lock ( ) {
282287 Ok ( guard) => guard,
283288 Err ( err) => {
284- global :: handle_error ( err) ;
289+ otel_error ! ( name : "inner.lock" , message= format! ( " err: {}" , err ) ) ;
285290 return Vec :: new ( ) ;
286291 }
287292 } ;
@@ -291,7 +296,7 @@ impl prometheus::core::Collector for Collector {
291296 scope_metrics : vec ! [ ] ,
292297 } ;
293298 if let Err ( err) = self . reader . collect ( & mut metrics) {
294- global :: handle_error ( err) ;
299+ otel_error ! ( name : "reader.collect" , message= format! ( " err: {}" , err ) ) ;
295300 return vec ! [ ] ;
296301 }
297302 let mut res = Vec :: with_capacity ( metrics. scope_metrics . len ( ) + 1 ) ;
@@ -311,21 +316,21 @@ impl prometheus::core::Collector for Collector {
311316
312317 for scope_metrics in metrics. scope_metrics {
313318 let scope_labels = if !self . disable_scope_info {
314- if !scope_metrics. scope . attributes . is_empty ( ) {
319+ // field `attributes` of struct `InstrumentationScope` is private field
320+ if scope_metrics. scope . attributes ( ) . count ( ) > 0 {
315321 let scope_info = inner
316322 . scope_infos
317323 . entry ( scope_metrics. scope . clone ( ) )
318324 . or_insert_with_key ( create_scope_info_metric) ;
319325 res. push ( scope_info. clone ( ) ) ;
320326 }
321327
322- let mut labels =
323- Vec :: with_capacity ( 1 + scope_metrics. scope . version . is_some ( ) as usize ) ;
328+ let mut labels = Vec :: with_capacity ( 1 + scope_metrics. scope . version ( ) . is_some ( ) as usize ) ;
324329 let mut name = LabelPair :: new ( ) ;
325330 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
326- name. set_value ( scope_metrics. scope . name . to_string ( ) ) ;
331+ name. set_value ( scope_metrics. scope . name ( ) . to_string ( ) ) ;
327332 labels. push ( name) ;
328- if let Some ( version) = & scope_metrics. scope . version {
333+ if let Some ( version) = & scope_metrics. scope . version ( ) {
329334 let mut l_version = LabelPair :: new ( ) ;
330335 l_version. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
331336 l_version. set_value ( version. to_string ( ) ) ;
@@ -421,11 +426,11 @@ fn validate_metrics(
421426) -> ( bool , Option < String > ) {
422427 if let Some ( existing) = mfs. get ( name) {
423428 if existing. get_field_type ( ) != metric_type {
424- global :: handle_error ( MetricsError :: Other ( format ! ( "Instrument type conflict, using existing type definition. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_field_type( ) , metric_type) ) ) ;
429+ otel_error ! ( name : "validate_metrics.invalid_metric_type" , message = format!( "Instrument type conflict, using existing type definition. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_field_type( ) , metric_type) ) ;
425430 return ( true , None ) ;
426431 }
427432 if existing. get_help ( ) != description {
428- global :: handle_error ( MetricsError :: Other ( format ! ( "Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) , description) ) ) ;
433+ otel_error ! ( name : "validate_metrics.invalid_description" , message = format!( "Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) , description) ) ;
429434 return ( false , Some ( existing. get_help ( ) . to_string ( ) ) ) ;
430435 }
431436 ( false , None )
@@ -578,16 +583,16 @@ fn create_info_metric(
578583 mf
579584}
580585
581- fn create_scope_info_metric ( scope : & Scope ) -> MetricFamily {
586+ fn create_scope_info_metric ( scope : & InstrumentationScope ) -> MetricFamily {
582587 let mut g = prometheus:: proto:: Gauge :: default ( ) ;
583588 g. set_value ( 1.0 ) ;
584589
585- let mut labels = Vec :: with_capacity ( 1 + scope. version . is_some ( ) as usize ) ;
590+ let mut labels = Vec :: with_capacity ( 1 + scope. version ( ) . is_some ( ) as usize ) ;
586591 let mut name = LabelPair :: new ( ) ;
587592 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
588- name. set_value ( scope. name . to_string ( ) ) ;
593+ name. set_value ( scope. name ( ) . to_string ( ) ) ;
589594 labels. push ( name) ;
590- if let Some ( version) = & scope. version {
595+ if let Some ( version) = & scope. version ( ) {
591596 let mut v_label = LabelPair :: new ( ) ;
592597 v_label. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
593598 v_label. set_value ( version. to_string ( ) ) ;
0 commit comments