2828//! let counter = meter
2929//! .u64_counter("a.counter")
3030//! .with_description("Counts things")
31- //! .init ();
31+ //! .build ();
3232//! let histogram = meter
3333//! .u64_histogram("a.histogram")
3434//! .with_description("Records values")
35- //! .init ();
35+ //! .build ();
3636//!
3737//! counter.add(100, &[KeyValue::new("key", "value")]);
3838//! histogram.record(100, &[KeyValue::new("key", "value")]);
9797#![ cfg_attr( test, deny( warnings) ) ]
9898
9999use once_cell:: sync:: { Lazy , OnceCell } ;
100- use opentelemetry:: {
101- global,
102- metrics:: { MetricsError , Result } ,
103- Key , Value ,
104- } ;
100+ use opentelemetry:: { otel_error, InstrumentationScope , Key , Value } ;
105101use opentelemetry_sdk:: {
106102 metrics:: {
107- data:: { self , ResourceMetrics , Temporality } ,
108- reader:: { MetricReader , TemporalitySelector } ,
109- InstrumentKind , ManualReader , Pipeline ,
103+ data:: { self , ResourceMetrics } ,
104+ reader:: MetricReader ,
105+ InstrumentKind , ManualReader , MetricResult , Pipeline , Temporality ,
110106 } ,
111- Resource , Scope ,
107+ Resource ,
112108} ;
113109use prometheus:: {
114110 core:: Desc ,
@@ -152,30 +148,26 @@ pub struct PrometheusExporter {
152148 reader : Arc < ManualReader > ,
153149}
154150
155- impl TemporalitySelector for PrometheusExporter {
156- /// Note: Prometheus only supports cumulative temporality so this will always be
157- /// [Temporality::Cumulative].
158- fn temporality ( & self , kind : InstrumentKind ) -> Temporality {
159- self . reader . temporality ( kind)
160- }
161- }
162-
163151impl MetricReader for PrometheusExporter {
164152 fn register_pipeline ( & self , pipeline : Weak < Pipeline > ) {
165153 self . reader . register_pipeline ( pipeline)
166154 }
167155
168- fn collect ( & self , rm : & mut ResourceMetrics ) -> Result < ( ) > {
156+ fn collect ( & self , rm : & mut ResourceMetrics ) -> MetricResult < ( ) > {
169157 self . reader . collect ( rm)
170158 }
171159
172- fn force_flush ( & self ) -> Result < ( ) > {
160+ fn force_flush ( & self ) -> MetricResult < ( ) > {
173161 self . reader . force_flush ( )
174162 }
175163
176- fn shutdown ( & self ) -> Result < ( ) > {
164+ fn shutdown ( & self ) -> MetricResult < ( ) > {
177165 self . reader . shutdown ( )
178166 }
167+
168+ fn temporality ( & self , kind : InstrumentKind ) -> Temporality {
169+ self . reader . temporality ( kind)
170+ }
179171}
180172
181173struct Collector {
@@ -193,7 +185,7 @@ struct Collector {
193185
194186#[ derive( Default ) ]
195187struct CollectorInner {
196- scope_infos : HashMap < Scope , MetricFamily > ,
188+ scope_infos : HashMap < InstrumentationScope , MetricFamily > ,
197189 metric_families : HashMap < String , MetricFamily > ,
198190}
199191
@@ -281,7 +273,10 @@ impl prometheus::core::Collector for Collector {
281273 let mut inner = match self . inner . lock ( ) {
282274 Ok ( guard) => guard,
283275 Err ( err) => {
284- global:: handle_error ( err) ;
276+ otel_error ! (
277+ name: "OpenTelemetry error occurred." ,
278+ message = err. to_string( ) ,
279+ ) ;
285280 return Vec :: new ( ) ;
286281 }
287282 } ;
@@ -291,7 +286,10 @@ impl prometheus::core::Collector for Collector {
291286 scope_metrics : vec ! [ ] ,
292287 } ;
293288 if let Err ( err) = self . reader . collect ( & mut metrics) {
294- global:: handle_error ( err) ;
289+ otel_error ! (
290+ name: "OpenTelemetry metrics error occurred." ,
291+ message = err. to_string( ) ,
292+ ) ;
295293 return vec ! [ ] ;
296294 }
297295 let mut res = Vec :: with_capacity ( metrics. scope_metrics . len ( ) + 1 ) ;
@@ -311,7 +309,7 @@ impl prometheus::core::Collector for Collector {
311309
312310 for scope_metrics in metrics. scope_metrics {
313311 let scope_labels = if !self . disable_scope_info {
314- if ! scope_metrics. scope . attributes . is_empty ( ) {
312+ if scope_metrics. scope . attributes ( ) . count ( ) > 0 {
315313 let scope_info = inner
316314 . scope_infos
317315 . entry ( scope_metrics. scope . clone ( ) )
@@ -320,12 +318,12 @@ impl prometheus::core::Collector for Collector {
320318 }
321319
322320 let mut labels =
323- Vec :: with_capacity ( 1 + scope_metrics. scope . version . is_some ( ) as usize ) ;
321+ Vec :: with_capacity ( 1 + scope_metrics. scope . version ( ) . is_some ( ) as usize ) ;
324322 let mut name = LabelPair :: new ( ) ;
325323 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
326- name. set_value ( scope_metrics. scope . name . to_string ( ) ) ;
324+ name. set_value ( scope_metrics. scope . name ( ) . to_string ( ) ) ;
327325 labels. push ( name) ;
328- if let Some ( version) = & scope_metrics. scope . version {
326+ if let Some ( version) = & scope_metrics. scope . version ( ) {
329327 let mut l_version = LabelPair :: new ( ) ;
330328 l_version. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
331329 l_version. set_value ( version. to_string ( ) ) ;
@@ -421,11 +419,17 @@ fn validate_metrics(
421419) -> ( bool , Option < String > ) {
422420 if let Some ( existing) = mfs. get ( name) {
423421 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) ) ) ;
422+ otel_error ! (
423+ name: "OpenTelemetry metrics error occurred." ,
424+ message = format!( "Instrument type conflict, using existing type definition. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_field_type( ) , metric_type) . as_str( ) ,
425+ ) ;
425426 return ( true , None ) ;
426427 }
427428 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) ) ) ;
429+ otel_error ! (
430+ name: "OpenTelemetry metrics error occurred." ,
431+ message = format!( "Instrument description conflict, using existing. Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) . to_string( ) , description. to_string( ) ) . as_str( ) ,
432+ ) ;
429433 return ( false , Some ( existing. get_help ( ) . to_string ( ) ) ) ;
430434 }
431435 ( false , None )
@@ -578,16 +582,16 @@ fn create_info_metric(
578582 mf
579583}
580584
581- fn create_scope_info_metric ( scope : & Scope ) -> MetricFamily {
585+ fn create_scope_info_metric ( scope : & InstrumentationScope ) -> MetricFamily {
582586 let mut g = prometheus:: proto:: Gauge :: default ( ) ;
583587 g. set_value ( 1.0 ) ;
584588
585- let mut labels = Vec :: with_capacity ( 1 + scope. version . is_some ( ) as usize ) ;
589+ let mut labels = Vec :: with_capacity ( 1 + scope. version ( ) . is_some ( ) as usize ) ;
586590 let mut name = LabelPair :: new ( ) ;
587591 name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
588- name. set_value ( scope. name . to_string ( ) ) ;
592+ name. set_value ( scope. name ( ) . to_string ( ) ) ;
589593 labels. push ( name) ;
590- if let Some ( version) = & scope. version {
594+ if let Some ( version) = & scope. version ( ) {
591595 let mut v_label = LabelPair :: new ( ) ;
592596 v_label. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
593597 v_label. set_value ( version. to_string ( ) ) ;
0 commit comments