28
28
//! let counter = meter
29
29
//! .u64_counter("a.counter")
30
30
//! .with_description("Counts things")
31
- //! .init ();
31
+ //! .build ();
32
32
//! let histogram = meter
33
33
//! .u64_histogram("a.histogram")
34
34
//! .with_description("Records values")
35
- //! .init ();
35
+ //! .build ();
36
36
//!
37
37
//! counter.add(100, &[KeyValue::new("key", "value")]);
38
38
//! histogram.record(100, &[KeyValue::new("key", "value")]);
97
97
#![ cfg_attr( test, deny( warnings) ) ]
98
98
99
99
use once_cell:: sync:: { Lazy , OnceCell } ;
100
- use opentelemetry:: {
101
- global,
102
- metrics:: { MetricsError , Result } ,
103
- Key , Value ,
104
- } ;
100
+ use opentelemetry:: { otel_error, otel_warn, InstrumentationScope , Key , Value } ;
105
101
use opentelemetry_sdk:: {
106
102
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 ,
110
106
} ,
111
- Resource , Scope ,
107
+ Resource ,
112
108
} ;
113
109
use prometheus:: {
114
110
core:: Desc ,
@@ -152,30 +148,28 @@ pub struct PrometheusExporter {
152
148
reader : Arc < ManualReader > ,
153
149
}
154
150
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
-
163
151
impl MetricReader for PrometheusExporter {
164
152
fn register_pipeline ( & self , pipeline : Weak < Pipeline > ) {
165
153
self . reader . register_pipeline ( pipeline)
166
154
}
167
155
168
- fn collect ( & self , rm : & mut ResourceMetrics ) -> Result < ( ) > {
156
+ fn collect ( & self , rm : & mut ResourceMetrics ) -> MetricResult < ( ) > {
169
157
self . reader . collect ( rm)
170
158
}
171
159
172
- fn force_flush ( & self ) -> Result < ( ) > {
160
+ fn force_flush ( & self ) -> MetricResult < ( ) > {
173
161
self . reader . force_flush ( )
174
162
}
175
163
176
- fn shutdown ( & self ) -> Result < ( ) > {
164
+ fn shutdown ( & self ) -> MetricResult < ( ) > {
177
165
self . reader . shutdown ( )
178
166
}
167
+
168
+ /// Note: Prometheus only supports cumulative temporality, so this will always be
169
+ /// [Temporality::Cumulative].
170
+ fn temporality ( & self , _kind : InstrumentKind ) -> Temporality {
171
+ Temporality :: Cumulative
172
+ }
179
173
}
180
174
181
175
struct Collector {
@@ -193,7 +187,7 @@ struct Collector {
193
187
194
188
#[ derive( Default ) ]
195
189
struct CollectorInner {
196
- scope_infos : HashMap < Scope , MetricFamily > ,
190
+ scope_infos : HashMap < InstrumentationScope , MetricFamily > ,
197
191
metric_families : HashMap < String , MetricFamily > ,
198
192
}
199
193
@@ -281,7 +275,10 @@ impl prometheus::core::Collector for Collector {
281
275
let mut inner = match self . inner . lock ( ) {
282
276
Ok ( guard) => guard,
283
277
Err ( err) => {
284
- global:: handle_error ( err) ;
278
+ otel_error ! (
279
+ name: "MetricScrapeFailed" ,
280
+ message = err. to_string( ) ,
281
+ ) ;
285
282
return Vec :: new ( ) ;
286
283
}
287
284
} ;
@@ -291,7 +288,10 @@ impl prometheus::core::Collector for Collector {
291
288
scope_metrics : vec ! [ ] ,
292
289
} ;
293
290
if let Err ( err) = self . reader . collect ( & mut metrics) {
294
- global:: handle_error ( err) ;
291
+ otel_error ! (
292
+ name: "MetricScrapeFailed" ,
293
+ message = err. to_string( ) ,
294
+ ) ;
295
295
return vec ! [ ] ;
296
296
}
297
297
let mut res = Vec :: with_capacity ( metrics. scope_metrics . len ( ) + 1 ) ;
@@ -311,7 +311,7 @@ impl prometheus::core::Collector for Collector {
311
311
312
312
for scope_metrics in metrics. scope_metrics {
313
313
let scope_labels = if !self . disable_scope_info {
314
- if ! scope_metrics. scope . attributes . is_empty ( ) {
314
+ if scope_metrics. scope . attributes ( ) . count ( ) > 0 {
315
315
let scope_info = inner
316
316
. scope_infos
317
317
. entry ( scope_metrics. scope . clone ( ) )
@@ -320,12 +320,12 @@ impl prometheus::core::Collector for Collector {
320
320
}
321
321
322
322
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 ) ;
324
324
let mut name = LabelPair :: new ( ) ;
325
325
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 ( ) ) ;
327
327
labels. push ( name) ;
328
- if let Some ( version) = & scope_metrics. scope . version {
328
+ if let Some ( version) = & scope_metrics. scope . version ( ) {
329
329
let mut l_version = LabelPair :: new ( ) ;
330
330
l_version. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
331
331
l_version. set_value ( version. to_string ( ) ) ;
@@ -421,11 +421,19 @@ fn validate_metrics(
421
421
) -> ( bool , Option < String > ) {
422
422
if let Some ( existing) = mfs. get ( name) {
423
423
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_warn ! (
425
+ name: "MetricValidationFailed" ,
426
+ message = "Instrument type conflict, using existing type definition" ,
427
+ metric_type = format!( "Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_field_type( ) , metric_type) . as_str( ) ,
428
+ ) ;
425
429
return ( true , None ) ;
426
430
}
427
431
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) ) ) ;
432
+ otel_warn ! (
433
+ name: "MetricValidationFailed" ,
434
+ message = "Instrument description conflict, using existing" ,
435
+ metric_description = format!( "Instrument {name}, Existing: {:?}, dropped: {:?}" , existing. get_help( ) . to_string( ) , description. to_string( ) ) . as_str( ) ,
436
+ ) ;
429
437
return ( false , Some ( existing. get_help ( ) . to_string ( ) ) ) ;
430
438
}
431
439
( false , None )
@@ -578,16 +586,16 @@ fn create_info_metric(
578
586
mf
579
587
}
580
588
581
- fn create_scope_info_metric ( scope : & Scope ) -> MetricFamily {
589
+ fn create_scope_info_metric ( scope : & InstrumentationScope ) -> MetricFamily {
582
590
let mut g = prometheus:: proto:: Gauge :: default ( ) ;
583
591
g. set_value ( 1.0 ) ;
584
592
585
- let mut labels = Vec :: with_capacity ( 1 + scope. version . is_some ( ) as usize ) ;
593
+ let mut labels = Vec :: with_capacity ( 1 + scope. version ( ) . is_some ( ) as usize ) ;
586
594
let mut name = LabelPair :: new ( ) ;
587
595
name. set_name ( SCOPE_INFO_KEYS [ 0 ] . into ( ) ) ;
588
- name. set_value ( scope. name . to_string ( ) ) ;
596
+ name. set_value ( scope. name ( ) . to_string ( ) ) ;
589
597
labels. push ( name) ;
590
- if let Some ( version) = & scope. version {
598
+ if let Some ( version) = & scope. version ( ) {
591
599
let mut v_label = LabelPair :: new ( ) ;
592
600
v_label. set_name ( SCOPE_INFO_KEYS [ 1 ] . into ( ) ) ;
593
601
v_label. set_value ( version. to_string ( ) ) ;
0 commit comments