iroh/
metrics.rs

1//! Co-locating all of the iroh metrics structs
2use std::sync::Arc;
3
4use iroh_metrics::MetricsGroupSet;
5#[cfg(feature = "test-utils")]
6pub use iroh_relay::server::Metrics as RelayMetrics;
7use serde::{Deserialize, Serialize};
8
9pub use crate::{
10    address_lookup::Metrics as AddressLookupMetrics, net_report::Metrics as NetReportMetrics,
11    socket::Metrics as SocketMetrics,
12};
13
14/// Metrics collected by an [`crate::endpoint::Endpoint`].
15///
16/// See [`crate::endpoint::Endpoint::metrics`] for details.
17#[derive(Default, Debug, Clone, Serialize, Deserialize, MetricsGroupSet)]
18#[metrics(name = "endpoint")]
19#[non_exhaustive]
20pub struct EndpointMetrics {
21    /// Metrics collected by the endpoint's socket.
22    pub socket: Arc<SocketMetrics>,
23    /// Metrics collected by net reports.
24    pub net_report: Arc<NetReportMetrics>,
25    /// Metrics collected by address lookup.
26    pub address_lookup: Arc<AddressLookupMetrics>,
27}
28
29#[cfg(test)]
30mod tests {
31    use super::EndpointMetrics;
32    #[test]
33    fn test_serde() {
34        use crate::address_lookup::ServiceLabels;
35
36        let metrics = EndpointMetrics::default();
37        metrics.socket.actor_link_change.inc();
38        metrics.net_report.reports.inc_by(10);
39        metrics
40            .address_lookup
41            .service_results
42            .get_or_create(&ServiceLabels::new("dns"))
43            .inc_by(3);
44        let encoded = postcard::to_stdvec(&metrics).unwrap();
45        let decoded: EndpointMetrics = postcard::from_bytes(&encoded).unwrap();
46        assert_eq!(decoded.socket.actor_link_change.get(), 1);
47        assert_eq!(decoded.net_report.reports.get(), 10);
48        assert_eq!(
49            decoded
50                .address_lookup
51                .service_results
52                .get(&ServiceLabels::new("dns"))
53                .map(|counter| counter.get()),
54            Some(3)
55        );
56    }
57}