opentelemetry_sdk/testing/metrics/
metric_reader.rs

1use crate::error::{OTelSdkError, OTelSdkResult};
2use crate::metrics::Temporality;
3use crate::metrics::{
4    data::ResourceMetrics, instrument::InstrumentKind, pipeline::Pipeline, reader::MetricReader,
5};
6use std::sync::{Arc, Mutex, Weak};
7use std::time::Duration;
8
9#[derive(Debug, Clone)]
10pub struct TestMetricReader {
11    is_shutdown: Arc<Mutex<bool>>,
12}
13
14impl TestMetricReader {
15    // Constructor to initialize the TestMetricReader
16    pub fn new() -> Self {
17        TestMetricReader {
18            is_shutdown: Arc::new(Mutex::new(false)),
19        }
20    }
21
22    // Method to check if the reader is shutdown
23    pub fn is_shutdown(&self) -> bool {
24        *self.is_shutdown.lock().unwrap()
25    }
26}
27
28impl Default for TestMetricReader {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl MetricReader for TestMetricReader {
35    fn register_pipeline(&self, _pipeline: Weak<Pipeline>) {}
36
37    fn collect(&self, _rm: &mut ResourceMetrics) -> OTelSdkResult {
38        Ok(())
39    }
40
41    fn force_flush(&self) -> OTelSdkResult {
42        Ok(())
43    }
44
45    fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
46        let result = self.force_flush();
47        {
48            let mut is_shutdown = self.is_shutdown.lock().unwrap();
49            *is_shutdown = true;
50        }
51        result.map_err(|e| OTelSdkError::InternalFailure(e.to_string()))
52    }
53
54    fn temporality(&self, _kind: InstrumentKind) -> Temporality {
55        Temporality::default()
56    }
57}