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,25 @@ 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
157- /// [Temporality::Cumulative].
161+ impl MetricReader for PrometheusExporter {
162+ // fn temporality(&self, kind: InstrumentKind) -> Temporality;
158163 fn temporality ( & self , kind : InstrumentKind ) -> Temporality {
159164 self . reader . temporality ( kind)
160165 }
161- }
162166
163- impl MetricReader for PrometheusExporter {
164167 fn register_pipeline ( & self , pipeline : Weak < Pipeline > ) {
165168 self . reader . register_pipeline ( pipeline)
166169 }
167170
168- fn collect ( & self , rm : & mut ResourceMetrics ) -> Result < ( ) > {
171+ fn collect ( & self , rm : & mut ResourceMetrics ) -> MetricResult < ( ) > {
169172 self . reader . collect ( rm)
170173 }
171174
172- fn force_flush ( & self ) -> Result < ( ) > {
175+ fn force_flush ( & self ) -> MetricResult < ( ) > {
173176 self . reader . force_flush ( )
174177 }
175178
176- fn shutdown ( & self ) -> Result < ( ) > {
179+ fn shutdown ( & self ) -> MetricResult < ( ) > {
177180 self . reader . shutdown ( )
178181 }
179182}
@@ -193,7 +196,7 @@ struct Collector {
193196
194197#[ derive( Default ) ]
195198struct CollectorInner {
196- scope_infos : HashMap < Scope , MetricFamily > ,
199+ scope_infos : HashMap < InstrumentationScope , MetricFamily > ,
197200 metric_families : HashMap < String , MetricFamily > ,
198201}
199202
@@ -281,7 +284,7 @@ impl prometheus::core::Collector for Collector {
281284 let mut inner = match self . inner . lock ( ) {
282285 Ok ( guard) => guard,
283286 Err ( err) => {
284- global :: handle_error ( err) ;
287+ otel_error ! ( name : "inner.lock" , message= format! ( " err: {}" , err ) ) ;
285288 return Vec :: new ( ) ;
286289 }
287290 } ;
@@ -291,7 +294,7 @@ impl prometheus::core::Collector for Collector {
291294 scope_metrics : vec ! [ ] ,
292295 } ;
293296 if let Err ( err) = self . reader . collect ( & mut metrics) {
294- global :: handle_error ( err) ;
297+ otel_error ! ( name : "reader.collect" , message= format! ( " err: {}" , err ) ) ;
295298 return vec ! [ ] ;
296299 }
297300 let mut res = Vec :: with_capacity ( metrics. scope_metrics . len ( ) + 1 ) ;
@@ -311,21 +314,21 @@ impl prometheus::core::Collector for Collector {
311314
312315 for scope_metrics in metrics. scope_metrics {
313316 let scope_labels = if !self . disable_scope_info {
314- if !scope_metrics. scope . attributes . is_empty ( ) {
317+ // field `attributes` of struct `InstrumentationScope` is private field
318+ if scope_metrics. scope . attributes ( ) . count ( ) > 0 {
315319 let scope_info = inner
316320 . scope_infos
317321 . entry ( scope_metrics. scope . clone ( ) )
318322 . or_insert_with_key ( create_scope_info_metric) ;
319323 res. push ( scope_info. clone ( ) ) ;
320324 }
321325
322- let mut labels =
323- Vec :: with_capacity ( 1 + scope_metrics. scope . version . is_some ( ) as usize ) ;
326+ let mut labels = Vec :: with_capacity ( 1 + scope_metrics. scope . version ( ) . is_some ( ) as usize ) ;
324327 let mut name = LabelPair :: new ( ) ;
325328 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
326- name. set_value ( scope_metrics. scope . name . to_string ( ) ) ;
329+ name. set_value ( scope_metrics. scope . name ( ) . to_string ( ) ) ;
327330 labels. push ( name) ;
328- if let Some ( version) = & scope_metrics. scope . version {
331+ if let Some ( version) = & scope_metrics. scope . version ( ) {
329332 let mut l_version = LabelPair :: new ( ) ;
330333 l_version. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
331334 l_version. set_value ( version. to_string ( ) ) ;
@@ -421,11 +424,11 @@ fn validate_metrics(
421424) -> ( bool , Option < String > ) {
422425 if let Some ( existing) = mfs. get ( name) {
423426 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) ) ) ;
427+ 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) ) ;
425428 return ( true , None ) ;
426429 }
427430 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) ) ) ;
431+ otel_error ! ( name : "validate_metrics.invalid_description" , message = format!( "Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) , description) ) ;
429432 return ( false , Some ( existing. get_help ( ) . to_string ( ) ) ) ;
430433 }
431434 ( false , None )
@@ -578,16 +581,16 @@ fn create_info_metric(
578581 mf
579582}
580583
581- fn create_scope_info_metric ( scope : & Scope ) -> MetricFamily {
584+ fn create_scope_info_metric ( scope : & InstrumentationScope ) -> MetricFamily {
582585 let mut g = prometheus:: proto:: Gauge :: default ( ) ;
583586 g. set_value ( 1.0 ) ;
584587
585- let mut labels = Vec :: with_capacity ( 1 + scope. version . is_some ( ) as usize ) ;
588+ let mut labels = Vec :: with_capacity ( 1 + scope. version ( ) . is_some ( ) as usize ) ;
586589 let mut name = LabelPair :: new ( ) ;
587590 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
588- name. set_value ( scope. name . to_string ( ) ) ;
591+ name. set_value ( scope. name ( ) . to_string ( ) ) ;
589592 labels. push ( name) ;
590- if let Some ( version) = & scope. version {
593+ if let Some ( version) = & scope. version ( ) {
591594 let mut v_label = LabelPair :: new ( ) ;
592595 v_label. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
593596 v_label. set_value ( version. to_string ( ) ) ;
0 commit comments