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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#![allow(dead_code, unused_imports, unused_variables)]
use crate::InfernoError;
use anyhow::Result;
use axum::{Router, extract::State, http::StatusCode, response::IntoResponse, routing::get};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, RwLock};
use tracing::{info, instrument};

/// Observability configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObservabilityConfig {
    /// Enable Prometheus metrics export
    pub prometheus_enabled: bool,
    /// Prometheus metrics endpoint
    pub prometheus_endpoint: String,
    /// Prometheus scrape interval in seconds
    pub prometheus_scrape_interval: u64,

    /// Enable OpenTelemetry tracing
    pub otel_enabled: bool,
    /// OpenTelemetry collector endpoint
    pub otel_endpoint: String,
    /// OpenTelemetry service name
    pub otel_service_name: String,
    /// OpenTelemetry sampling ratio (0.0 to 1.0)
    pub otel_sampling_ratio: f64,

    /// Enable Grafana integration
    pub grafana_enabled: bool,
    /// Grafana API endpoint
    pub grafana_endpoint: String,
    /// Grafana API key
    pub grafana_api_key: Option<String>,

    /// Custom metrics configuration
    pub custom_metrics_enabled: bool,
    /// Metrics retention period in hours
    pub metrics_retention_hours: u64,
    /// Enable histogram metrics
    pub histogram_enabled: bool,
    /// Histogram bucket configuration
    pub histogram_buckets: Vec<f64>,
}

impl Default for ObservabilityConfig {
    fn default() -> Self {
        Self {
            prometheus_enabled: true,
            prometheus_endpoint: "/metrics".to_string(),
            prometheus_scrape_interval: 15,

            otel_enabled: false,
            otel_endpoint: "http://localhost:4317".to_string(),
            otel_service_name: "inferno".to_string(),
            otel_sampling_ratio: 1.0,

            grafana_enabled: false,
            grafana_endpoint: "http://localhost:3000".to_string(),
            grafana_api_key: None,

            custom_metrics_enabled: true,
            metrics_retention_hours: 24,
            histogram_enabled: true,
            histogram_buckets: vec![
                0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
            ],
        }
    }
}

/// Metric types for Prometheus
#[derive(Debug, Clone, Copy)]
pub enum MetricType {
    Counter,
    Gauge,
    Histogram,
    Summary,
}

/// Individual metric data
#[derive(Debug, Clone)]
pub struct Metric {
    pub name: String,
    pub help: String,
    pub metric_type: MetricType,
    pub value: MetricValue,
    pub labels: HashMap<String, String>,
    pub timestamp: DateTime<Utc>,
}

/// Metric value types
#[derive(Debug, Clone)]
pub enum MetricValue {
    Counter(u64),
    Gauge(f64),
    Histogram(Vec<f64>),
    Summary {
        sum: f64,
        count: u64,
        quantiles: Vec<(f64, f64)>,
    },
}

/// Prometheus metrics collector
pub struct PrometheusCollector {
    metrics: Arc<RwLock<HashMap<String, Metric>>>,
    config: ObservabilityConfig,
}

impl PrometheusCollector {
    pub fn new(config: ObservabilityConfig) -> Self {
        Self {
            metrics: Arc::new(RwLock::new(HashMap::new())),
            config,
        }
    }

    /// Register a new metric
    pub async fn register_metric(
        &self,
        name: String,
        help: String,
        metric_type: MetricType,
        labels: HashMap<String, String>,
    ) -> Result<()> {
        let metric = Metric {
            name: name.clone(),
            help,
            metric_type,
            value: match metric_type {
                MetricType::Counter => MetricValue::Counter(0),
                MetricType::Gauge => MetricValue::Gauge(0.0),
                MetricType::Histogram => MetricValue::Histogram(vec![]),
                MetricType::Summary => MetricValue::Summary {
                    sum: 0.0,
                    count: 0,
                    quantiles: vec![(0.5, 0.0), (0.9, 0.0), (0.99, 0.0)],
                },
            },
            labels,
            timestamp: Utc::now(),
        };

        let mut metrics = self.metrics.write().await;
        metrics.insert(name, metric);
        Ok(())
    }

    /// Increment a counter metric
    pub async fn increment_counter(&self, name: &str, increment: u64) -> Result<()> {
        let mut metrics = self.metrics.write().await;
        if let Some(metric) = metrics.get_mut(name) {
            if let MetricValue::Counter(ref mut value) = metric.value {
                *value += increment;
                metric.timestamp = Utc::now();
            }
        }
        Ok(())
    }

    /// Set a gauge metric
    pub async fn set_gauge(&self, name: &str, value: f64) -> Result<()> {
        let mut metrics = self.metrics.write().await;
        if let Some(metric) = metrics.get_mut(name) {
            if let MetricValue::Gauge(ref mut v) = metric.value {
                *v = value;
                metric.timestamp = Utc::now();
            }
        }
        Ok(())
    }

    /// Observe a value for histogram
    pub async fn observe_histogram(&self, name: &str, value: f64) -> Result<()> {
        let mut metrics = self.metrics.write().await;
        if let Some(metric) = metrics.get_mut(name) {
            if let MetricValue::Histogram(ref mut values) = metric.value {
                values.push(value);
                metric.timestamp = Utc::now();

                // Keep only recent values to prevent unbounded growth
                if values.len() > 10000 {
                    values.drain(0..1000);
                }
            }
        }
        Ok(())
    }

    /// Export metrics in Prometheus format
    pub async fn export_prometheus_format(&self) -> String {
        let metrics = self.metrics.read().await;
        let mut output = String::new();

        for metric in metrics.values() {
            // Write help text
            output.push_str(&format!("# HELP {} {}\n", metric.name, metric.help));

            // Write type
            let type_str = match metric.metric_type {
                MetricType::Counter => "counter",
                MetricType::Gauge => "gauge",
                MetricType::Histogram => "histogram",
                MetricType::Summary => "summary",
            };
            output.push_str(&format!("# TYPE {} {}\n", metric.name, type_str));

            // Write metric value with labels
            let labels_str = if metric.labels.is_empty() {
                String::new()
            } else {
                let labels: Vec<String> = metric
                    .labels
                    .iter()
                    .map(|(k, v)| format!("{}=\"{}\"", k, v))
                    .collect();
                format!("{{{}}}", labels.join(","))
            };

            match &metric.value {
                MetricValue::Counter(value) => {
                    output.push_str(&format!("{}{} {}\n", metric.name, labels_str, value));
                }
                MetricValue::Gauge(value) => {
                    output.push_str(&format!("{}{} {}\n", metric.name, labels_str, value));
                }
                MetricValue::Histogram(values) => {
                    if !values.is_empty() {
                        // Calculate histogram buckets
                        for bucket in &self.config.histogram_buckets {
                            let count = values.iter().filter(|&&v| v <= *bucket).count();
                            output.push_str(&format!(
                                "{}_bucket{{le=\"{}\"{}}} {}\n",
                                metric.name,
                                bucket,
                                if labels_str.is_empty() {
                                    String::new()
                                } else {
                                    format!(",{}", &labels_str[1..labels_str.len() - 1])
                                },
                                count
                            ));
                        }
                        output.push_str(&format!(
                            "{}_bucket{{le=\"+Inf\"{}}} {}\n",
                            metric.name,
                            if labels_str.is_empty() {
                                String::new()
                            } else {
                                format!(",{}", &labels_str[1..labels_str.len() - 1])
                            },
                            values.len()
                        ));

                        let sum: f64 = values.iter().sum();
                        output.push_str(&format!("{}_sum{} {}\n", metric.name, labels_str, sum));
                        output.push_str(&format!(
                            "{}_count{} {}\n",
                            metric.name,
                            labels_str,
                            values.len()
                        ));
                    }
                }
                MetricValue::Summary {
                    sum,
                    count,
                    quantiles,
                } => {
                    for (quantile, value) in quantiles {
                        output.push_str(&format!(
                            "{}{{quantile=\"{}\"{}}} {}\n",
                            metric.name,
                            quantile,
                            if labels_str.is_empty() {
                                String::new()
                            } else {
                                format!(",{}", &labels_str[1..labels_str.len() - 1])
                            },
                            value
                        ));
                    }
                    output.push_str(&format!("{}_sum{} {}\n", metric.name, labels_str, sum));
                    output.push_str(&format!("{}_count{} {}\n", metric.name, labels_str, count));
                }
            }
        }

        output
    }
}

/// OpenTelemetry trace span
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceSpan {
    pub trace_id: String,
    pub span_id: String,
    pub parent_span_id: Option<String>,
    pub operation_name: String,
    pub start_time: DateTime<Utc>,
    pub end_time: Option<DateTime<Utc>>,
    pub duration_ms: Option<f64>,
    pub status: SpanStatus,
    pub attributes: HashMap<String, String>,
    pub events: Vec<SpanEvent>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SpanStatus {
    Unset,
    Ok,
    Error(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpanEvent {
    pub name: String,
    pub timestamp: DateTime<Utc>,
    pub attributes: HashMap<String, String>,
}

/// OpenTelemetry tracer
pub struct OpenTelemetryTracer {
    spans: Arc<Mutex<Vec<TraceSpan>>>,
    config: ObservabilityConfig,
}

impl OpenTelemetryTracer {
    pub fn new(config: ObservabilityConfig) -> Self {
        Self {
            spans: Arc::new(Mutex::new(Vec::new())),
            config,
        }
    }

    /// Start a new trace span
    pub async fn start_span(&self, operation_name: String) -> String {
        let span = TraceSpan {
            trace_id: uuid::Uuid::new_v4().to_string(),
            span_id: uuid::Uuid::new_v4().to_string(),
            parent_span_id: None,
            operation_name,
            start_time: Utc::now(),
            end_time: None,
            duration_ms: None,
            status: SpanStatus::Unset,
            attributes: HashMap::new(),
            events: Vec::new(),
        };

        let span_id = span.span_id.clone();
        let mut spans = self.spans.lock().await;
        spans.push(span);

        // Keep only recent spans
        if spans.len() > 1000 {
            spans.drain(0..100);
        }

        span_id
    }

    /// End a trace span
    pub async fn end_span(&self, span_id: &str, status: SpanStatus) -> Result<()> {
        let mut spans = self.spans.lock().await;
        if let Some(span) = spans.iter_mut().find(|s| s.span_id == span_id) {
            span.end_time = Some(Utc::now());
            span.duration_ms = Some(
                (span.end_time.expect("End time should be set just above") - span.start_time)
                    .num_milliseconds() as f64,
            );
            span.status = status;
        }
        Ok(())
    }

    /// Add attributes to a span
    pub async fn add_span_attributes(
        &self,
        span_id: &str,
        attributes: HashMap<String, String>,
    ) -> Result<()> {
        let mut spans = self.spans.lock().await;
        if let Some(span) = spans.iter_mut().find(|s| s.span_id == span_id) {
            span.attributes.extend(attributes);
        }
        Ok(())
    }

    /// Add event to a span
    pub async fn add_span_event(
        &self,
        span_id: &str,
        event_name: String,
        attributes: HashMap<String, String>,
    ) -> Result<()> {
        let mut spans = self.spans.lock().await;
        if let Some(span) = spans.iter_mut().find(|s| s.span_id == span_id) {
            span.events.push(SpanEvent {
                name: event_name,
                timestamp: Utc::now(),
                attributes,
            });
        }
        Ok(())
    }

    /// Export spans in OTLP format (simplified)
    pub async fn export_otlp_format(&self) -> Vec<TraceSpan> {
        let spans = self.spans.lock().await;
        spans.clone()
    }
}

/// Grafana dashboard configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrafanaDashboard {
    pub id: String,
    pub title: String,
    pub panels: Vec<DashboardPanel>,
    pub refresh_interval: String,
    pub time_range: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DashboardPanel {
    pub id: u32,
    pub title: String,
    pub panel_type: String, // graph, stat, gauge, table
    pub datasource: String,
    pub query: String,
    pub grid_pos: GridPosition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GridPosition {
    pub x: u32,
    pub y: u32,
    pub w: u32,
    pub h: u32,
}

/// Observability manager combining all observability features
pub struct ObservabilityManager {
    config: ObservabilityConfig,
    prometheus: Arc<PrometheusCollector>,
    tracer: Arc<OpenTelemetryTracer>,
    dashboards: Arc<RwLock<Vec<GrafanaDashboard>>>,
}

impl ObservabilityManager {
    pub fn new(config: ObservabilityConfig) -> Self {
        let prometheus = Arc::new(PrometheusCollector::new(config.clone()));
        let tracer = Arc::new(OpenTelemetryTracer::new(config.clone()));

        Self {
            config,
            prometheus,
            tracer,
            dashboards: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Initialize default metrics
    pub async fn initialize(&self) -> Result<()> {
        info!("Initializing observability stack");

        // Register default Prometheus metrics
        if self.config.prometheus_enabled {
            // System metrics
            self.prometheus
                .register_metric(
                    "inferno_up".to_string(),
                    "Whether the Inferno service is up".to_string(),
                    MetricType::Gauge,
                    HashMap::new(),
                )
                .await?;

            // Inference metrics
            self.prometheus
                .register_metric(
                    "inferno_inference_requests_total".to_string(),
                    "Total number of inference requests".to_string(),
                    MetricType::Counter,
                    HashMap::from([("model".to_string(), "all".to_string())]),
                )
                .await?;

            self.prometheus
                .register_metric(
                    "inferno_inference_duration_seconds".to_string(),
                    "Inference request duration in seconds".to_string(),
                    MetricType::Histogram,
                    HashMap::new(),
                )
                .await?;

            // Model metrics
            self.prometheus
                .register_metric(
                    "inferno_models_loaded".to_string(),
                    "Number of models currently loaded".to_string(),
                    MetricType::Gauge,
                    HashMap::new(),
                )
                .await?;

            self.prometheus
                .register_metric(
                    "inferno_model_memory_bytes".to_string(),
                    "Memory usage per model in bytes".to_string(),
                    MetricType::Gauge,
                    HashMap::new(),
                )
                .await?;

            // Error metrics
            self.prometheus
                .register_metric(
                    "inferno_errors_total".to_string(),
                    "Total number of errors".to_string(),
                    MetricType::Counter,
                    HashMap::from([("type".to_string(), "all".to_string())]),
                )
                .await?;

            // Set service up
            self.prometheus.set_gauge("inferno_up", 1.0).await?;
        }

        // Create default Grafana dashboard
        if self.config.grafana_enabled {
            self.create_default_dashboard().await?;
        }

        Ok(())
    }

    /// Create default Grafana dashboard
    async fn create_default_dashboard(&self) -> Result<()> {
        let dashboard = GrafanaDashboard {
            id: "inferno-default".to_string(),
            title: "Inferno AI/ML Model Runner".to_string(),
            refresh_interval: "5s".to_string(),
            time_range: "now-1h".to_string(),
            panels: vec![
                DashboardPanel {
                    id: 1,
                    title: "Service Status".to_string(),
                    panel_type: "stat".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "inferno_up".to_string(),
                    grid_pos: GridPosition { x: 0, y: 0, w: 6, h: 4 },
                },
                DashboardPanel {
                    id: 2,
                    title: "Request Rate".to_string(),
                    panel_type: "graph".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "rate(inferno_inference_requests_total[5m])".to_string(),
                    grid_pos: GridPosition { x: 6, y: 0, w: 12, h: 8 },
                },
                DashboardPanel {
                    id: 3,
                    title: "Response Time".to_string(),
                    panel_type: "graph".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "histogram_quantile(0.95, rate(inferno_inference_duration_seconds_bucket[5m]))".to_string(),
                    grid_pos: GridPosition { x: 18, y: 0, w: 6, h: 8 },
                },
                DashboardPanel {
                    id: 4,
                    title: "Models Loaded".to_string(),
                    panel_type: "gauge".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "inferno_models_loaded".to_string(),
                    grid_pos: GridPosition { x: 0, y: 4, w: 6, h: 4 },
                },
                DashboardPanel {
                    id: 5,
                    title: "Error Rate".to_string(),
                    panel_type: "graph".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "rate(inferno_errors_total[5m])".to_string(),
                    grid_pos: GridPosition { x: 6, y: 8, w: 12, h: 8 },
                },
                DashboardPanel {
                    id: 6,
                    title: "Memory Usage".to_string(),
                    panel_type: "graph".to_string(),
                    datasource: "Prometheus".to_string(),
                    query: "sum(inferno_model_memory_bytes) / 1024 / 1024 / 1024".to_string(),
                    grid_pos: GridPosition { x: 18, y: 8, w: 6, h: 8 },
                },
            ],
        };

        let mut dashboards = self.dashboards.write().await;
        dashboards.push(dashboard);

        Ok(())
    }

    /// Record an inference request
    #[instrument(skip(self))]
    pub async fn record_inference(
        &self,
        model: &str,
        duration: Duration,
        success: bool,
    ) -> Result<()> {
        if self.config.prometheus_enabled {
            // Increment request counter
            self.prometheus
                .increment_counter("inferno_inference_requests_total", 1)
                .await?;

            // Record duration histogram
            self.prometheus
                .observe_histogram("inferno_inference_duration_seconds", duration.as_secs_f64())
                .await?;

            // Record error if failed
            if !success {
                self.prometheus
                    .increment_counter("inferno_errors_total", 1)
                    .await?;
            }
        }

        if self.config.otel_enabled {
            // Create trace span
            let span_id = self.tracer.start_span(format!("inference.{}", model)).await;

            // Add attributes
            let mut attributes = HashMap::new();
            attributes.insert("model".to_string(), model.to_string());
            attributes.insert("duration_ms".to_string(), duration.as_millis().to_string());
            attributes.insert("success".to_string(), success.to_string());

            self.tracer
                .add_span_attributes(&span_id, attributes)
                .await?;

            // End span
            let status = if success {
                SpanStatus::Ok
            } else {
                SpanStatus::Error("Inference failed".to_string())
            };
            self.tracer.end_span(&span_id, status).await?;
        }

        Ok(())
    }

    /// Get Prometheus metrics
    pub async fn get_prometheus_metrics(&self) -> String {
        self.prometheus.export_prometheus_format().await
    }

    /// Get OpenTelemetry traces
    pub async fn get_traces(&self) -> Vec<TraceSpan> {
        self.tracer.export_otlp_format().await
    }

    /// Get Grafana dashboards
    pub async fn get_dashboards(&self) -> Vec<GrafanaDashboard> {
        let dashboards = self.dashboards.read().await;
        dashboards.clone()
    }

    /// Export dashboard as JSON
    pub async fn export_dashboard_json(&self, dashboard_id: &str) -> Result<String> {
        let dashboards = self.dashboards.read().await;

        if let Some(dashboard) = dashboards.iter().find(|d| d.id == dashboard_id) {
            Ok(serde_json::to_string_pretty(dashboard)?)
        } else {
            Err(InfernoError::ModelNotFound(format!("Dashboard {} not found", dashboard_id)).into())
        }
    }
}

/// Create observability routes for Axum server
pub fn create_observability_routes(manager: Arc<ObservabilityManager>) -> Router {
    Router::new()
        .route("/metrics", get(prometheus_metrics_handler))
        .route("/traces", get(traces_handler))
        .route("/dashboards", get(dashboards_handler))
        .route("/dashboards/:id", get(dashboard_handler))
        .with_state(manager)
}

/// Prometheus metrics endpoint handler
async fn prometheus_metrics_handler(
    State(manager): State<Arc<ObservabilityManager>>,
) -> impl IntoResponse {
    let metrics = manager.get_prometheus_metrics().await;
    (StatusCode::OK, metrics)
}

/// OpenTelemetry traces endpoint handler
async fn traces_handler(State(manager): State<Arc<ObservabilityManager>>) -> impl IntoResponse {
    let traces = manager.get_traces().await;
    (
        StatusCode::OK,
        serde_json::to_string(&traces).unwrap_or_default(),
    )
}

/// Grafana dashboards list handler
async fn dashboards_handler(State(manager): State<Arc<ObservabilityManager>>) -> impl IntoResponse {
    let dashboards = manager.get_dashboards().await;
    (
        StatusCode::OK,
        serde_json::to_string(&dashboards).unwrap_or_default(),
    )
}

/// Individual dashboard handler
async fn dashboard_handler(
    State(manager): State<Arc<ObservabilityManager>>,
    axum::extract::Path(dashboard_id): axum::extract::Path<String>,
) -> impl IntoResponse {
    match manager.export_dashboard_json(&dashboard_id).await {
        Ok(json) => (StatusCode::OK, json),
        Err(_) => (StatusCode::NOT_FOUND, "Dashboard not found".to_string()),
    }
}

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

    #[tokio::test]
    async fn test_prometheus_collector() {
        let config = ObservabilityConfig::default();
        let collector = PrometheusCollector::new(config);

        // Register metrics
        collector
            .register_metric(
                "test_counter".to_string(),
                "Test counter metric".to_string(),
                MetricType::Counter,
                HashMap::new(),
            )
            .await
            .expect("Failed to register metric in test");

        collector
            .register_metric(
                "test_gauge".to_string(),
                "Test gauge metric".to_string(),
                MetricType::Gauge,
                HashMap::new(),
            )
            .await
            .expect("Failed to register metric in test");

        // Update metrics
        collector
            .increment_counter("test_counter", 5)
            .await
            .expect("Failed to increment counter in test");
        collector
            .set_gauge("test_gauge", 42.5)
            .await
            .expect("Failed to set gauge in test");

        // Export
        let output = collector.export_prometheus_format().await;
        assert!(output.contains("test_counter 5"));
        assert!(output.contains("test_gauge 42.5"));
    }

    #[tokio::test]
    async fn test_opentelemetry_tracer() {
        let config = ObservabilityConfig::default();
        let tracer = OpenTelemetryTracer::new(config);

        // Start span
        let span_id = tracer.start_span("test_operation".to_string()).await;

        // Add attributes
        let mut attributes = HashMap::new();
        attributes.insert("test_key".to_string(), "test_value".to_string());
        tracer
            .add_span_attributes(&span_id, attributes)
            .await
            .expect("Failed to add span attributes in test");

        // Add event
        tracer
            .add_span_event(&span_id, "test_event".to_string(), HashMap::new())
            .await
            .expect("Failed to register metric in test");

        // End span
        tracer
            .end_span(&span_id, SpanStatus::Ok)
            .await
            .expect("Failed to end span in test");

        // Export
        let spans = tracer.export_otlp_format().await;
        assert_eq!(spans.len(), 1);
        assert_eq!(spans[0].operation_name, "test_operation");
    }

    #[tokio::test]
    async fn test_observability_manager() {
        let config = ObservabilityConfig::default();
        let manager = ObservabilityManager::new(config);

        // Initialize
        manager
            .initialize()
            .await
            .expect("Failed to initialize observability manager in test");

        // Record inference
        manager
            .record_inference("test_model", Duration::from_millis(100), true)
            .await
            .expect("Failed to register metric in test");

        // Get metrics
        let metrics = manager.get_prometheus_metrics().await;
        assert!(metrics.contains("inferno_up 1"));
        assert!(metrics.contains("inferno_inference_requests_total"));
    }
}