forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodic_reader.rs
963 lines (847 loc) · 35.6 KB
/
periodic_reader.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
use std::{
env, fmt,
sync::{
mpsc::{self, Receiver, Sender},
Arc, Mutex, Weak,
},
thread,
time::{Duration, Instant},
};
use opentelemetry::{otel_debug, otel_error, otel_info, otel_warn};
use crate::{
metrics::{exporter::PushMetricExporter, reader::SdkProducer, MetricError, MetricResult},
Resource,
};
use super::{
data::ResourceMetrics, instrument::InstrumentKind, reader::MetricReader, Pipeline, Temporality,
};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_INTERVAL: Duration = Duration::from_secs(60);
const METRIC_EXPORT_INTERVAL_NAME: &str = "OTEL_METRIC_EXPORT_INTERVAL";
const METRIC_EXPORT_TIMEOUT_NAME: &str = "OTEL_METRIC_EXPORT_TIMEOUT";
/// Configuration options for [PeriodicReader].
#[derive(Debug)]
pub struct PeriodicReaderBuilder<E> {
interval: Duration,
timeout: Duration,
exporter: E,
}
impl<E> PeriodicReaderBuilder<E>
where
E: PushMetricExporter,
{
fn new(exporter: E) -> Self {
let interval = env::var(METRIC_EXPORT_INTERVAL_NAME)
.ok()
.and_then(|v| v.parse().map(Duration::from_millis).ok())
.unwrap_or(DEFAULT_INTERVAL);
let timeout = env::var(METRIC_EXPORT_TIMEOUT_NAME)
.ok()
.and_then(|v| v.parse().map(Duration::from_millis).ok())
.unwrap_or(DEFAULT_TIMEOUT);
PeriodicReaderBuilder {
interval,
timeout,
exporter,
}
}
/// Configures the intervening time between exports for a [PeriodicReader].
///
/// This option overrides any value set for the `OTEL_METRIC_EXPORT_INTERVAL`
/// environment variable.
///
/// If this option is not used or `interval` is equal to zero, 60 seconds is
/// used as the default.
pub fn with_interval(mut self, interval: Duration) -> Self {
if !interval.is_zero() {
self.interval = interval;
}
self
}
/// Configures the timeout for an export to complete. PeriodicReader itself
/// does not enforce timeout. Instead timeout is passed on to the exporter
/// for each export attempt.
///
/// This option overrides any value set for the `OTEL_METRIC_EXPORT_TIMEOUT`
/// environment variable.
///
/// If this option is not used or `timeout` is equal to zero, 30 seconds is used
/// as the default.
pub fn with_timeout(mut self, timeout: Duration) -> Self {
if !timeout.is_zero() {
self.timeout = timeout;
}
self
}
/// Create a [PeriodicReader] with the given config.
pub fn build(self) -> PeriodicReader {
PeriodicReader::new(self.exporter, self.interval, self.timeout)
}
}
/// A [MetricReader] that continuously collects and exports metrics at a set
/// interval.
///
/// By default, `PeriodicReader` will collect and export metrics every 60
/// seconds. The export time is not counted towards the interval between
/// attempts. `PeriodicReader` itself does not enforce a timeout. Instead, the
/// timeout is passed on to the configured exporter for each export attempt.
///
/// `PeriodicReader` spawns a background thread to handle the periodic
/// collection and export of metrics. The background thread will continue to run
/// until `shutdown()` is called.
///
/// When using this reader with the OTLP Exporter, the following exporter
/// features are supported:
/// - `grpc-tonic`: This requires `MeterProvider` to be created within a tokio
/// runtime.
/// - `reqwest-blocking-client`: Works with a regular `main` or `tokio::main`.
///
/// In other words, other clients like `reqwest` and `hyper` are not supported.
///
/// # Example
///
/// ```no_run
/// use opentelemetry_sdk::metrics::PeriodicReader;
/// # fn example<E>(get_exporter: impl Fn() -> E)
/// # where
/// # E: opentelemetry_sdk::metrics::exporter::PushMetricExporter,
/// # {
///
/// let exporter = get_exporter(); // set up a push exporter
///
/// let reader = PeriodicReader::builder(exporter).build();
/// # drop(reader);
/// # }
/// ```
#[derive(Clone)]
pub struct PeriodicReader {
inner: Arc<PeriodicReaderInner>,
}
impl PeriodicReader {
/// Configuration options for a periodic reader with own thread
pub fn builder<E>(exporter: E) -> PeriodicReaderBuilder<E>
where
E: PushMetricExporter,
{
PeriodicReaderBuilder::new(exporter)
}
fn new<E>(exporter: E, interval: Duration, timeout: Duration) -> Self
where
E: PushMetricExporter,
{
let (message_sender, message_receiver): (Sender<Message>, Receiver<Message>) =
mpsc::channel();
let exporter_arc = Arc::new(exporter);
let reader = PeriodicReader {
inner: Arc::new(PeriodicReaderInner {
message_sender: Arc::new(message_sender),
producer: Mutex::new(None),
exporter: exporter_arc.clone(),
}),
};
let cloned_reader = reader.clone();
let result_thread_creation = thread::Builder::new()
.name("OpenTelemetry.Metrics.PeriodicReader".to_string())
.spawn(move || {
let mut interval_start = Instant::now();
let mut remaining_interval = interval;
otel_info!(
name: "PeriodReaderThreadStarted",
interval_in_millisecs = interval.as_millis(),
timeout_in_millisecs = timeout.as_millis()
);
loop {
otel_debug!(
name: "PeriodReaderThreadLoopAlive", message = "Next export will happen after interval, unless flush or shutdown is triggered.", interval_in_millisecs = remaining_interval.as_millis()
);
match message_receiver.recv_timeout(remaining_interval) {
Ok(Message::Flush(response_sender)) => {
otel_debug!(
name: "PeriodReaderThreadExportingDueToFlush"
);
if let Err(_e) = cloned_reader.collect_and_export(timeout) {
response_sender.send(false).unwrap();
} else {
response_sender.send(true).unwrap();
}
// Adjust the remaining interval after the flush
let elapsed = interval_start.elapsed();
if elapsed < interval {
remaining_interval = interval - elapsed;
otel_debug!(
name: "PeriodReaderThreadAdjustingRemainingIntervalAfterFlush",
remaining_interval = remaining_interval.as_secs()
);
} else {
otel_debug!(
name: "PeriodReaderThreadAdjustingExportAfterFlush",
);
// Reset the interval if the flush finishes after the expected export time
// effectively missing the normal export.
// Should we attempt to do the missed export immediately?
// Or do the next export at the next interval?
// Currently this attempts the next export immediately.
// i.e calling Flush can affect the regularity.
interval_start = Instant::now();
remaining_interval = Duration::ZERO;
}
}
Ok(Message::Shutdown(response_sender)) => {
// Perform final export and break out of loop and exit the thread
otel_debug!(name: "PeriodReaderThreadExportingDueToShutdown");
let export_result = cloned_reader.collect_and_export(timeout);
let shutdown_result = exporter_arc.shutdown();
otel_debug!(
name: "PeriodReaderInvokedExporterShutdown",
shutdown_result = format!("{:?}", shutdown_result)
);
if export_result.is_err() || shutdown_result.is_err() {
response_sender.send(false).unwrap();
} else {
response_sender.send(true).unwrap();
}
otel_debug!(
name: "PeriodReaderThreadExiting",
reason = "ShutdownRequested"
);
break;
}
Err(mpsc::RecvTimeoutError::Timeout) => {
let export_start = Instant::now();
otel_debug!(
name: "PeriodReaderThreadExportingDueToTimer"
);
if let Err(_e) = cloned_reader.collect_and_export(timeout) {
otel_debug!(
name: "PeriodReaderThreadExportingDueToTimerFailed"
);
}
let time_taken_for_export = export_start.elapsed();
if time_taken_for_export > interval {
otel_debug!(
name: "PeriodReaderThreadExportTookLongerThanInterval"
);
// if export took longer than interval, do the
// next export immediately.
// Alternatively, we could skip the next export
// and wait for the next interval.
// Or enforce that export timeout is less than interval.
// What is the desired behavior?
interval_start = Instant::now();
remaining_interval = Duration::ZERO;
} else {
remaining_interval = interval - time_taken_for_export;
interval_start = Instant::now();
}
}
Err(mpsc::RecvTimeoutError::Disconnected) => {
// Channel disconnected, only thing to do is break
// out (i.e exit the thread)
otel_debug!(
name: "PeriodReaderThreadExiting",
reason = "MessageSenderDisconnected"
);
break;
}
}
}
otel_info!(
name: "PeriodReaderThreadStopped"
);
});
// TODO: Should we fail-fast here and bubble up the error to user?
#[allow(unused_variables)]
if let Err(e) = result_thread_creation {
otel_error!(
name: "PeriodReaderThreadStartError",
message = "Failed to start PeriodicReader thread. Metrics will not be exported.",
error = format!("{:?}", e)
);
}
reader
}
fn collect_and_export(&self, timeout: Duration) -> MetricResult<()> {
self.inner.collect_and_export(timeout)
}
}
impl fmt::Debug for PeriodicReader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PeriodicReader").finish()
}
}
struct PeriodicReaderInner {
exporter: Arc<dyn PushMetricExporter>,
message_sender: Arc<mpsc::Sender<Message>>,
producer: Mutex<Option<Weak<dyn SdkProducer>>>,
}
impl PeriodicReaderInner {
fn register_pipeline(&self, producer: Weak<dyn SdkProducer>) {
let mut inner = self.producer.lock().expect("lock poisoned");
*inner = Some(producer);
}
fn temporality(&self, _kind: InstrumentKind) -> Temporality {
self.exporter.temporality()
}
fn collect(&self, rm: &mut ResourceMetrics) -> MetricResult<()> {
let producer = self.producer.lock().expect("lock poisoned");
if let Some(p) = producer.as_ref() {
p.upgrade()
.ok_or_else(|| MetricError::Other("pipeline is dropped".into()))?
.produce(rm)?;
Ok(())
} else {
Err(MetricError::Other("pipeline is not registered".into()))
}
}
fn collect_and_export(&self, _timeout: Duration) -> MetricResult<()> {
// TODO: Reuse the internal vectors. Or refactor to avoid needing any
// owned data structures to be passed to exporters.
let mut rm = ResourceMetrics {
resource: Resource::empty(),
scope_metrics: Vec::new(),
};
let collect_result = self.collect(&mut rm);
#[allow(clippy::question_mark)]
if let Err(e) = collect_result {
otel_warn!(
name: "PeriodReaderCollectError",
error = format!("{:?}", e)
);
return Err(e);
}
if rm.scope_metrics.is_empty() {
otel_debug!(name: "NoMetricsCollected");
return Ok(());
}
let metrics_count = rm.scope_metrics.iter().fold(0, |count, scope_metrics| {
count + scope_metrics.metrics.len()
});
otel_debug!(name: "PeriodicReaderMetricsCollected", count = metrics_count);
// TODO: subtract the time taken for collect from the timeout. collect
// involves observable callbacks too, which are user defined and can
// take arbitrary time.
//
// Relying on futures executor to execute async call.
// TODO: Add timeout and pass it to exporter or consider alternative
// design to enforce timeout here.
let exporter_result = futures_executor::block_on(self.exporter.export(&mut rm));
#[allow(clippy::question_mark)]
if let Err(e) = exporter_result {
otel_warn!(
name: "PeriodReaderExportError",
error = format!("{:?}", e)
);
return Err(e);
}
Ok(())
}
fn force_flush(&self) -> MetricResult<()> {
// TODO: Better message for this scenario.
// Flush and Shutdown called from 2 threads Flush check shutdown
// flag before shutdown thread sets it. Both threads attempt to send
// message to the same channel. Case1: Flush thread sends message first,
// shutdown thread sends message next. Flush would succeed, as
// background thread won't process shutdown message until flush
// triggered export is done. Case2: Shutdown thread sends message first,
// flush thread sends message next. Shutdown would succeed, as
// background thread would process shutdown message first. The
// background exits so it won't receive the flush message. ForceFlush
// returns Failure, but we could indicate specifically that shutdown has
// completed. TODO is to see if this message can be improved.
let (response_tx, response_rx) = mpsc::channel();
self.message_sender
.send(Message::Flush(response_tx))
.map_err(|e| MetricError::Other(e.to_string()))?;
if let Ok(response) = response_rx.recv() {
// TODO: call exporter's force_flush method.
if response {
Ok(())
} else {
Err(MetricError::Other("Failed to flush".into()))
}
} else {
Err(MetricError::Other("Failed to flush".into()))
}
}
fn shutdown(&self) -> MetricResult<()> {
// TODO: See if this is better to be created upfront.
let (response_tx, response_rx) = mpsc::channel();
self.message_sender
.send(Message::Shutdown(response_tx))
.map_err(|e| MetricError::Other(e.to_string()))?;
// TODO: Make this timeout configurable.
match response_rx.recv_timeout(Duration::from_secs(5)) {
Ok(response) => {
if response {
Ok(())
} else {
Err(MetricError::Other("Failed to shutdown".into()))
}
}
Err(mpsc::RecvTimeoutError::Timeout) => Err(MetricError::Other(
"Failed to shutdown due to Timeout".into(),
)),
Err(mpsc::RecvTimeoutError::Disconnected) => {
Err(MetricError::Other("Failed to shutdown".into()))
}
}
}
}
#[derive(Debug)]
enum Message {
Flush(Sender<bool>),
Shutdown(Sender<bool>),
}
impl MetricReader for PeriodicReader {
fn register_pipeline(&self, pipeline: Weak<Pipeline>) {
self.inner.register_pipeline(pipeline);
}
fn collect(&self, rm: &mut ResourceMetrics) -> MetricResult<()> {
self.inner.collect(rm)
}
fn force_flush(&self) -> MetricResult<()> {
self.inner.force_flush()
}
// TODO: Offer an async version of shutdown so users can await the shutdown
// completion, and avoid blocking the thread. The default shutdown on drop
// can still use blocking call. If user already explicitly called shutdown,
// drop won't call shutdown again.
fn shutdown(&self) -> MetricResult<()> {
self.inner.shutdown()
}
/// To construct a [MetricReader][metric-reader] when setting up an SDK,
/// The output temporality (optional), a function of instrument kind.
/// This function SHOULD be obtained from the exporter.
///
/// If not configured, the Cumulative temporality SHOULD be used.
///
/// [metric-reader]: https://github.com/open-telemetry/opentelemetry-specification/blob/0a78571045ca1dca48621c9648ec3c832c3c541c/specification/metrics/sdk.md#metricreader
fn temporality(&self, kind: InstrumentKind) -> Temporality {
kind.temporality_preference(self.inner.temporality(kind))
}
}
#[cfg(all(test, feature = "testing"))]
mod tests {
use super::PeriodicReader;
use crate::{
metrics::{
data::ResourceMetrics, exporter::PushMetricExporter, reader::MetricReader, MetricError,
MetricResult, SdkMeterProvider, Temporality,
},
testing::metrics::InMemoryMetricExporter,
Resource,
};
use async_trait::async_trait;
use opentelemetry::metrics::MeterProvider;
use std::{
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
mpsc, Arc,
},
time::Duration,
};
// use below command to run all tests
// cargo test metrics::periodic_reader::tests --features=testing,spec_unstable_metrics_views -- --nocapture
#[derive(Debug, Clone)]
struct MetricExporterThatFailsOnlyOnFirst {
count: Arc<AtomicUsize>,
}
impl Default for MetricExporterThatFailsOnlyOnFirst {
fn default() -> Self {
MetricExporterThatFailsOnlyOnFirst {
count: Arc::new(AtomicUsize::new(0)),
}
}
}
impl MetricExporterThatFailsOnlyOnFirst {
fn get_count(&self) -> usize {
self.count.load(Ordering::Relaxed)
}
}
#[async_trait]
impl PushMetricExporter for MetricExporterThatFailsOnlyOnFirst {
async fn export(&self, _metrics: &mut ResourceMetrics) -> MetricResult<()> {
if self.count.fetch_add(1, Ordering::Relaxed) == 0 {
Err(MetricError::Other("export failed".into()))
} else {
Ok(())
}
}
async fn force_flush(&self) -> MetricResult<()> {
Ok(())
}
fn shutdown(&self) -> MetricResult<()> {
Ok(())
}
fn temporality(&self) -> Temporality {
Temporality::Cumulative
}
}
#[derive(Debug, Clone, Default)]
struct MockMetricExporter {
is_shutdown: Arc<AtomicBool>,
}
#[async_trait]
impl PushMetricExporter for MockMetricExporter {
async fn export(&self, _metrics: &mut ResourceMetrics) -> MetricResult<()> {
Ok(())
}
async fn force_flush(&self) -> MetricResult<()> {
Ok(())
}
fn shutdown(&self) -> MetricResult<()> {
self.is_shutdown.store(true, Ordering::Relaxed);
Ok(())
}
fn temporality(&self) -> Temporality {
Temporality::Cumulative
}
}
#[test]
fn collection_triggered_by_interval_multiple() {
// Arrange
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let i = Arc::new(AtomicUsize::new(0));
let i_clone = i.clone();
// Act
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
let _counter = meter
.u64_observable_counter("testcounter")
.with_callback(move |_| {
i_clone.fetch_add(1, Ordering::Relaxed);
})
.build();
// Sleep for a duration 5X (plus liberal buffer to account for potential
// CI slowness) the interval to ensure multiple collection.
// Not a fan of such tests, but this seems to be the only way to test
// if periodic reader is doing its job.
// TODO: Decide if this should be ignored in CI
std::thread::sleep(interval * 5 * 20);
// Assert
assert!(i.load(Ordering::Relaxed) >= 5);
}
#[test]
fn shutdown_repeat() {
// Arrange
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let result = meter_provider.shutdown();
assert!(result.is_ok());
// calling shutdown again should return Err
let result = meter_provider.shutdown();
assert!(result.is_err());
// calling shutdown again should return Err
let result = meter_provider.shutdown();
assert!(result.is_err());
}
#[test]
fn flush_after_shutdown() {
// Arrange
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let result = meter_provider.force_flush();
assert!(result.is_ok());
let result = meter_provider.shutdown();
assert!(result.is_ok());
// calling force_flush after shutdown should return Err
let result = meter_provider.force_flush();
assert!(result.is_err());
}
#[test]
fn flush_repeat() {
// Arrange
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let result = meter_provider.force_flush();
assert!(result.is_ok());
// calling force_flush again should return Ok
let result = meter_provider.force_flush();
assert!(result.is_ok());
}
#[test]
fn periodic_reader_without_pipeline() {
// Arrange
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let rm = &mut ResourceMetrics {
resource: Resource::empty(),
scope_metrics: Vec::new(),
};
// Pipeline is not registered, so collect should return an error
let result = reader.collect(rm);
assert!(result.is_err());
// Pipeline is not registered, so flush should return an error
let result = reader.force_flush();
assert!(result.is_err());
// Adding reader to meter provider should register the pipeline
// TODO: This part might benefit from a different design.
let meter_provider = SdkMeterProvider::builder()
.with_reader(reader.clone())
.build();
// Now collect and flush should succeed
let result = reader.collect(rm);
assert!(result.is_ok());
let result = meter_provider.force_flush();
assert!(result.is_ok());
}
#[test]
fn exporter_failures_are_handled() {
// create a mock exporter that fails 1st time and succeeds 2nd time
// Validate using this exporter that periodic reader can handle exporter failure
// and continue to export metrics.
// Arrange
let interval = std::time::Duration::from_millis(10);
let exporter = MetricExporterThatFailsOnlyOnFirst::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
let counter = meter.u64_counter("sync_counter").build();
counter.add(1, &[]);
let _obs_counter = meter
.u64_observable_counter("testcounter")
.with_callback(move |observer| {
observer.observe(1, &[]);
})
.build();
// Sleep for a duration much longer than the interval to trigger
// multiple exports, including failures.
// Not a fan of such tests, but this seems to be the
// only way to test if periodic reader is doing its job. TODO: Decide if
// this should be ignored in CI
std::thread::sleep(Duration::from_millis(500));
// Assert that atleast 2 exports are attempted given the 1st one fails.
assert!(exporter.get_count() >= 2);
}
#[test]
fn shutdown_passed_to_exporter() {
// Arrange
let exporter = MockMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone()).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
let counter = meter.u64_counter("sync_counter").build();
counter.add(1, &[]);
// shutdown the provider, which should call shutdown on periodic reader
// which in turn should call shutdown on exporter.
let result = meter_provider.shutdown();
assert!(result.is_ok());
assert!(exporter.is_shutdown.load(Ordering::Relaxed));
}
#[test]
fn collection() {
collection_triggered_by_interval_helper();
collection_triggered_by_flush_helper();
collection_triggered_by_shutdown_helper();
collection_triggered_by_drop_helper();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn collection_from_tokio_multi_with_one_worker() {
collection_triggered_by_interval_helper();
collection_triggered_by_flush_helper();
collection_triggered_by_shutdown_helper();
collection_triggered_by_drop_helper();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn collection_from_tokio_with_two_worker() {
collection_triggered_by_interval_helper();
collection_triggered_by_flush_helper();
collection_triggered_by_shutdown_helper();
collection_triggered_by_drop_helper();
}
#[tokio::test(flavor = "current_thread")]
async fn collection_from_tokio_current() {
collection_triggered_by_interval_helper();
collection_triggered_by_flush_helper();
collection_triggered_by_shutdown_helper();
collection_triggered_by_drop_helper();
}
fn collection_triggered_by_interval_helper() {
collection_helper(|_| {
// Sleep for a duration longer than the interval to ensure at least one collection
// Not a fan of such tests, but this seems to be the only way to test
// if periodic reader is doing its job.
// TODO: Decide if this should be ignored in CI
std::thread::sleep(Duration::from_millis(500));
});
}
fn collection_triggered_by_flush_helper() {
collection_helper(|meter_provider| {
meter_provider.force_flush().expect("flush should succeed");
});
}
fn collection_triggered_by_shutdown_helper() {
collection_helper(|meter_provider| {
meter_provider.shutdown().expect("shutdown should succeed");
});
}
fn collection_triggered_by_drop_helper() {
collection_helper(|meter_provider| {
drop(meter_provider);
});
}
fn collection_helper(trigger: fn(SdkMeterProvider)) {
// Arrange
let interval = std::time::Duration::from_millis(10);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let (sender, receiver) = mpsc::channel();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
let _counter = meter
.u64_observable_counter("testcounter")
.with_callback(move |observer| {
observer.observe(1, &[]);
sender.send(()).expect("channel should still be open");
})
.build();
// Act
trigger(meter_provider);
// Assert
receiver
.recv_timeout(Duration::ZERO)
.expect("message should be available in channel, indicating a collection occurred, which should trigger observable callback");
let exported_metrics = exporter
.get_finished_metrics()
.expect("this should not fail");
assert!(
!exported_metrics.is_empty(),
"Metrics should be available in exporter."
);
}
async fn some_async_function() -> u64 {
// No dependency on any particular async runtime.
std::thread::sleep(std::time::Duration::from_millis(1));
1
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn async_inside_observable_callback_from_tokio_multi_with_one_worker() {
async_inside_observable_callback_helper();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn async_inside_observable_callback_from_tokio_multi_with_two_worker() {
async_inside_observable_callback_helper();
}
#[tokio::test(flavor = "current_thread")]
async fn async_inside_observable_callback_from_tokio_current_thread() {
async_inside_observable_callback_helper();
}
#[test]
fn async_inside_observable_callback_from_regular_main() {
async_inside_observable_callback_helper();
}
fn async_inside_observable_callback_helper() {
let interval = std::time::Duration::from_millis(10);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
let _gauge = meter
.u64_observable_gauge("my_observable_gauge")
.with_callback(|observer| {
// using futures_executor::block_on intentionally and avoiding
// any particular async runtime.
let value = futures_executor::block_on(some_async_function());
observer.observe(value, &[]);
})
.build();
meter_provider.force_flush().expect("flush should succeed");
let exported_metrics = exporter
.get_finished_metrics()
.expect("this should not fail");
assert!(
!exported_metrics.is_empty(),
"Metrics should be available in exporter."
);
}
async fn some_tokio_async_function() -> u64 {
// Tokio specific async function
tokio::time::sleep(Duration::from_millis(1)).await;
1
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn tokio_async_inside_observable_callback_from_tokio_multi_with_one_worker() {
tokio_async_inside_observable_callback_helper(true);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn tokio_async_inside_observable_callback_from_tokio_multi_with_two_worker() {
tokio_async_inside_observable_callback_helper(true);
}
#[tokio::test(flavor = "current_thread")]
#[ignore] //TODO: Investigate if this can be fixed.
async fn tokio_async_inside_observable_callback_from_tokio_current_thread() {
tokio_async_inside_observable_callback_helper(true);
}
#[test]
fn tokio_async_inside_observable_callback_from_regular_main() {
tokio_async_inside_observable_callback_helper(false);
}
fn tokio_async_inside_observable_callback_helper(use_current_tokio_runtime: bool) {
let interval = std::time::Duration::from_millis(10);
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone())
.with_interval(interval)
.build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();
let meter = meter_provider.meter("test");
if use_current_tokio_runtime {
let rt = tokio::runtime::Handle::current().clone();
let _gauge = meter
.u64_observable_gauge("my_observable_gauge")
.with_callback(move |observer| {
// call tokio specific async function from here
let value = rt.block_on(some_tokio_async_function());
observer.observe(value, &[]);
})
.build();
// rt here is a reference to the current tokio runtime.
// Dropping it occurs when the tokio::main itself ends.
} else {
let rt = tokio::runtime::Runtime::new().unwrap();
let _gauge = meter
.u64_observable_gauge("my_observable_gauge")
.with_callback(move |observer| {
// call tokio specific async function from here
let value = rt.block_on(some_tokio_async_function());
observer.observe(value, &[]);
})
.build();
// rt is not dropped here as it is moved to the closure,
// and is dropped only when MeterProvider itself is dropped.
// This works when called from normal main.
};
meter_provider.force_flush().expect("flush should succeed");
let exported_metrics = exporter
.get_finished_metrics()
.expect("this should not fail");
assert!(
!exported_metrics.is_empty(),
"Metrics should be available in exporter."
);
}
}