Skip to main content

vtcode_core/telemetry/
pipeline.rs

1use hashbrown::HashMap;
2use std::time::SystemTime;
3
4use anyhow::Result;
5use tokio::sync::Mutex;
6
7use crate::config::TelemetryConfig;
8
9/// Single telemetry observation.
10#[derive(Debug, Clone)]
11pub struct TelemetryEvent {
12    pub name: String,
13    pub value: f64,
14    pub timestamp: SystemTime,
15    pub tags: HashMap<String, String>,
16}
17
18impl TelemetryEvent {
19    pub fn new(name: impl Into<String>, value: f64) -> Self {
20        Self {
21            name: name.into(),
22            value,
23            timestamp: SystemTime::now(),
24            tags: HashMap::new(),
25        }
26    }
27}
28
29/// In-memory telemetry pipeline suitable for dashboards.
30#[derive(Debug)]
31pub struct TelemetryPipeline {
32    config: TelemetryConfig,
33    events: Mutex<Vec<TelemetryEvent>>,
34}
35
36impl TelemetryPipeline {
37    pub fn new(config: TelemetryConfig) -> Self {
38        Self {
39            config,
40            events: Mutex::new(Vec::new()),
41        }
42    }
43
44    pub async fn record(&self, event: TelemetryEvent) -> Result<()> {
45        if !self.config.dashboards_enabled {
46            return Ok(());
47        }
48
49        let mut events = self.events.lock().await;
50        events.push(event);
51        Ok(())
52    }
53
54    pub async fn snapshot(&self) -> Vec<TelemetryEvent> {
55        let mut events = self.events.lock().await;
56        // Use drain instead of clone to avoid allocation
57        std::mem::take(&mut *events)
58    }
59}