yang_base/
observability.rs1use std::time::Duration;
13
14#[derive(Debug, Clone, Default)]
19#[non_exhaustive]
20pub struct ObservabilityConfig {
21 pub slow_query_threshold: Option<Duration>,
25}
26
27impl ObservabilityConfig {
28 pub fn new() -> Self {
30 Self::default()
31 }
32
33 pub fn with_slow_query_threshold(mut self, threshold: Duration) -> Self {
35 self.slow_query_threshold = Some(threshold);
36 self
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use crate::tools::ToolsBuilder;
44
45 #[test]
46 fn builder_sets_threshold() {
47 let cfg = ObservabilityConfig::new().with_slow_query_threshold(Duration::from_millis(500));
48 assert_eq!(cfg.slow_query_threshold, Some(Duration::from_millis(500)));
49 }
50
51 #[test]
52 fn default_disables_slow_query_log() {
53 let cfg = ObservabilityConfig::new();
54 assert_eq!(cfg.slow_query_threshold, None);
55 }
56
57 #[test]
58 fn config_slot_roundtrip_via_tools() {
59 let threshold = Duration::from_millis(200);
60 let tools = ToolsBuilder::new()
61 .config(ObservabilityConfig::new().with_slow_query_threshold(threshold))
62 .build()
63 .expect("注册可观测性配置后应构建成功");
64
65 let retrieved = tools
66 .config::<ObservabilityConfig>()
67 .expect("已注册的配置应可读回");
68 assert_eq!(retrieved.slow_query_threshold, Some(threshold));
69 }
70}