mocopr-core 0.1.0

Core types and protocol implementation for MoCoPr (Model Context Protocol)
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Comprehensive monitoring and observability system for MoCoPr
// This provides production-ready monitoring capabilities

use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::RwLock;
use tracing::{debug, error, warn};

/// Health check status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum HealthStatus {
    /// System is healthy
    Healthy,
    /// System is degraded but operational
    Degraded,
    /// System is unhealthy and may not function correctly
    Unhealthy,
    /// System is in unknown state
    Unknown,
}

/// Individual health check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckResult {
    /// Name of the health check
    pub name: String,
    /// Status of the health check
    pub status: HealthStatus,
    /// Optional message providing details
    pub message: Option<String>,
    /// Time when the check was performed
    pub timestamp: SystemTime,
    /// Duration the check took to complete
    pub duration: Duration,
}

/// Overall system health report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    /// Overall status (worst of all individual checks)
    pub status: HealthStatus,
    /// Individual check results
    pub checks: Vec<HealthCheckResult>,
    /// Time when the report was generated
    pub timestamp: SystemTime,
    /// Total time to generate the report
    pub total_duration: Duration,
}

/// Trait for implementing health checks
#[async_trait::async_trait]
pub trait HealthCheck: Send + Sync {
    /// Name of the health check
    fn name(&self) -> &str;

    /// Perform the health check
    async fn check(&self) -> HealthCheckResult;
}

/// Performance metrics for monitoring
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Total number of requests processed
    pub total_requests: u64,
    /// Number of successful requests
    pub successful_requests: u64,
    /// Number of failed requests
    pub failed_requests: u64,
    /// Average response time in milliseconds
    pub avg_response_time_ms: f64,
    /// 95th percentile response time in milliseconds
    pub p95_response_time_ms: f64,
    /// 99th percentile response time in milliseconds
    pub p99_response_time_ms: f64,
    /// Current active connections
    pub active_connections: u64,
    /// Memory usage in bytes
    pub memory_usage_bytes: u64,
    /// CPU usage percentage
    pub cpu_usage_percent: f64,
    /// Timestamp when metrics were collected
    pub timestamp: SystemTime,
}

/// Request metrics for tracking individual operations
#[derive(Debug, Clone)]
pub struct RequestMetrics {
    /// Request start time
    pub start_time: Instant,
    /// Request method/operation
    pub method: String,
    /// Request success status
    pub success: bool,
    /// Response time
    pub response_time: Duration,
    /// Error message if failed
    pub error_message: Option<String>,
}

/// Comprehensive monitoring system
pub struct MonitoringSystem {
    /// Registered health checks
    health_checks: Arc<RwLock<Vec<Box<dyn HealthCheck>>>>,
    /// Performance metrics
    metrics: Arc<RwLock<PerformanceMetrics>>,
    /// Recent response times for percentile calculations
    response_times: Arc<RwLock<Vec<Duration>>>,
    /// Configuration
    config: MonitoringConfig,
}

/// Configuration for monitoring system
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
    /// Maximum number of response times to keep in memory
    pub max_response_times: usize,
    /// Health check interval
    pub health_check_interval: Duration,
    /// Enable detailed logging
    pub detailed_logging: bool,
}

impl Default for MonitoringConfig {
    fn default() -> Self {
        Self {
            max_response_times: 10000,
            health_check_interval: Duration::from_secs(30),
            detailed_logging: true,
        }
    }
}

impl MonitoringSystem {
    /// Create a new monitoring system
    pub fn new(config: MonitoringConfig) -> Self {
        Self {
            health_checks: Arc::new(RwLock::new(Vec::new())),
            metrics: Arc::new(RwLock::new(PerformanceMetrics::default())),
            response_times: Arc::new(RwLock::new(Vec::new())),
            config,
        }
    }

    /// Register a health check
    pub async fn register_health_check(&self, check: Box<dyn HealthCheck>) {
        let mut checks = self.health_checks.write().await;
        checks.push(check);
    }

    /// Run all health checks and generate a report
    pub async fn health_check(&self) -> HealthReport {
        let start_time = Instant::now();
        let mut results = Vec::new();
        let mut overall_status = HealthStatus::Healthy;

        let checks = self.health_checks.read().await;

        for check in checks.iter() {
            let result = check.check().await;

            // Update overall status (worst case)
            match (&overall_status, &result.status) {
                (HealthStatus::Healthy, HealthStatus::Degraded) => {
                    overall_status = HealthStatus::Degraded
                }
                (HealthStatus::Healthy | HealthStatus::Degraded, HealthStatus::Unhealthy) => {
                    overall_status = HealthStatus::Unhealthy
                }
                (
                    HealthStatus::Healthy | HealthStatus::Degraded | HealthStatus::Unhealthy,
                    HealthStatus::Unknown,
                ) => overall_status = HealthStatus::Unknown,
                _ => {}
            }

            results.push(result);
        }

        let total_duration = start_time.elapsed();

        HealthReport {
            status: overall_status,
            checks: results,
            timestamp: SystemTime::now(),
            total_duration,
        }
    }

    /// Record a request for metrics
    pub async fn record_request(&self, request: RequestMetrics) {
        let mut metrics = self.metrics.write().await;
        let mut response_times = self.response_times.write().await;

        // Update basic counters
        metrics.total_requests += 1;
        if request.success {
            metrics.successful_requests += 1;
        } else {
            metrics.failed_requests += 1;
        }

        // Update response times
        response_times.push(request.response_time);

        // Keep only recent response times
        let current_len = response_times.len();
        if current_len > self.config.max_response_times {
            response_times.drain(0..current_len - self.config.max_response_times);
        }

        // Calculate percentiles
        let mut sorted_times = response_times.clone();
        sorted_times.sort();

        if !sorted_times.is_empty() {
            let avg_ms = sorted_times.iter().sum::<Duration>().as_secs_f64() * 1000.0
                / sorted_times.len() as f64;
            let p95_idx = (sorted_times.len() as f64 * 0.95) as usize;
            let p99_idx = (sorted_times.len() as f64 * 0.99) as usize;

            metrics.avg_response_time_ms = avg_ms;
            metrics.p95_response_time_ms = sorted_times
                .get(p95_idx)
                .unwrap_or(&Duration::ZERO)
                .as_secs_f64()
                * 1000.0;
            metrics.p99_response_time_ms = sorted_times
                .get(p99_idx)
                .unwrap_or(&Duration::ZERO)
                .as_secs_f64()
                * 1000.0;
        }

        // Update timestamp
        metrics.timestamp = SystemTime::now();

        // Log request if detailed logging is enabled
        if self.config.detailed_logging {
            if request.success {
                debug!(
                    "Request completed: {} in {:?}",
                    request.method, request.response_time
                );
            } else {
                warn!(
                    "Request failed: {} in {:?} - {}",
                    request.method,
                    request.response_time,
                    request
                        .error_message
                        .unwrap_or_else(|| "Unknown error".to_string())
                );
            }
        }
    }

    /// Get current performance metrics
    pub async fn get_metrics(&self) -> PerformanceMetrics {
        self.metrics.read().await.clone()
    }

    /// Start periodic health checks
    pub async fn start_periodic_health_checks(&self) {
        let health_checks = self.health_checks.clone();
        let interval = self.config.health_check_interval;

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                let checks = health_checks.read().await;
                for check in checks.iter() {
                    let result = check.check().await;

                    match result.status {
                        HealthStatus::Healthy => {
                            debug!("Health check '{}' passed", result.name);
                        }
                        HealthStatus::Degraded => {
                            warn!(
                                "Health check '{}' degraded: {}",
                                result.name,
                                result.message.unwrap_or_else(|| "No details".to_string())
                            );
                        }
                        HealthStatus::Unhealthy => {
                            error!(
                                "Health check '{}' failed: {}",
                                result.name,
                                result.message.unwrap_or_else(|| "No details".to_string())
                            );
                        }
                        HealthStatus::Unknown => {
                            warn!(
                                "Health check '{}' status unknown: {}",
                                result.name,
                                result.message.unwrap_or_else(|| "No details".to_string())
                            );
                        }
                    }
                }
            }
        });
    }

    /// Update system resource metrics
    pub async fn update_system_metrics(&self, active_connections: u64) {
        let mut metrics = self.metrics.write().await;
        metrics.active_connections = active_connections;

        // Update system resource usage
        #[cfg(target_os = "linux")]
        {
            if let Ok(usage) = self.get_system_usage().await {
                metrics.memory_usage_bytes = usage.memory_bytes;
                metrics.cpu_usage_percent = usage.cpu_percent;
            }
        }
    }

    /// Get system resource usage (Linux only)
    #[cfg(target_os = "linux")]
    async fn get_system_usage(&self) -> Result<SystemUsage, Box<dyn std::error::Error>> {
        use std::fs;

        // Read memory usage from /proc/self/status
        let status = fs::read_to_string("/proc/self/status")?;
        let memory_kb = status
            .lines()
            .find(|line| line.starts_with("VmRSS:"))
            .and_then(|line| line.split_whitespace().nth(1))
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(0);

        // Read CPU usage from /proc/self/stat
        let stat = fs::read_to_string("/proc/self/stat")?;
        let fields: Vec<&str> = stat.split_whitespace().collect();
        let utime = fields
            .get(13)
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(0);
        let stime = fields
            .get(14)
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(0);

        // Simple CPU usage calculation (this is a simplified version)
        let cpu_percent = ((utime + stime) as f64 / 100.0) * 0.1; // Rough estimate

        Ok(SystemUsage {
            memory_bytes: memory_kb * 1024,
            cpu_percent,
        })
    }
}

#[cfg(target_os = "linux")]
struct SystemUsage {
    memory_bytes: u64,
    cpu_percent: f64,
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            total_requests: 0,
            successful_requests: 0,
            failed_requests: 0,
            avg_response_time_ms: 0.0,
            p95_response_time_ms: 0.0,
            p99_response_time_ms: 0.0,
            active_connections: 0,
            memory_usage_bytes: 0,
            cpu_usage_percent: 0.0,
            timestamp: SystemTime::now(),
        }
    }
}

/// Built-in health check for basic system status
pub struct BasicHealthCheck {
    name: String,
}

impl BasicHealthCheck {
    /// Create a new HTTP health check
    pub fn new(name: String) -> Self {
        Self { name }
    }
}

#[async_trait::async_trait]
impl HealthCheck for BasicHealthCheck {
    fn name(&self) -> &str {
        &self.name
    }

    async fn check(&self) -> HealthCheckResult {
        let start_time = Instant::now();

        // Basic system health check
        let status = if std::env::var("HEALTH_CHECK_FAIL").is_ok() {
            HealthStatus::Unhealthy
        } else {
            HealthStatus::Healthy
        };

        let duration = start_time.elapsed();

        HealthCheckResult {
            name: self.name.clone(),
            status,
            message: Some("Basic system health check".to_string()),
            timestamp: SystemTime::now(),
            duration,
        }
    }
}

/// Health check for file system access
pub struct FileSystemHealthCheck {
    test_path: std::path::PathBuf,
}

impl FileSystemHealthCheck {
    /// Create a new file health check
    pub fn new(test_path: std::path::PathBuf) -> Self {
        Self { test_path }
    }
}

#[async_trait::async_trait]
impl HealthCheck for FileSystemHealthCheck {
    fn name(&self) -> &str {
        "filesystem"
    }

    async fn check(&self) -> HealthCheckResult {
        let start_time = Instant::now();

        let (status, message) = match std::fs::metadata(&self.test_path) {
            Ok(_) => (HealthStatus::Healthy, "File system accessible".to_string()),
            Err(e) => (
                HealthStatus::Unhealthy,
                format!("File system check failed: {}", e),
            ),
        };

        let duration = start_time.elapsed();

        HealthCheckResult {
            name: "filesystem".to_string(),
            status,
            message: Some(message),
            timestamp: SystemTime::now(),
            duration,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[tokio::test]
    async fn test_monitoring_system() {
        let config = MonitoringConfig::default();
        let monitoring = MonitoringSystem::new(config);

        // Register a health check
        let health_check = Box::new(BasicHealthCheck::new("test".to_string()));
        monitoring.register_health_check(health_check).await;

        // Run health check
        let report = monitoring.health_check().await;
        assert_eq!(report.status, HealthStatus::Healthy);
        assert_eq!(report.checks.len(), 1);

        // Record a request
        let request = RequestMetrics {
            start_time: Instant::now(),
            method: "test_method".to_string(),
            success: true,
            response_time: Duration::from_millis(100),
            error_message: None,
        };

        monitoring.record_request(request).await;

        // Check metrics
        let metrics = monitoring.get_metrics().await;
        assert_eq!(metrics.total_requests, 1);
        assert_eq!(metrics.successful_requests, 1);
        assert_eq!(metrics.failed_requests, 0);
    }

    #[tokio::test]
    async fn test_health_check_aggregation() {
        let config = MonitoringConfig::default();
        let monitoring = MonitoringSystem::new(config);

        // Register multiple health checks with different statuses
        let healthy_check = Box::new(BasicHealthCheck::new("healthy".to_string()));
        let degraded_check = Box::new(FileSystemHealthCheck::new(std::path::PathBuf::from(
            "/nonexistent",
        )));

        monitoring.register_health_check(healthy_check).await;
        monitoring.register_health_check(degraded_check).await;

        // Run health check
        let report = monitoring.health_check().await;

        // Overall status should be the worst individual status
        assert_eq!(report.checks.len(), 2);
        // The overall status will be unhealthy due to the nonexistent path
        assert_eq!(report.status, HealthStatus::Unhealthy);
    }
}