inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Metrics Command
//!
//! This module provides metrics collection and export functionality with support
//! for JSON, Prometheus, and snapshot formats. Also includes a standalone metrics
//! HTTP server for production monitoring.

use crate::interfaces::cli::{Command, CommandContext, CommandOutput};
use crate::{config::Config, metrics::MetricsCollector};
use anyhow::Result;
use async_trait::async_trait;
use axum::{Json, extract::State, http::StatusCode, response::IntoResponse};
use clap::{Args, Subcommand};
use serde_json::json;
use std::sync::Arc;
use tokio::signal;
use tracing::info;

#[derive(Args)]
pub struct MetricsArgs {
    #[command(subcommand)]
    pub command: MetricsCommand,
}

#[derive(Subcommand)]
pub enum MetricsCommand {
    #[command(about = "Export metrics in JSON format")]
    Json,

    #[command(about = "Export metrics in Prometheus format")]
    Prometheus,

    #[command(about = "Show detailed metrics snapshot")]
    Snapshot {
        #[arg(short, long, help = "Pretty print JSON output")]
        pretty: bool,
    },

    #[command(about = "Start standalone metrics server")]
    Server {
        #[arg(
            short,
            long,
            help = "Server bind address (format: host:port)",
            default_value = "127.0.0.1:9090"
        )]
        bind: String,
    },
}

// ============================================================================
// Command Trait Implementations
// ============================================================================

/// Export metrics in JSON format
pub struct MetricsJsonCommand {
    #[allow(dead_code)]
    config: Config,
}

impl MetricsJsonCommand {
    pub fn new(config: Config) -> Self {
        Self { config }
    }
}

#[async_trait]
impl Command for MetricsJsonCommand {
    fn name(&self) -> &str {
        "metrics json"
    }

    fn description(&self) -> &str {
        "Export metrics in JSON format"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        // No validation needed for JSON export
        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Exporting metrics in JSON format");

        let (collector, processor) = MetricsCollector::new();
        processor.start();

        let json_output = collector.export_metrics_json().await?;

        // Human-readable output (already JSON)
        if !ctx.json_output {
            println!("{}", json_output);
        }

        // Structured output - parse the JSON string back to Value
        let metrics_value: serde_json::Value = serde_json::from_str(&json_output)?;

        Ok(CommandOutput::success_with_data(
            "Metrics exported in JSON format",
            json!({
                "format": "json",
                "metrics": metrics_value,
            }),
        ))
    }
}

/// Export metrics in Prometheus format
pub struct MetricsPrometheusCommand {
    #[allow(dead_code)]
    config: Config,
}

impl MetricsPrometheusCommand {
    pub fn new(config: Config) -> Self {
        Self { config }
    }
}

#[async_trait]
impl Command for MetricsPrometheusCommand {
    fn name(&self) -> &str {
        "metrics prometheus"
    }

    fn description(&self) -> &str {
        "Export metrics in Prometheus format"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        // No validation needed for Prometheus export
        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Exporting metrics in Prometheus format");

        let (collector, processor) = MetricsCollector::new();
        processor.start();

        let prometheus_output = collector.export_prometheus_format().await?;

        // Human-readable output (Prometheus text format)
        if !ctx.json_output {
            println!("{}", prometheus_output);
        }

        // Structured output
        Ok(CommandOutput::success_with_data(
            "Metrics exported in Prometheus format",
            json!({
                "format": "prometheus",
                "content_type": "text/plain; version=0.0.4; charset=utf-8",
                "metrics": prometheus_output,
            }),
        ))
    }
}

/// Show detailed metrics snapshot
pub struct MetricsSnapshotCommand {
    #[allow(dead_code)]
    config: Config,
    pretty: bool,
}

impl MetricsSnapshotCommand {
    pub fn new(config: Config, pretty: bool) -> Self {
        Self { config, pretty }
    }
}

#[async_trait]
impl Command for MetricsSnapshotCommand {
    fn name(&self) -> &str {
        "metrics snapshot"
    }

    fn description(&self) -> &str {
        "Show detailed metrics snapshot"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        // No validation needed for snapshot
        Ok(())
    }

    async fn execute(&self, ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Creating metrics snapshot");

        let (collector, processor) = MetricsCollector::new();
        processor.start();

        let snapshot = collector.get_snapshot().await?;

        // Human-readable output
        if !ctx.json_output {
            if self.pretty {
                println!("{}", serde_json::to_string_pretty(&snapshot)?);
            } else {
                println!("{}", serde_json::to_string(&snapshot)?);
            }
        }

        // Structured output
        Ok(CommandOutput::success_with_data(
            "Metrics snapshot created",
            json!({
                "snapshot": snapshot,
                "timestamp": chrono::Utc::now().to_rfc3339(),
                "pretty": self.pretty,
            }),
        ))
    }
}

/// Start standalone metrics server
pub struct MetricsServerCommand {
    #[allow(dead_code)]
    config: Config,
    bind: String,
}

impl MetricsServerCommand {
    pub fn new(config: Config, bind: String) -> Self {
        Self { config, bind }
    }
}

#[async_trait]
impl Command for MetricsServerCommand {
    fn name(&self) -> &str {
        "metrics server"
    }

    fn description(&self) -> &str {
        "Start standalone metrics server"
    }

    async fn validate(&self, _ctx: &CommandContext) -> Result<()> {
        // Validate bind address format
        validate_bind_address(&self.bind)?;
        Ok(())
    }

    async fn execute(&self, _ctx: &mut CommandContext) -> Result<CommandOutput> {
        info!("Starting standalone metrics server on {}", self.bind);
        start_metrics_server(&self.bind).await?;

        Ok(CommandOutput::success_with_data(
            "Metrics server stopped",
            json!({
                "bind_address": self.bind,
                "status": "stopped",
            }),
        ))
    }
}

/// Validate a bind address format (host:port)
fn validate_bind_address(bind: &str) -> Result<()> {
    let parts: Vec<&str> = bind.split(':').collect();
    if parts.len() != 2 {
        anyhow::bail!(
            "Invalid bind address format '{}'. Expected format: host:port (e.g., 127.0.0.1:9090)",
            bind
        );
    }

    let host = parts[0];
    let port_str = parts[1];

    // Validate port is a number
    let port: u16 = port_str.parse().map_err(|_| {
        anyhow::anyhow!(
            "Invalid port '{}' in bind address. Port must be a number between 1 and 65535",
            port_str
        )
    })?;

    // Validate port is in valid range
    if port == 0 {
        anyhow::bail!("Port 0 is not allowed. Please specify a port between 1 and 65535");
    }

    // Basic host validation - allow IP addresses and hostnames
    if host.is_empty() {
        anyhow::bail!("Host cannot be empty in bind address");
    }

    Ok(())
}

pub async fn execute(args: MetricsArgs, _config: &Config) -> Result<()> {
    match args.command {
        MetricsCommand::Json => {
            let (collector, processor) = MetricsCollector::new();
            processor.start();

            let json_output = collector.export_metrics_json().await?;
            println!("{}", json_output);
        }

        MetricsCommand::Prometheus => {
            let (collector, processor) = MetricsCollector::new();
            processor.start();

            let prometheus_output = collector.export_prometheus_format().await?;
            println!("{}", prometheus_output);
        }

        MetricsCommand::Snapshot { pretty } => {
            let (collector, processor) = MetricsCollector::new();
            processor.start();

            let snapshot = collector.get_snapshot().await?;

            if pretty {
                println!("{}", serde_json::to_string_pretty(&snapshot)?);
            } else {
                println!("{}", serde_json::to_string(&snapshot)?);
            }
        }

        MetricsCommand::Server { bind } => {
            info!("Starting standalone metrics server on {}", bind);
            start_metrics_server(&bind).await?;
        }
    }

    Ok(())
}

async fn start_metrics_server(bind_addr: &str) -> Result<()> {
    use axum::{Router, routing::get};

    use std::sync::Arc;

    use tower::ServiceBuilder;
    use tower_http::{cors::CorsLayer, trace::TraceLayer};

    // Initialize metrics collector
    let (metrics_collector, processor) = MetricsCollector::new();
    processor.start();

    let state = Arc::new(MetricsServerState {
        metrics: metrics_collector,
    });

    // Build the router with metrics endpoints only
    let app = Router::new()
        .route("/", get(metrics_root))
        .route("/metrics", get(metrics_prometheus_handler))
        .route("/metrics/json", get(metrics_json_handler))
        .route("/metrics/snapshot", get(metrics_snapshot_handler))
        .route("/health", get(health_check))
        .layer(
            ServiceBuilder::new()
                .layer(TraceLayer::new_for_http())
                .layer(CorsLayer::permissive()),
        )
        .with_state(state);

    info!("Metrics server endpoints:");
    info!("  GET  /             - Server information");
    info!("  GET  /health       - Health check");
    info!("  GET  /metrics      - Prometheus metrics");
    info!("  GET  /metrics/json - JSON metrics");
    info!("  GET  /metrics/snapshot - Detailed metrics snapshot");

    // Create the listener
    let listener = tokio::net::TcpListener::bind(bind_addr).await?;

    info!("Metrics server running on http://{}", bind_addr);

    // Run the server with graceful shutdown
    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal())
        .await?;

    info!("Metrics server shut down gracefully");
    Ok(())
}

struct MetricsServerState {
    metrics: MetricsCollector,
}

async fn metrics_root() -> impl IntoResponse {
    Json(json!({
        "name": "Inferno Metrics Server",
        "version": std::env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.1.0".to_string()),
        "endpoints": {
            "/health": "Health check",
            "/metrics": "Prometheus metrics",
            "/metrics/json": "JSON formatted metrics",
            "/metrics/snapshot": "Detailed metrics snapshot"
        }
    }))
}

async fn health_check() -> impl IntoResponse {
    Json(json!({
        "status": "healthy",
        "timestamp": chrono::Utc::now().to_rfc3339()
    }))
}

async fn metrics_prometheus_handler(
    State(state): State<Arc<MetricsServerState>>,
) -> impl IntoResponse {
    use axum::http::header;

    match state.metrics.export_prometheus_format().await {
        Ok(metrics) => (
            StatusCode::OK,
            [(
                header::CONTENT_TYPE,
                "text/plain; version=0.0.4; charset=utf-8",
            )],
            metrics,
        )
            .into_response(),
        Err(e) => {
            tracing::warn!("Failed to export Prometheus metrics: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to export metrics",
            )
                .into_response()
        }
    }
}

async fn metrics_json_handler(State(state): State<Arc<MetricsServerState>>) -> impl IntoResponse {
    match state.metrics.export_metrics_json().await {
        Ok(metrics_json) => (StatusCode::OK, metrics_json).into_response(),
        Err(e) => {
            tracing::warn!("Failed to export JSON metrics: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to export metrics",
            )
                .into_response()
        }
    }
}

async fn metrics_snapshot_handler(
    State(state): State<Arc<MetricsServerState>>,
) -> impl IntoResponse {
    match state.metrics.get_snapshot().await {
        Ok(snapshot) => Json(snapshot).into_response(),
        Err(e) => {
            tracing::warn!("Failed to get metrics snapshot: {}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({
                    "error": "Failed to get metrics snapshot"
                })),
            )
                .into_response()
        }
    }
}

async fn shutdown_signal() {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }

    info!("Shutdown signal received");
}

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

    #[test]
    fn test_validate_bind_address_valid() {
        assert!(validate_bind_address("127.0.0.1:9090").is_ok());
        assert!(validate_bind_address("0.0.0.0:8080").is_ok());
        assert!(validate_bind_address("localhost:3000").is_ok());
        assert!(validate_bind_address("192.168.1.100:65535").is_ok());
    }

    #[test]
    fn test_validate_bind_address_invalid_format() {
        // Missing port
        let result = validate_bind_address("127.0.0.1");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Invalid bind address format")
        );

        // Too many colons
        let result = validate_bind_address("127.0.0.1:9090:extra");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_bind_address_invalid_port() {
        // Non-numeric port
        let result = validate_bind_address("127.0.0.1:abc");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid port"));

        // Port 0
        let result = validate_bind_address("127.0.0.1:0");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Port 0 is not allowed")
        );

        // Port too large
        let result = validate_bind_address("127.0.0.1:70000");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_bind_address_empty_host() {
        let result = validate_bind_address(":9090");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Host cannot be empty")
        );
    }

    #[tokio::test]
    async fn test_metrics_json_command_validation() {
        let config = Config::default();
        let cmd = MetricsJsonCommand::new(config.clone());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_metrics_prometheus_command_validation() {
        let config = Config::default();
        let cmd = MetricsPrometheusCommand::new(config.clone());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_metrics_snapshot_command_validation() {
        let config = Config::default();
        let cmd = MetricsSnapshotCommand::new(config.clone(), false);
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_metrics_server_command_validation_valid() {
        let config = Config::default();
        let cmd = MetricsServerCommand::new(config.clone(), "127.0.0.1:9090".to_string());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_metrics_server_command_validation_invalid() {
        let config = Config::default();
        let cmd = MetricsServerCommand::new(config.clone(), "invalid".to_string());
        let ctx = CommandContext::new(config);

        let result = cmd.validate(&ctx).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_metrics_snapshot_command_pretty() {
        let config = Config::default();
        let cmd = MetricsSnapshotCommand::new(config.clone(), true);
        let mut ctx = CommandContext::new(config);

        // Should execute without errors
        let result = cmd.execute(&mut ctx).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert!(output.success);
        assert!(output.data.is_some());

        let data = output.data.unwrap();
        assert_eq!(data["pretty"], true);
    }

    #[tokio::test]
    async fn test_metrics_json_command_execution() {
        let config = Config::default();
        let cmd = MetricsJsonCommand::new(config.clone());
        let mut ctx = CommandContext::new(config);
        ctx.json_output = true; // Suppress stdout

        let result = cmd.execute(&mut ctx).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert!(output.success);
        assert!(output.data.is_some());

        let data = output.data.unwrap();
        assert_eq!(data["format"], "json");
    }

    #[tokio::test]
    async fn test_metrics_prometheus_command_execution() {
        let config = Config::default();
        let cmd = MetricsPrometheusCommand::new(config.clone());
        let mut ctx = CommandContext::new(config);
        ctx.json_output = true; // Suppress stdout

        let result = cmd.execute(&mut ctx).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert!(output.success);
        assert!(output.data.is_some());

        let data = output.data.unwrap();
        assert_eq!(data["format"], "prometheus");
        assert_eq!(
            data["content_type"],
            "text/plain; version=0.0.4; charset=utf-8"
        );
    }

    #[test]
    fn test_command_names() {
        let config = Config::default();

        let json_cmd = MetricsJsonCommand::new(config.clone());
        assert_eq!(json_cmd.name(), "metrics json");
        assert_eq!(json_cmd.description(), "Export metrics in JSON format");

        let prom_cmd = MetricsPrometheusCommand::new(config.clone());
        assert_eq!(prom_cmd.name(), "metrics prometheus");
        assert_eq!(
            prom_cmd.description(),
            "Export metrics in Prometheus format"
        );

        let snap_cmd = MetricsSnapshotCommand::new(config.clone(), false);
        assert_eq!(snap_cmd.name(), "metrics snapshot");
        assert_eq!(snap_cmd.description(), "Show detailed metrics snapshot");

        let server_cmd = MetricsServerCommand::new(config, "127.0.0.1:9090".to_string());
        assert_eq!(server_cmd.name(), "metrics server");
        assert_eq!(server_cmd.description(), "Start standalone metrics server");
    }
}