reachability_core/telemetry/
inspect.rs
1use fuchsia_inspect::{Inspector, Node as InspectNode};
6use fuchsia_sync::Mutex;
7use futures::FutureExt;
8use std::sync::Arc;
9use windowed_stats::aggregations::{create_saturating_add_fn, SumAndCount};
10use windowed_stats::{FifteenMinutelyWindows, HourlyWindows, MinutelyWindows, TimeSeries};
11
12#[derive(Debug)]
13pub struct Stats {
14 pub(crate) reachability_lost_count: TimeSeries<u32>,
15 pub(crate) internet_available_sec: TimeSeries<i32>,
16 pub(crate) dns_active_sec: TimeSeries<i32>,
17 pub(crate) http_active_sec: TimeSeries<i32>,
18 pub(crate) total_duration_sec: TimeSeries<i32>,
19 pub(crate) ipv4_state: TimeSeries<SumAndCount>,
20 pub(crate) ipv6_state: TimeSeries<SumAndCount>,
21}
22
23impl Stats {
24 pub(crate) fn new() -> Self {
25 Self {
26 reachability_lost_count: TimeSeries::new(create_saturating_add_fn),
27 internet_available_sec: TimeSeries::new(create_saturating_add_fn),
28 dns_active_sec: TimeSeries::new(create_saturating_add_fn),
29 http_active_sec: TimeSeries::new(create_saturating_add_fn),
30
31 total_duration_sec: TimeSeries::with_n_windows(
37 MinutelyWindows(1),
38 FifteenMinutelyWindows(1),
39 HourlyWindows(1),
40 create_saturating_add_fn,
41 ),
42
43 ipv4_state: TimeSeries::new(create_saturating_add_fn),
44 ipv6_state: TimeSeries::new(create_saturating_add_fn),
45 }
46 }
47
48 pub(crate) fn log_inspect(&mut self, node: &InspectNode) {
49 self.reachability_lost_count.log_inspect_uint_array(node, "reachability_lost_count");
50 self.internet_available_sec.log_inspect_int_array(node, "internet_available_sec");
51 self.dns_active_sec.log_inspect_int_array(node, "dns_active_sec");
52 self.http_active_sec.log_inspect_int_array(node, "http_active_sec");
53 self.total_duration_sec.log_inspect_int_array(node, "total_duration_sec");
54 self.ipv4_state.log_avg_inspect_double_array(node, "ipv4_state");
55 self.ipv6_state.log_avg_inspect_double_array(node, "ipv6_state");
56 }
57}
58
59pub(crate) fn inspect_record_stats(
60 inspect_node: &InspectNode,
61 child_name: &str,
62 stats: Arc<Mutex<Stats>>,
63) {
64 inspect_node.record_lazy_child(child_name, move || {
65 let stats = Arc::clone(&stats);
66 async move {
67 let inspector = Inspector::default();
68 {
69 stats.lock().log_inspect(inspector.root());
70 }
71 Ok(inspector)
72 }
73 .boxed()
74 });
75}