Skip to content

Commit 764cd73

Browse files
authored
fix(metrics)!: hold onto MetricsProvider in MetricsLayer (#224)
## Motivation Fixes #178 Dropping `MeterProvider` early shuts down the `Meter` we carry aroud forcing users wither to keep a copy of the provider for the duration of their program or losing all their metrics. ## Solution Keep the provider inside our layer. It adds a new generic argument to the type which is a breaking change. The tests were written in a way that prevented this bug by having the provider cloned so I've got rid of that too.
1 parent fd0a58a commit 764cd73

File tree

2 files changed

+30
-52
lines changed

2 files changed

+30
-52
lines changed

src/metrics.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,19 @@ impl Visit for MetricVisitor<'_> {
368368
///
369369
/// In the future, this can be improved by associating each `Metric` instance to
370370
/// its callsite, eliminating the need for any maps.
371-
///
372371
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
373-
pub struct MetricsLayer<S> {
372+
pub struct MetricsLayer<S, M> {
374373
inner: Filtered<InstrumentLayer, MetricsFilter, S>,
374+
// We need to hold onto this so that the `InstrumentLayer` can use the created `Meter`.
375+
_meter_provider: M,
375376
}
376377

377-
impl<S> MetricsLayer<S>
378+
impl<S, M> MetricsLayer<S, M>
378379
where
379380
S: Subscriber + for<'span> LookupSpan<'span>,
380381
{
381382
/// Create a new instance of MetricsLayer.
382-
pub fn new<M>(meter_provider: M) -> MetricsLayer<S>
383+
pub fn new(meter_provider: M) -> MetricsLayer<S, M>
383384
where
384385
M: MeterProvider,
385386
{
@@ -396,6 +397,7 @@ where
396397

397398
MetricsLayer {
398399
inner: layer.with_filter(MetricsFilter),
400+
_meter_provider: meter_provider,
399401
}
400402
}
401403
}
@@ -467,7 +469,7 @@ where
467469
}
468470
}
469471

470-
impl<S> Layer<S> for MetricsLayer<S>
472+
impl<S, M: 'static> Layer<S> for MetricsLayer<S, M>
471473
where
472474
S: Subscriber + for<'span> LookupSpan<'span>,
473475
{

tests/metrics_publishing.rs

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use opentelemetry_sdk::{
44
metrics::{
55
data::{self, Gauge, Histogram, Sum},
66
reader::MetricReader,
7-
InstrumentKind, ManualReader, MeterProviderBuilder, SdkMeterProvider,
7+
InstrumentKind, ManualReader, MeterProviderBuilder,
88
},
99
};
1010

@@ -27,9 +27,8 @@ async fn u64_counter_is_exported() {
2727

2828
tracing::subscriber::with_default(subscriber, || {
2929
tracing::info!(monotonic_counter.hello_world = 1_u64);
30+
exporter.export().unwrap();
3031
});
31-
32-
exporter.export().unwrap();
3332
}
3433

3534
#[tokio::test]
@@ -43,9 +42,8 @@ async fn u64_counter_is_exported_i64_at_instrumentation_point() {
4342

4443
tracing::subscriber::with_default(subscriber, || {
4544
tracing::info!(monotonic_counter.hello_world2 = 1_i64);
45+
exporter.export().unwrap();
4646
});
47-
48-
exporter.export().unwrap();
4947
}
5048

5149
#[tokio::test]
@@ -59,9 +57,8 @@ async fn f64_counter_is_exported() {
5957

6058
tracing::subscriber::with_default(subscriber, || {
6159
tracing::info!(monotonic_counter.float_hello_world = 1.000000123_f64);
60+
exporter.export().unwrap();
6261
});
63-
64-
exporter.export().unwrap();
6562
}
6663

6764
#[tokio::test]
@@ -75,9 +72,8 @@ async fn i64_up_down_counter_is_exported() {
7572

7673
tracing::subscriber::with_default(subscriber, || {
7774
tracing::info!(counter.pebcak = -5_i64);
75+
exporter.export().unwrap();
7876
});
79-
80-
exporter.export().unwrap();
8177
}
8278

8379
#[tokio::test]
@@ -91,9 +87,8 @@ async fn i64_up_down_counter_is_exported_u64_at_instrumentation_point() {
9187

9288
tracing::subscriber::with_default(subscriber, || {
9389
tracing::info!(counter.pebcak2 = 5_u64);
90+
exporter.export().unwrap();
9491
});
95-
96-
exporter.export().unwrap();
9792
}
9893

9994
#[tokio::test]
@@ -107,9 +102,8 @@ async fn f64_up_down_counter_is_exported() {
107102

108103
tracing::subscriber::with_default(subscriber, || {
109104
tracing::info!(counter.pebcak_blah = 99.123_f64);
105+
exporter.export().unwrap();
110106
});
111-
112-
exporter.export().unwrap();
113107
}
114108

115109
#[tokio::test]
@@ -120,9 +114,8 @@ async fn u64_gauge_is_exported() {
120114
tracing::subscriber::with_default(subscriber, || {
121115
tracing::info!(gauge.gygygy = 1_u64);
122116
tracing::info!(gauge.gygygy = 2_u64);
117+
exporter.export().unwrap();
123118
});
124-
125-
exporter.export().unwrap();
126119
}
127120

128121
#[tokio::test]
@@ -133,9 +126,8 @@ async fn f64_gauge_is_exported() {
133126
tracing::subscriber::with_default(subscriber, || {
134127
tracing::info!(gauge.huitt = 1_f64);
135128
tracing::info!(gauge.huitt = 2_f64);
129+
exporter.export().unwrap();
136130
});
137-
138-
exporter.export().unwrap();
139131
}
140132

141133
#[tokio::test]
@@ -146,9 +138,8 @@ async fn i64_gauge_is_exported() {
146138
tracing::subscriber::with_default(subscriber, || {
147139
tracing::info!(gauge.samsagaz = 1_i64);
148140
tracing::info!(gauge.samsagaz = 2_i64);
141+
exporter.export().unwrap();
149142
});
150-
151-
exporter.export().unwrap();
152143
}
153144

154145
#[tokio::test]
@@ -162,9 +153,8 @@ async fn u64_histogram_is_exported() {
162153

163154
tracing::subscriber::with_default(subscriber, || {
164155
tracing::info!(histogram.abcdefg = 9_u64);
156+
exporter.export().unwrap();
165157
});
166-
167-
exporter.export().unwrap();
168158
}
169159

170160
#[tokio::test]
@@ -178,9 +168,8 @@ async fn f64_histogram_is_exported() {
178168

179169
tracing::subscriber::with_default(subscriber, || {
180170
tracing::info!(histogram.abcdefg_racecar = 777.0012_f64);
171+
exporter.export().unwrap();
181172
});
182-
183-
exporter.export().unwrap();
184173
}
185174

186175
#[tokio::test]
@@ -207,9 +196,8 @@ async fn u64_counter_with_attributes_is_exported() {
207196
str_key_1 = "foo",
208197
bool_key_1 = true,
209198
);
199+
exporter.export().unwrap();
210200
});
211-
212-
exporter.export().unwrap();
213201
}
214202

215203
#[tokio::test]
@@ -236,9 +224,8 @@ async fn f64_counter_with_attributes_is_exported() {
236224
str_key_1 = "foo",
237225
bool_key_1 = true,
238226
);
227+
exporter.export().unwrap();
239228
});
240-
241-
exporter.export().unwrap();
242229
}
243230

244231
#[tokio::test]
@@ -265,9 +252,8 @@ async fn i64_up_down_counter_with_attributes_is_exported() {
265252
str_key_1 = "foo",
266253
bool_key_1 = true,
267254
);
255+
exporter.export().unwrap();
268256
});
269-
270-
exporter.export().unwrap();
271257
}
272258

273259
#[tokio::test]
@@ -294,9 +280,8 @@ async fn f64_up_down_counter_with_attributes_is_exported() {
294280
str_key_1 = "foo",
295281
bool_key_1 = true,
296282
);
283+
exporter.export().unwrap();
297284
});
298-
299-
exporter.export().unwrap();
300285
}
301286

302287
#[tokio::test]
@@ -323,9 +308,8 @@ async fn f64_gauge_with_attributes_is_exported() {
323308
str_key_1 = "foo",
324309
bool_key_1 = true,
325310
);
311+
exporter.export().unwrap();
326312
});
327-
328-
exporter.export().unwrap();
329313
}
330314

331315
#[tokio::test]
@@ -352,9 +336,8 @@ async fn u64_gauge_with_attributes_is_exported() {
352336
str_key_1 = "foo",
353337
bool_key_1 = true,
354338
);
339+
exporter.export().unwrap();
355340
});
356-
357-
exporter.export().unwrap();
358341
}
359342

360343
#[tokio::test]
@@ -381,9 +364,8 @@ async fn i64_gauge_with_attributes_is_exported() {
381364
str_key_1 = "foo",
382365
bool_key_1 = true,
383366
);
367+
exporter.export().unwrap();
384368
});
385-
386-
exporter.export().unwrap();
387369
}
388370

389371
#[tokio::test]
@@ -410,9 +392,8 @@ async fn u64_histogram_with_attributes_is_exported() {
410392
str_key_1 = "foo",
411393
bool_key_1 = true,
412394
);
395+
exporter.export().unwrap();
413396
});
414-
415-
exporter.export().unwrap();
416397
}
417398

418399
#[tokio::test]
@@ -439,9 +420,8 @@ async fn f64_histogram_with_attributes_is_exported() {
439420
str_key_1 = "foo",
440421
bool_key_1 = true,
441422
);
423+
exporter.export().unwrap();
442424
});
443-
444-
exporter.export().unwrap();
445425
}
446426

447427
#[tokio::test]
@@ -468,9 +448,8 @@ async fn display_attribute_is_exported() {
468448
monotonic_counter.hello_world = 1_u64,
469449
display_key_1 = %display_attribute,
470450
);
451+
exporter.export().unwrap();
471452
});
472-
473-
exporter.export().unwrap();
474453
}
475454

476455
#[tokio::test]
@@ -497,9 +476,8 @@ async fn debug_attribute_is_exported() {
497476
monotonic_counter.hello_world = 1_u64,
498477
debug_key_1 = ?debug_attribute,
499478
);
479+
exporter.export().unwrap();
500480
});
501-
502-
exporter.export().unwrap();
503481
}
504482

505483
fn init_subscriber<T>(
@@ -522,7 +500,6 @@ fn init_subscriber<T>(
522500
expected_value,
523501
expected_attributes,
524502
reader,
525-
_meter_provider: provider.clone(),
526503
};
527504

528505
(
@@ -568,7 +545,6 @@ struct TestExporter<T> {
568545
expected_value: T,
569546
expected_attributes: Option<Vec<KeyValue>>,
570547
reader: TestReader,
571-
_meter_provider: SdkMeterProvider,
572548
}
573549

574550
trait AsAny {

0 commit comments

Comments
 (0)