1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::sync::Arc;
use crate::agent::metrics::MetricsCollector;
use crate::server::metrics_service::MetricsService;
/// Unified metrics infrastructure supporting both MetricsBus and MetricsService
///
/// This provides backward compatibility during migration from the dual-metrics system
/// to a unified approach. Eventually MetricsService will be the single source of truth.
pub struct MetricsInfrastructure {
/// Service-based metrics (agent/server style) - primary
pub service: Arc<MetricsService>,
/// Bus-based metrics (web_service style) - secondary, for backward compatibility
pub bus: Option<crate::agent::metrics::MetricsBus>,
}
impl MetricsInfrastructure {
/// Create new metrics infrastructure with both service and optional bus
pub async fn new(
db_path: impl AsRef<std::path::Path>,
enable_bus: bool,
bus_capacity: usize,
) -> Result<Self, crate::agent::metrics::MetricsError> {
// Initialize MetricsService (the future)
let service = Arc::new(MetricsService::new(db_path).await?);
// Optionally initialize MetricsBus (the past, for compatibility)
let bus = if enable_bus {
let (bus, _receiver) = crate::agent::metrics::MetricsBus::new(bus_capacity);
Some(bus)
} else {
None
};
Ok(Self { service, bus })
}
/// Get the metrics collector for emitting events
pub fn collector(&self) -> MetricsCollector {
self.service.collector()
}
/// Get a reference to the metrics service
pub fn service(&self) -> &Arc<MetricsService> {
&self.service
}
/// Get a reference to the optional metrics bus
pub fn bus(&self) -> Option<&crate::agent::metrics::MetricsBus> {
self.bus.as_ref()
}
}
impl Clone for MetricsInfrastructure {
fn clone(&self) -> Self {
Self {
service: Arc::clone(&self.service),
bus: self.bus.clone(),
}
}
}