Skip to content

Commit 66b0294

Browse files
feat(enterprise): implement comprehensive stats and metrics commands (#240)
* feat(enterprise): implement comprehensive stats and metrics commands Implements issue #156 - comprehensive statistics and metrics collection commands for Redis Enterprise API. ## Commands Added ### Database Statistics - `redisctl enterprise stats database <id>` - Get database statistics - `redisctl enterprise stats database-shards <id>` - Get database shard statistics - `redisctl enterprise stats database-metrics <id> --interval 1h` - Get database metrics over time ### Node Statistics - `redisctl enterprise stats node <id>` - Get node statistics - `redisctl enterprise stats node-metrics <id> --interval 1h` - Get node metrics over time ### Cluster Statistics - `redisctl enterprise stats cluster` - Get cluster-wide statistics - `redisctl enterprise stats cluster-metrics --interval 1h` - Get cluster metrics over time ### Listener & Export - `redisctl enterprise stats listener` - Get listener statistics - `redisctl enterprise stats export --format json --interval 1h` - Export statistics in various formats ## Implementation Notes - Uses redis-enterprise StatsHandler for all operations - Supports various time intervals (1m, 5m, 1h, 1d) - Handles large datasets efficiently with proper JSON serialization - Consistent error handling and output formatting - Standard JMESPath filtering and output format support ## Testing - Compiles cleanly with proper type safety - Follows existing enterprise command patterns - Uses established connection management and output utilities This provides essential monitoring capabilities for Redis Enterprise deployments, complementing the existing database and node management commands. * chore: add #[allow(dead_code)] to suppress false positive warnings These functions are used by the binary but appear unused from the library's perspective. * fix(enterprise): correct LastStatsResponse structure to match actual API The API returns stats fields directly with stime/etime, not wrapped in a metrics object. This fixes the 'error decoding response body' issue when running stats commands. * fix(enterprise): fix stats test data format The LastStatsResponse struct uses serde(flatten) to include metrics at the top level, not in a nested 'metrics' field. Updated test data to match the actual API response format.
1 parent 4050023 commit 66b0294

File tree

6 files changed

+395
-28
lines changed

6 files changed

+395
-28
lines changed

crates/redis-enterprise/src/stats.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ pub struct StatsInterval {
9393
}
9494

9595
/// Last stats response for single resource
96+
/// Response for last stats endpoint - the API returns metrics directly
9697
#[derive(Debug, Clone, Serialize, Deserialize)]
9798
pub struct LastStatsResponse {
98-
pub time: String,
99-
pub metrics: Value,
99+
pub stime: Option<String>,
100+
pub etime: Option<String>,
101+
pub interval: Option<String>,
100102
#[serde(flatten)]
101-
pub extra: Value,
103+
pub metrics: Value, // All other fields are metrics
102104
}
103105

104106
/// Aggregated stats response for multiple resources

crates/redis-enterprise/tests/stats_tests.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,41 +101,41 @@ fn test_shard_stats() -> serde_json::Value {
101101

102102
fn test_cluster_last_stats() -> serde_json::Value {
103103
json!({
104-
"time": "2023-01-01T12:02:00Z",
105-
"metrics": {
106-
"cpu_usage": 28.3,
107-
"memory_usage": 77.5,
108-
"network_in": 1150000,
109-
"network_out": 2300000,
110-
"total_req": 158000
111-
}
104+
"stime": "2023-01-01T12:00:00Z",
105+
"etime": "2023-01-01T12:02:00Z",
106+
"interval": "2m",
107+
"cpu_usage": 28.3,
108+
"memory_usage": 77.5,
109+
"network_in": 1150000,
110+
"network_out": 2300000,
111+
"total_req": 158000
112112
})
113113
}
114114

115115
fn test_node_last_stats() -> serde_json::Value {
116116
json!({
117-
"time": "2023-01-01T12:02:00Z",
118-
"metrics": {
119-
"cpu_user": 16.2,
120-
"cpu_system": 5.8,
121-
"cpu_idle": 78.0,
122-
"free_memory": 4194304000u64,
123-
"network_bytes_in": 520000,
124-
"network_bytes_out": 1040000
125-
}
117+
"stime": "2023-01-01T12:00:00Z",
118+
"etime": "2023-01-01T12:02:00Z",
119+
"interval": "2m",
120+
"cpu_user": 16.2,
121+
"cpu_system": 5.8,
122+
"cpu_idle": 78.0,
123+
"free_memory": 4194304000u64,
124+
"network_bytes_in": 520000,
125+
"network_bytes_out": 1040000
126126
})
127127
}
128128

129129
fn test_database_last_stats() -> serde_json::Value {
130130
json!({
131-
"time": "2023-01-01T12:02:00Z",
132-
"metrics": {
133-
"used_memory": 1100000,
134-
"total_req": 5200,
135-
"ops_per_sec": 105.2,
136-
"hits": 4680,
137-
"misses": 520
138-
}
131+
"stime": "2023-01-01T12:00:00Z",
132+
"etime": "2023-01-01T12:02:00Z",
133+
"interval": "2m",
134+
"used_memory": 1100000,
135+
"total_req": 5200,
136+
"ops_per_sec": 105.2,
137+
"hits": 4680,
138+
"misses": 520
139139
})
140140
}
141141

crates/redisctl/src/cli.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ pub enum EnterpriseCommands {
996996
/// Module management operations
997997
#[command(subcommand)]
998998
Module(crate::commands::enterprise::module::ModuleCommands),
999+
1000+
/// Statistics and metrics operations
1001+
#[command(subcommand)]
1002+
Stats(EnterpriseStatsCommands),
9991003
}
10001004

10011005
// Placeholder command structures - will be expanded in later PRs
@@ -2549,3 +2553,65 @@ pub enum EnterpriseCrdbCommands {
25492553
data: String,
25502554
},
25512555
}
2556+
2557+
#[derive(Subcommand, Debug, Clone)]
2558+
pub enum EnterpriseStatsCommands {
2559+
/// Get database statistics
2560+
Database {
2561+
/// Database ID
2562+
id: u32,
2563+
},
2564+
2565+
/// Get database shard statistics
2566+
DatabaseShards {
2567+
/// Database ID
2568+
id: u32,
2569+
},
2570+
2571+
/// Get database metrics over time
2572+
DatabaseMetrics {
2573+
/// Database ID
2574+
id: u32,
2575+
/// Time interval (1m, 5m, 1h, 1d)
2576+
#[arg(long, default_value = "1h")]
2577+
interval: String,
2578+
},
2579+
2580+
/// Get node statistics
2581+
Node {
2582+
/// Node ID
2583+
id: u32,
2584+
},
2585+
2586+
/// Get node metrics over time
2587+
NodeMetrics {
2588+
/// Node ID
2589+
id: u32,
2590+
/// Time interval (1m, 5m, 1h, 1d)
2591+
#[arg(long, default_value = "1h")]
2592+
interval: String,
2593+
},
2594+
2595+
/// Get cluster-wide statistics
2596+
Cluster,
2597+
2598+
/// Get cluster metrics over time
2599+
ClusterMetrics {
2600+
/// Time interval (1m, 5m, 1h, 1d)
2601+
#[arg(long, default_value = "1h")]
2602+
interval: String,
2603+
},
2604+
2605+
/// Get listener statistics
2606+
Listener,
2607+
2608+
/// Export statistics in various formats
2609+
Export {
2610+
/// Export format (json, prometheus, csv)
2611+
#[arg(long, default_value = "json")]
2612+
format: String,
2613+
/// Time interval for time-series data (1m, 5m, 1h, 1d)
2614+
#[arg(long)]
2615+
interval: Option<String>,
2616+
},
2617+
}

crates/redisctl/src/commands/enterprise/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pub mod node;
1414
pub mod node_impl;
1515
pub mod rbac;
1616
pub mod rbac_impl;
17+
pub mod stats;
1718
pub mod utils;

0 commit comments

Comments
 (0)