ant_quic/metrics/
mod.rs

1// Copyright 2024 Saorsa Labs Ltd.
2//
3// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
4// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
5//
6// Full details available at https://saorsalabs.com/licenses
7
8//! Metrics collection and export system
9//!
10//! This module provides metrics collection and export capabilities for ant-quic.
11//! It includes both internal metrics collection (always available) and optional
12//! Prometheus export functionality.
13//!
14//! ## Feature Gates
15//!
16//! - **Basic metrics**: Always available (no feature flag required)
17//! - **Prometheus export**: Requires the `prometheus` feature flag
18//!
19//! ## Example
20//!
21//! ```rust
22//! use ant_quic::metrics::MetricsConfig;
23//!
24//! // Basic metrics config (always available)
25//! let config = MetricsConfig::default();
26//! assert!(!config.enabled);
27//!
28//! #[cfg(feature = "prometheus")]
29//! {
30//!     // Prometheus-specific functionality only available with feature flag
31//!     use ant_quic::metrics::{PrometheusExporter, MetricsServer};
32//! }
33//! ```
34
35pub use crate::logging::metrics::*;
36
37#[cfg(feature = "prometheus")]
38pub mod prometheus;
39
40#[cfg(feature = "prometheus")]
41pub mod http_server;
42
43#[cfg(feature = "prometheus")]
44pub use self::prometheus::PrometheusExporter;
45
46#[cfg(feature = "prometheus")]
47pub use self::http_server::MetricsServer;
48
49/// Configuration for metrics collection and export
50#[derive(Debug, Clone)]
51pub struct MetricsConfig {
52    /// Whether to enable metrics collection
53    pub enabled: bool,
54    /// Port for the metrics HTTP server (only used when prometheus feature is enabled)
55    pub port: u16,
56    /// Address to bind the metrics server to
57    pub bind_address: std::net::IpAddr,
58    /// Update interval for metrics collection
59    pub update_interval: std::time::Duration,
60}
61
62impl Default for MetricsConfig {
63    fn default() -> Self {
64        Self {
65            enabled: false,
66            port: 9090,
67            bind_address: std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0)),
68            update_interval: std::time::Duration::from_secs(30),
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_metrics_config_default() {
79        let config = MetricsConfig::default();
80        assert!(!config.enabled);
81        assert_eq!(config.port, 9090);
82        assert_eq!(
83            config.bind_address,
84            std::net::IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0))
85        );
86        assert_eq!(config.update_interval, std::time::Duration::from_secs(30));
87    }
88}