@@ -2,11 +2,7 @@ use std::{collections::HashMap, fmt, sync::RwLock};
2
2
use tracing:: { field:: Visit , Subscriber } ;
3
3
use tracing_core:: Field ;
4
4
5
- use opentelemetry:: {
6
- metrics:: { Counter , Histogram , Meter , MeterProvider , UpDownCounter } ,
7
- sdk:: metrics:: controllers:: BasicController ,
8
- Context as OtelContext ,
9
- } ;
5
+ use opentelemetry:: metrics:: { Counter , Histogram , Meter , MeterProvider , UpDownCounter } ;
10
6
use tracing_subscriber:: { layer:: Context , registry:: LookupSpan , Layer } ;
11
7
12
8
const CARGO_PKG_VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
@@ -44,7 +40,6 @@ pub(crate) enum InstrumentType {
44
40
impl Instruments {
45
41
pub ( crate ) fn update_metric (
46
42
& self ,
47
- cx : & OtelContext ,
48
43
meter : & Meter ,
49
44
instrument_type : InstrumentType ,
50
45
metric_name : & ' static str ,
@@ -78,55 +73,55 @@ impl Instruments {
78
73
& self . u64_counter ,
79
74
metric_name,
80
75
|| meter. u64_counter ( metric_name) . init ( ) ,
81
- |ctr| ctr. add ( cx , value, & [ ] ) ,
76
+ |ctr| ctr. add ( value, & [ ] ) ,
82
77
) ;
83
78
}
84
79
InstrumentType :: CounterF64 ( value) => {
85
80
update_or_insert (
86
81
& self . f64_counter ,
87
82
metric_name,
88
83
|| meter. f64_counter ( metric_name) . init ( ) ,
89
- |ctr| ctr. add ( cx , value, & [ ] ) ,
84
+ |ctr| ctr. add ( value, & [ ] ) ,
90
85
) ;
91
86
}
92
87
InstrumentType :: UpDownCounterI64 ( value) => {
93
88
update_or_insert (
94
89
& self . i64_up_down_counter ,
95
90
metric_name,
96
91
|| meter. i64_up_down_counter ( metric_name) . init ( ) ,
97
- |ctr| ctr. add ( cx , value, & [ ] ) ,
92
+ |ctr| ctr. add ( value, & [ ] ) ,
98
93
) ;
99
94
}
100
95
InstrumentType :: UpDownCounterF64 ( value) => {
101
96
update_or_insert (
102
97
& self . f64_up_down_counter ,
103
98
metric_name,
104
99
|| meter. f64_up_down_counter ( metric_name) . init ( ) ,
105
- |ctr| ctr. add ( cx , value, & [ ] ) ,
100
+ |ctr| ctr. add ( value, & [ ] ) ,
106
101
) ;
107
102
}
108
103
InstrumentType :: HistogramU64 ( value) => {
109
104
update_or_insert (
110
105
& self . u64_histogram ,
111
106
metric_name,
112
107
|| meter. u64_histogram ( metric_name) . init ( ) ,
113
- |rec| rec. record ( cx , value, & [ ] ) ,
108
+ |rec| rec. record ( value, & [ ] ) ,
114
109
) ;
115
110
}
116
111
InstrumentType :: HistogramI64 ( value) => {
117
112
update_or_insert (
118
113
& self . i64_histogram ,
119
114
metric_name,
120
115
|| meter. i64_histogram ( metric_name) . init ( ) ,
121
- |rec| rec. record ( cx , value, & [ ] ) ,
116
+ |rec| rec. record ( value, & [ ] ) ,
122
117
) ;
123
118
}
124
119
InstrumentType :: HistogramF64 ( value) => {
125
120
update_or_insert (
126
121
& self . f64_histogram ,
127
122
metric_name,
128
123
|| meter. f64_histogram ( metric_name) . init ( ) ,
129
- |rec| rec. record ( cx , value, & [ ] ) ,
124
+ |rec| rec. record ( value, & [ ] ) ,
130
125
) ;
131
126
}
132
127
} ;
@@ -144,18 +139,15 @@ impl<'a> Visit for MetricVisitor<'a> {
144
139
}
145
140
146
141
fn record_u64 ( & mut self , field : & Field , value : u64 ) {
147
- let cx = OtelContext :: current ( ) ;
148
142
if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_MONOTONIC_COUNTER ) {
149
143
self . instruments . update_metric (
150
- & cx,
151
144
self . meter ,
152
145
InstrumentType :: CounterU64 ( value) ,
153
146
metric_name,
154
147
) ;
155
148
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_COUNTER ) {
156
149
if value <= I64_MAX {
157
150
self . instruments . update_metric (
158
- & cx,
159
151
self . meter ,
160
152
InstrumentType :: UpDownCounterI64 ( value as i64 ) ,
161
153
metric_name,
@@ -170,7 +162,6 @@ impl<'a> Visit for MetricVisitor<'a> {
170
162
}
171
163
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_HISTOGRAM ) {
172
164
self . instruments . update_metric (
173
- & cx,
174
165
self . meter ,
175
166
InstrumentType :: HistogramU64 ( value) ,
176
167
metric_name,
@@ -179,24 +170,20 @@ impl<'a> Visit for MetricVisitor<'a> {
179
170
}
180
171
181
172
fn record_f64 ( & mut self , field : & Field , value : f64 ) {
182
- let cx = OtelContext :: current ( ) ;
183
173
if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_MONOTONIC_COUNTER ) {
184
174
self . instruments . update_metric (
185
- & cx,
186
175
self . meter ,
187
176
InstrumentType :: CounterF64 ( value) ,
188
177
metric_name,
189
178
) ;
190
179
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_COUNTER ) {
191
180
self . instruments . update_metric (
192
- & cx,
193
181
self . meter ,
194
182
InstrumentType :: UpDownCounterF64 ( value) ,
195
183
metric_name,
196
184
) ;
197
185
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_HISTOGRAM ) {
198
186
self . instruments . update_metric (
199
- & cx,
200
187
self . meter ,
201
188
InstrumentType :: HistogramF64 ( value) ,
202
189
metric_name,
@@ -205,24 +192,20 @@ impl<'a> Visit for MetricVisitor<'a> {
205
192
}
206
193
207
194
fn record_i64 ( & mut self , field : & Field , value : i64 ) {
208
- let cx = OtelContext :: current ( ) ;
209
195
if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_MONOTONIC_COUNTER ) {
210
196
self . instruments . update_metric (
211
- & cx,
212
197
self . meter ,
213
198
InstrumentType :: CounterU64 ( value as u64 ) ,
214
199
metric_name,
215
200
) ;
216
201
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_COUNTER ) {
217
202
self . instruments . update_metric (
218
- & cx,
219
203
self . meter ,
220
204
InstrumentType :: UpDownCounterI64 ( value) ,
221
205
metric_name,
222
206
) ;
223
207
} else if let Some ( metric_name) = field. name ( ) . strip_prefix ( METRIC_PREFIX_HISTOGRAM ) {
224
208
self . instruments . update_metric (
225
- & cx,
226
209
self . meter ,
227
210
InstrumentType :: HistogramI64 ( value) ,
228
211
metric_name,
@@ -246,14 +229,14 @@ impl<'a> Visit for MetricVisitor<'a> {
246
229
/// use tracing_opentelemetry::MetricsLayer;
247
230
/// use tracing_subscriber::layer::SubscriberExt;
248
231
/// use tracing_subscriber::Registry;
249
- /// # use opentelemetry::sdk::metrics::controllers::BasicController ;
232
+ /// # use opentelemetry::sdk::metrics::MeterProvider ;
250
233
///
251
- /// // Constructing a BasicController is out-of-scope for the docs here, but there
234
+ /// // Constructing a MeterProvider is out-of-scope for the docs here, but there
252
235
/// // are examples in the opentelemetry repository. See:
253
- /// // https://github.com/open-telemetry/opentelemetry-rust/blob/d4b9befea04bcc7fc19319a6ebf5b5070131c486 /examples/basic-otlp /src/main.rs#L35-L52
254
- /// # let controller: BasicController = unimplemented!();
236
+ /// // https://github.com/open-telemetry/opentelemetry-rust/blob/dfeac078ff7853e7dc814778524b93470dfa5c9c /examples/metrics-basic /src/main.rs#L7
237
+ /// # let meter_provider: MeterProvider = unimplemented!();
255
238
///
256
- /// let opentelemetry_metrics = MetricsLayer::new(controller );
239
+ /// let opentelemetry_metrics = MetricsLayer::new(meter_provider );
257
240
/// let subscriber = Registry::default().with(opentelemetry_metrics);
258
241
/// tracing::subscriber::set_global_default(subscriber).unwrap();
259
242
/// ```
@@ -343,9 +326,16 @@ pub struct MetricsLayer {
343
326
344
327
impl MetricsLayer {
345
328
/// Create a new instance of MetricsLayer.
346
- pub fn new ( controller : BasicController ) -> Self {
347
- let meter =
348
- controller. versioned_meter ( INSTRUMENTATION_LIBRARY_NAME , Some ( CARGO_PKG_VERSION ) , None ) ;
329
+ pub fn new < M > ( meter_provider : M ) -> Self
330
+ where
331
+ M : MeterProvider ,
332
+ {
333
+ let meter = meter_provider. versioned_meter (
334
+ INSTRUMENTATION_LIBRARY_NAME ,
335
+ Some ( CARGO_PKG_VERSION ) ,
336
+ None :: < & ' static str > ,
337
+ None ,
338
+ ) ;
349
339
MetricsLayer {
350
340
meter,
351
341
instruments : Default :: default ( ) ,
0 commit comments