diff --git a/src/db/metrics.rs b/src/db/metrics.rs index 897a1600..69d58352 100644 --- a/src/db/metrics.rs +++ b/src/db/metrics.rs @@ -29,12 +29,12 @@ macro_rules! db_metric { ($operation:expr, $code:block) => {{ #[cfg(feature = "enable_metrics")] let timer = crate::db::metrics::DbOperationTimer::new($operation); - + let result = $code; - + #[cfg(feature = "enable_metrics")] timer.finish(); - + result }}; } @@ -46,20 +46,20 @@ pub async fn update_pool_metrics(_pool: &crate::db::DbPool) { // Note: This is a simplified implementation // In a real implementation, you'd want to get actual pool statistics // from the connection pool (r2d2 provides some stats) - + // For now, we'll just update with basic info let db_type = crate::db::DbConnType::from_url(&crate::CONFIG.database_url()) .map(|t| match t { crate::db::DbConnType::sqlite => "sqlite", - crate::db::DbConnType::mysql => "mysql", + crate::db::DbConnType::mysql => "mysql", crate::db::DbConnType::postgresql => "postgresql", }) .unwrap_or("unknown"); - + // These would be actual pool statistics in a real implementation let active_connections = 1; // placeholder let idle_connections = crate::CONFIG.database_max_conns() as i64 - active_connections; - + crate::metrics::update_db_connections(db_type, active_connections, idle_connections); } } diff --git a/src/metrics.rs b/src/metrics.rs index 0bcb9370..6da045b2 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -1,18 +1,20 @@ #![allow(dead_code, unused_imports)] +use std::time::SystemTime; + #[cfg(feature = "enable_metrics")] use once_cell::sync::Lazy; #[cfg(feature = "enable_metrics")] use prometheus::{ - register_gauge_vec, register_histogram_vec, register_int_counter_vec, register_int_gauge_vec, - Encoder, GaugeVec, HistogramVec, IntCounterVec, IntGaugeVec, TextEncoder, + register_gauge_vec, register_histogram_vec, register_int_counter_vec, register_int_gauge_vec, Encoder, GaugeVec, + HistogramVec, IntCounterVec, IntGaugeVec, TextEncoder, }; use crate::{db::DbConn, error::Error, CONFIG}; #[cfg(feature = "enable_metrics")] use std::sync::{Arc, RwLock}; #[cfg(feature = "enable_metrics")] -use std::time::{SystemTime, UNIX_EPOCH}; +use std::time::UNIX_EPOCH; // HTTP request metrics #[cfg(feature = "enable_metrics")] diff --git a/src/metrics_test.rs b/src/metrics_test.rs index 8d1ca85b..4af5f429 100644 --- a/src/metrics_test.rs +++ b/src/metrics_test.rs @@ -52,7 +52,7 @@ mod tests { fn test_build_info_initialization() { // Test build info metrics initialization init_build_info(); - + // Test uptime metrics let start_time = std::time::SystemTime::now(); update_uptime(start_time); @@ -68,10 +68,10 @@ mod tests { // Test gathering all metrics let metrics_output = gather_metrics(); assert!(metrics_output.is_ok()); - + let metrics_text = metrics_output.unwrap(); assert!(!metrics_text.is_empty()); - + // Should contain Prometheus format headers assert!(metrics_text.contains("# HELP")); assert!(metrics_text.contains("# TYPE")); @@ -81,17 +81,17 @@ mod tests { async fn test_business_metrics_collection() { // This test would require a mock database connection // For now, we just test that the function doesn't panic - + // In a real test, you would: // 1. Create a test database // 2. Insert test data (users, organizations, ciphers) // 3. Call update_business_metrics // 4. Verify the metrics were updated correctly - + // Placeholder test - in production this would use a mock DbConn assert!(true); } - + #[test] fn test_path_normalization() { // Test that path normalization works for metric cardinality control @@ -99,22 +99,22 @@ mod tests { increment_http_requests("GET", "/api/accounts/123/profile", 200); increment_http_requests("POST", "/api/organizations/456/users", 201); increment_http_requests("PUT", "/api/ciphers/789", 200); - + // Test that gather_metrics works let result = gather_metrics(); assert!(result.is_ok()); - + let metrics_text = result.unwrap(); // Paths should be normalized in the actual implementation // This test verifies the collection doesn't panic assert!(!metrics_text.is_empty()); } - + #[test] fn test_concurrent_metrics_collection() { use std::sync::Arc; use std::thread; - + // Test concurrent access to metrics let handles: Vec<_> = (0..10).map(|i| { thread::spawn(move || { @@ -123,12 +123,12 @@ mod tests { update_db_connections("sqlite", i, 10 - i); }) }).collect(); - + // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } - + // Verify metrics collection still works let result = gather_metrics(); assert!(result.is_ok()); @@ -149,7 +149,7 @@ mod tests { increment_auth_attempts("password", "success"); update_user_sessions("authenticated", 150); init_build_info(); - + let start_time = std::time::SystemTime::now(); update_uptime(start_time); @@ -164,15 +164,15 @@ mod tests { // This should also be a no-op when metrics are disabled // We can't test with a real DbConn without significant setup, // but we can verify it doesn't panic - + // In a real implementation, you'd mock DbConn assert!(true); } - + #[test] fn test_concurrent_no_op_calls() { use std::thread; - + // Test that concurrent calls to disabled metrics don't cause issues let handles: Vec<_> = (0..5).map(|i| { thread::spawn(move || { @@ -182,11 +182,11 @@ mod tests { increment_auth_attempts("password", "success"); }) }).collect(); - + for handle in handles { handle.join().unwrap(); } - + // All calls should be no-ops let result = gather_metrics(); assert!(result.is_ok());