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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Monitoring configuration settings for the application.
//!
//! This module defines configuration parameters related to system monitoring,
//! particularly for Prometheus metrics collection.
use config::ConfigError;
use serde::Deserialize;
use serde::Serialize;
use crate::Error;
use crate::Result;
/// Configuration structure for monitoring features
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MonitoringConfig {
/// Enables Prometheus metrics endpoint when set to true
/// Default value: false (via default_prometheus_enabled)
#[serde(default = "default_prometheus_enabled")]
pub prometheus_enabled: bool,
/// TCP port number for Prometheus metrics endpoint
/// Default value: 8080 (via default_prometheus_port)
#[serde(default = "default_prometheus_port")]
pub prometheus_port: u16,
}
impl Default for MonitoringConfig {
fn default() -> Self {
Self {
prometheus_enabled: default_prometheus_enabled(),
prometheus_port: default_prometheus_port(),
}
}
}
impl MonitoringConfig {
/// Validates monitoring configuration
/// # Errors
/// Returns `Error::InvalidConfig` when:
/// - Prometheus is enabled with invalid port
/// - Port conflicts with well-known services
pub fn validate(&self) -> Result<()> {
if self.prometheus_enabled {
// Validate port range
if self.prometheus_port == 0 {
return Err(Error::Config(ConfigError::Message(
"prometheus_port cannot be 0 when enabled".into(),
)));
}
// Check privileged ports (requires root)
if self.prometheus_port < 1024 {
return Err(Error::Config(ConfigError::Message(format!(
"prometheus_port {} is a privileged port (requires root)",
self.prometheus_port
))));
}
// Verify port availability (runtime check)
#[cfg(not(test))]
{
use std::net::TcpListener;
if let Err(e) = TcpListener::bind(("0.0.0.0", self.prometheus_port)) {
return Err(Error::Config(ConfigError::Message(format!(
"prometheus_port {} unavailable: {}",
self.prometheus_port, e
))));
}
}
} else {
// Warn about unused port configuration
#[cfg(debug_assertions)]
if self.prometheus_port != default_prometheus_port() {
tracing::warn!(
"prometheus_port configured to {} but monitoring is disabled",
self.prometheus_port
);
}
}
Ok(())
}
}
fn default_prometheus_enabled() -> bool {
false
}
fn default_prometheus_port() -> u16 {
8080
}