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 { config, events: Mutex::new(Vec::new()) }
39    }
40
41    pub async fn record(&self, event: TelemetryEvent) -> Result<()> {
42        if !self.config.dashboards_enabled {
43            return Ok(());
44        }
45
46        let mut events = self.events.lock().await;
47        events.push(event);
48        Ok(())
49    }
50
51    pub async fn snapshot(&self) -> Vec<TelemetryEvent> {
52        let mut events = self.events.lock().await;
53        // Use drain instead of clone to avoid allocation
54        std::mem::take(&mut *events)
55    }
56}