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")]);
97101#![ cfg_attr( test, deny( warnings) ) ]
98102
99103use once_cell:: sync:: { Lazy , OnceCell } ;
100- use opentelemetry:: {
101- global,
102- metrics:: { MetricsError , Result } ,
103- Key , Value ,
104- } ;
104+ use opentelemetry:: { otel_error, InstrumentationScope , Key , Value } ;
105105use opentelemetry_sdk:: {
106106 metrics:: {
107- data:: { self , ResourceMetrics , Temporality } ,
108- reader:: { MetricReader , TemporalitySelector } ,
109- InstrumentKind , ManualReader , Pipeline ,
107+ data:: { self , ResourceMetrics } ,
108+ reader:: MetricReader ,
109+ InstrumentKind , ManualReader , MetricResult , Pipeline , Temporality ,
110110 } ,
111- Resource , Scope ,
111+ Resource ,
112112} ;
113113use prometheus:: {
114114 core:: Desc ,
@@ -152,28 +152,27 @@ pub struct PrometheusExporter {
152152 reader : Arc < ManualReader > ,
153153}
154154
155- impl TemporalitySelector for PrometheusExporter {
156- /// Note: Prometheus only supports cumulative temporality so this will always be
155+ impl MetricReader for PrometheusExporter {
156+
157+ /// Note: Prometheus only supports cumulative temporality, so this will always be
157158 /// [Temporality::Cumulative].
158- fn temporality ( & self , kind : InstrumentKind ) -> Temporality {
159- self . reader . temporality ( kind )
159+ fn temporality ( & self , _kind : InstrumentKind ) -> Temporality {
160+ Temporality :: Cumulative
160161 }
161- }
162162
163- impl MetricReader for PrometheusExporter {
164163 fn register_pipeline ( & self , pipeline : Weak < Pipeline > ) {
165164 self . reader . register_pipeline ( pipeline)
166165 }
167166
168- fn collect ( & self , rm : & mut ResourceMetrics ) -> Result < ( ) > {
167+ fn collect ( & self , rm : & mut ResourceMetrics ) -> MetricResult < ( ) > {
169168 self . reader . collect ( rm)
170169 }
171170
172- fn force_flush ( & self ) -> Result < ( ) > {
171+ fn force_flush ( & self ) -> MetricResult < ( ) > {
173172 self . reader . force_flush ( )
174173 }
175174
176- fn shutdown ( & self ) -> Result < ( ) > {
175+ fn shutdown ( & self ) -> MetricResult < ( ) > {
177176 self . reader . shutdown ( )
178177 }
179178}
@@ -193,7 +192,7 @@ struct Collector {
193192
194193#[ derive( Default ) ]
195194struct CollectorInner {
196- scope_infos : HashMap < Scope , MetricFamily > ,
195+ scope_infos : HashMap < InstrumentationScope , MetricFamily > ,
197196 metric_families : HashMap < String , MetricFamily > ,
198197}
199198
@@ -281,7 +280,7 @@ impl prometheus::core::Collector for Collector {
281280 let mut inner = match self . inner . lock ( ) {
282281 Ok ( guard) => guard,
283282 Err ( err) => {
284- global :: handle_error ( err) ;
283+ otel_error ! ( name : "inner.lock" , message= format! ( " err: {}" , err ) ) ;
285284 return Vec :: new ( ) ;
286285 }
287286 } ;
@@ -291,7 +290,7 @@ impl prometheus::core::Collector for Collector {
291290 scope_metrics : vec ! [ ] ,
292291 } ;
293292 if let Err ( err) = self . reader . collect ( & mut metrics) {
294- global :: handle_error ( err) ;
293+ otel_error ! ( name : "reader.collect" , message= format! ( " err: {}" , err ) ) ;
295294 return vec ! [ ] ;
296295 }
297296 let mut res = Vec :: with_capacity ( metrics. scope_metrics . len ( ) + 1 ) ;
@@ -311,7 +310,8 @@ impl prometheus::core::Collector for Collector {
311310
312311 for scope_metrics in metrics. scope_metrics {
313312 let scope_labels = if !self . disable_scope_info {
314- if !scope_metrics. scope . attributes . is_empty ( ) {
313+ // field `attributes` of struct `InstrumentationScope` is private field
314+ if scope_metrics. scope . attributes ( ) . count ( ) > 0 {
315315 let scope_info = inner
316316 . scope_infos
317317 . entry ( scope_metrics. scope . clone ( ) )
@@ -320,12 +320,12 @@ impl prometheus::core::Collector for Collector {
320320 }
321321
322322 let mut labels =
323- Vec :: with_capacity ( 1 + scope_metrics. scope . version . is_some ( ) as usize ) ;
323+ Vec :: with_capacity ( 1 + scope_metrics. scope . version ( ) . is_some ( ) as usize ) ;
324324 let mut name = LabelPair :: new ( ) ;
325325 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
326- name. set_value ( scope_metrics. scope . name . to_string ( ) ) ;
326+ name. set_value ( scope_metrics. scope . name ( ) . to_string ( ) ) ;
327327 labels. push ( name) ;
328- if let Some ( version) = & scope_metrics. scope . version {
328+ if let Some ( version) = & scope_metrics. scope . version ( ) {
329329 let mut l_version = LabelPair :: new ( ) ;
330330 l_version. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
331331 l_version. set_value ( version. to_string ( ) ) ;
@@ -421,11 +421,11 @@ fn validate_metrics(
421421) -> ( bool , Option < String > ) {
422422 if let Some ( existing) = mfs. get ( name) {
423423 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) ) ) ;
424+ 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) ) ;
425425 return ( true , None ) ;
426426 }
427427 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) ) ) ;
428+ otel_error ! ( name : "validate_metrics.invalid_description" , message = format!( "Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) , description) ) ;
429429 return ( false , Some ( existing. get_help ( ) . to_string ( ) ) ) ;
430430 }
431431 ( false , None )
@@ -578,16 +578,16 @@ fn create_info_metric(
578578 mf
579579}
580580
581- fn create_scope_info_metric ( scope : & Scope ) -> MetricFamily {
581+ fn create_scope_info_metric ( scope : & InstrumentationScope ) -> MetricFamily {
582582 let mut g = prometheus:: proto:: Gauge :: default ( ) ;
583583 g. set_value ( 1.0 ) ;
584584
585- let mut labels = Vec :: with_capacity ( 1 + scope. version . is_some ( ) as usize ) ;
585+ let mut labels = Vec :: with_capacity ( 1 + scope. version ( ) . is_some ( ) as usize ) ;
586586 let mut name = LabelPair :: new ( ) ;
587587 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
588- name. set_value ( scope. name . to_string ( ) ) ;
588+ name. set_value ( scope. name ( ) . to_string ( ) ) ;
589589 labels. push ( name) ;
590- if let Some ( version) = & scope. version {
590+ if let Some ( version) = & scope. version ( ) {
591591 let mut v_label = LabelPair :: new ( ) ;
592592 v_label. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
593593 v_label. set_value ( version. to_string ( ) ) ;
0 commit comments