Skip to main content

mockforge_observability/
lib.rs

1//! MockForge Observability
2//!
3//! Provides comprehensive observability features including:
4//! - Structured logging with JSON support
5//! - Prometheus metrics export
6//! - OpenTelemetry distributed tracing
7//! - Request/response recording (flight recorder)
8//! - Scenario control and chaos engineering
9//! - System metrics collection (CPU, memory, threads)
10//!
11//! # Example
12//!
13//! ```rust
14//! use mockforge_observability::prometheus::MetricsRegistry;
15//!
16//! let registry = MetricsRegistry::new();
17//! registry.record_http_request("GET", 200, 0.045);
18//! ```
19
20#[cfg(feature = "log-shipper")]
21pub mod log_shipper;
22pub mod logging;
23pub mod prometheus;
24#[cfg(feature = "sentry")]
25pub mod sentry_init;
26pub mod system_metrics;
27pub mod tracing_integration;
28
29// Re-export commonly used items
30pub use logging::{init_logging, init_logging_with_otel, LoggingConfig};
31pub use prometheus::{
32    get_global_registry, get_global_registry_arc, DriftEvaluationSample, MetricsRegistry,
33};
34#[cfg(feature = "sentry")]
35pub use sentry_init::init_sentry;
36pub use system_metrics::{start_system_metrics_collector, SystemMetricsConfig};
37pub use tracing_integration::{init_with_otel, shutdown_otel, OtelTracingConfig};
38
39/// Protocol types for metrics tracking
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41pub enum Protocol {
42    Http,
43    Grpc,
44    WebSocket,
45    GraphQL,
46}
47
48impl Protocol {
49    pub fn as_str(&self) -> &'static str {
50        match self {
51            Protocol::Http => "http",
52            Protocol::Grpc => "grpc",
53            Protocol::WebSocket => "websocket",
54            Protocol::GraphQL => "graphql",
55        }
56    }
57}
58
59impl std::fmt::Display for Protocol {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "{}", self.as_str())
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use std::collections::HashSet;
69
70    #[test]
71    fn test_protocol_display() {
72        assert_eq!(Protocol::Http.to_string(), "http");
73        assert_eq!(Protocol::Grpc.to_string(), "grpc");
74        assert_eq!(Protocol::WebSocket.to_string(), "websocket");
75        assert_eq!(Protocol::GraphQL.to_string(), "graphql");
76    }
77
78    #[test]
79    fn test_protocol_as_str() {
80        assert_eq!(Protocol::Http.as_str(), "http");
81        assert_eq!(Protocol::Grpc.as_str(), "grpc");
82        assert_eq!(Protocol::WebSocket.as_str(), "websocket");
83        assert_eq!(Protocol::GraphQL.as_str(), "graphql");
84    }
85
86    #[test]
87    fn test_protocol_debug() {
88        assert_eq!(format!("{:?}", Protocol::Http), "Http");
89        assert_eq!(format!("{:?}", Protocol::Grpc), "Grpc");
90        assert_eq!(format!("{:?}", Protocol::WebSocket), "WebSocket");
91        assert_eq!(format!("{:?}", Protocol::GraphQL), "GraphQL");
92    }
93
94    #[test]
95    fn test_protocol_clone() {
96        let proto = Protocol::Http;
97        let cloned = Clone::clone(&proto);
98        assert_eq!(proto, cloned);
99    }
100
101    #[test]
102    fn test_protocol_copy() {
103        let proto = Protocol::Grpc;
104        let copied = proto;
105        assert_eq!(Protocol::Grpc, copied);
106        assert_eq!(proto, Protocol::Grpc); // proto still accessible
107    }
108
109    #[test]
110    fn test_protocol_eq() {
111        assert_eq!(Protocol::Http, Protocol::Http);
112        assert_eq!(Protocol::Grpc, Protocol::Grpc);
113        assert_ne!(Protocol::Http, Protocol::Grpc);
114        assert_ne!(Protocol::WebSocket, Protocol::GraphQL);
115    }
116
117    #[test]
118    fn test_protocol_hash() {
119        let mut set = HashSet::new();
120        set.insert(Protocol::Http);
121        set.insert(Protocol::Grpc);
122        set.insert(Protocol::WebSocket);
123        set.insert(Protocol::GraphQL);
124
125        assert_eq!(set.len(), 4);
126        assert!(set.contains(&Protocol::Http));
127        assert!(set.contains(&Protocol::Grpc));
128        assert!(set.contains(&Protocol::WebSocket));
129        assert!(set.contains(&Protocol::GraphQL));
130
131        // Duplicate insertion shouldn't increase size
132        set.insert(Protocol::Http);
133        assert_eq!(set.len(), 4);
134    }
135
136    #[test]
137    fn test_protocol_all_variants() {
138        let protocols = [
139            Protocol::Http,
140            Protocol::Grpc,
141            Protocol::WebSocket,
142            Protocol::GraphQL,
143        ];
144
145        for proto in protocols {
146            let str_repr = proto.as_str();
147            assert!(!str_repr.is_empty());
148            assert_eq!(proto.to_string(), str_repr);
149        }
150    }
151}