ultrafast_mcp_monitoring/
config.rs1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::time::Duration;
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
9pub struct MonitoringConfig {
10 pub tracing: TracingConfig,
12 pub metrics: MetricsConfig,
14 pub health: HealthConfig,
16 pub http: HttpConfig,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct TracingConfig {
23 pub enabled: bool,
25 pub service_name: String,
27 pub service_version: String,
29 pub environment: String,
31 pub jaeger: Option<JaegerConfig>,
33 pub otlp: Option<OtlpConfig>,
35 pub console: bool,
37 pub level: String,
39 pub sample_rate: f32,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct MetricsConfig {
46 pub enabled: bool,
48 pub prometheus: Option<PrometheusConfig>,
50 pub otlp: Option<OtlpConfig>,
52 pub collection_interval: Duration,
54 pub system_metrics: bool,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct HealthConfig {
61 pub enabled: bool,
63 pub check_interval: Duration,
65 pub timeout: Duration,
67 pub custom_checks: Vec<String>,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct HttpConfig {
74 pub enabled: bool,
76 pub address: String,
78 pub port: u16,
80 pub tls: Option<TlsConfig>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct JaegerConfig {
87 pub agent_endpoint: String,
89 pub collector_endpoint: Option<String>,
91 pub headers: HashMap<String, String>,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct OtlpConfig {
98 pub endpoint: String,
100 pub headers: HashMap<String, String>,
102 pub timeout: Duration,
104 pub compression: Option<String>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct PrometheusConfig {
111 pub registry_name: String,
113 pub prefix: String,
115 pub labels: HashMap<String, String>,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct TlsConfig {
122 pub cert_file: String,
124 pub key_file: String,
126}
127
128impl Default for TracingConfig {
129 fn default() -> Self {
130 Self {
131 enabled: true,
132 service_name: "ultrafast-mcp".to_string(),
133 service_version: env!("CARGO_PKG_VERSION").to_string(),
134 environment: "development".to_string(),
135 jaeger: Some(JaegerConfig::default()),
136 otlp: None,
137 console: true,
138 level: "info".to_string(),
139 sample_rate: 1.0,
140 }
141 }
142}
143
144impl Default for MetricsConfig {
145 fn default() -> Self {
146 Self {
147 enabled: true,
148 prometheus: Some(PrometheusConfig::default()),
149 otlp: None,
150 collection_interval: Duration::from_secs(30),
151 system_metrics: true,
152 }
153 }
154}
155
156impl Default for HealthConfig {
157 fn default() -> Self {
158 Self {
159 enabled: true,
160 check_interval: Duration::from_secs(30),
161 timeout: Duration::from_secs(5),
162 custom_checks: vec![],
163 }
164 }
165}
166
167impl Default for HttpConfig {
168 fn default() -> Self {
169 Self {
170 enabled: true,
171 address: "127.0.0.1".to_string(),
172 port: 9090,
173 tls: None,
174 }
175 }
176}
177
178impl Default for JaegerConfig {
179 fn default() -> Self {
180 Self {
181 agent_endpoint: "http://localhost:14268/api/traces".to_string(),
182 collector_endpoint: None,
183 headers: HashMap::new(),
184 }
185 }
186}
187
188impl Default for PrometheusConfig {
189 fn default() -> Self {
190 Self {
191 registry_name: "ultrafast_mcp".to_string(),
192 prefix: "mcp".to_string(),
193 labels: HashMap::new(),
194 }
195 }
196}
197
198impl MonitoringConfig {
199 pub fn from_env() -> Self {
201 let mut config = Self::default();
202
203 if let Ok(val) = std::env::var("MCP_TRACING_ENABLED") {
205 config.tracing.enabled = val.parse().unwrap_or(true);
206 }
207
208 if let Ok(val) = std::env::var("MCP_SERVICE_NAME") {
209 config.tracing.service_name = val;
210 }
211
212 if let Ok(val) = std::env::var("MCP_ENVIRONMENT") {
213 config.tracing.environment = val;
214 }
215
216 if let Ok(val) = std::env::var("MCP_LOG_LEVEL") {
217 config.tracing.level = val;
218 }
219
220 if let Ok(val) = std::env::var("MCP_JAEGER_ENDPOINT") {
221 if let Some(jaeger) = config.tracing.jaeger.as_mut() {
222 jaeger.agent_endpoint = val;
223 }
224 }
225
226 if let Ok(val) = std::env::var("MCP_METRICS_ENABLED") {
227 config.metrics.enabled = val.parse().unwrap_or(true);
228 }
229
230 if let Ok(val) = std::env::var("MCP_HTTP_PORT") {
231 config.http.port = val.parse().unwrap_or(9090);
232 }
233
234 config
235 }
236
237 #[cfg(feature = "config-files")]
239 pub fn from_file(path: &str) -> anyhow::Result<Self> {
240 let content = std::fs::read_to_string(path)?;
241 let config = match path.split('.').next_back() {
242 Some("toml") => toml::from_str(&content)?,
243 #[cfg(feature = "config-files")]
244 Some("yaml") | Some("yml") => serde_yaml::from_str(&content)?,
245 Some("json") => serde_json::from_str(&content)?,
246 _ => return Err(anyhow::anyhow!("Unsupported file format")),
247 };
248 Ok(config)
249 }
250
251 #[cfg(not(feature = "config-files"))]
253 pub fn from_file(_path: &str) -> anyhow::Result<Self> {
254 Err(anyhow::anyhow!(
255 "Config file support not enabled. Enable 'config-files' feature."
256 ))
257 }
258}