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