use std::rc::Rc;
use super::snapshot::MemorySnapshot;
pub trait TelemetryBackend {
fn emit_snapshot(&self, snapshot: &MemorySnapshot);
fn emit_warning(&self, message: &str);
fn flush(&self);
}
#[derive(Debug, Default)]
pub struct NoOpBackend;
impl TelemetryBackend for NoOpBackend {
fn emit_snapshot(&self, _snapshot: &MemorySnapshot) {}
fn emit_warning(&self, _message: &str) {}
fn flush(&self) {}
}
pub struct LoggingBackend {
warn_threshold_bytes: usize,
logger: Rc<dyn crate::logger::Loggable>,
}
impl LoggingBackend {
pub fn with_logger(
warn_threshold_bytes: usize,
logger: Rc<dyn crate::logger::Loggable>,
) -> Self {
Self {
warn_threshold_bytes,
logger,
}
}
}
impl TelemetryBackend for LoggingBackend {
fn emit_snapshot(&self, snapshot: &MemorySnapshot) {
self.logger.info(&format!(
"[METRICS] iteration={} history_len={} heap_bytes={} checkpoint_count={}",
snapshot.iteration,
snapshot.execution_history_len,
snapshot.execution_history_heap_bytes,
snapshot.checkpoint_count
));
if snapshot.execution_history_heap_bytes > self.warn_threshold_bytes {
self.emit_warning(&format!(
"Execution history heap size {} bytes exceeds warning threshold {} bytes",
snapshot.execution_history_heap_bytes, self.warn_threshold_bytes
));
}
}
fn emit_warning(&self, message: &str) {
self.logger.warn(&format!("[METRICS WARNING] {message}"));
}
fn flush(&self) {
}
}