veecle_telemetry/collector/test_exporter.rs
1use std::sync::{Arc, Mutex};
2use std::vec::Vec;
3
4use super::Export;
5use crate::protocol::InstanceMessage;
6use crate::to_static::ToStatic;
7
8/// An exporter for testing that stores all telemetry messages in memory.
9///
10/// This exporter is useful for unit tests and integration tests where you need
11/// to verify that specific telemetry messages were generated.
12#[derive(Debug)]
13pub struct TestExporter {
14 /// Shared vector storing all exported telemetry messages
15 pub spans: Arc<Mutex<Vec<InstanceMessage<'static>>>>,
16}
17
18impl TestExporter {
19 /// Creates a new test exporter and returns both the exporter and a handle to the message storage.
20 ///
21 /// The returned tuple contains the exporter and a shared reference to the vector
22 /// where all telemetry messages will be stored.
23 ///
24 /// # Examples
25 ///
26 /// ```rust
27 /// use veecle_telemetry::collector::TestExporter;
28 ///
29 /// let (exporter, messages) = TestExporter::new();
30 /// // Use exporter for telemetry collection
31 /// // Check messages for verification
32 /// ```
33 pub fn new() -> (Self, Arc<Mutex<Vec<InstanceMessage<'static>>>>) {
34 let spans = Arc::new(Mutex::new(Vec::new()));
35 (
36 Self {
37 spans: spans.clone(),
38 },
39 spans,
40 )
41 }
42}
43
44impl Export for TestExporter {
45 fn export(&self, message: InstanceMessage<'_>) {
46 self.spans.lock().unwrap().push(message.to_static());
47 }
48}