1mod context;
15mod event;
16mod macros;
17mod query;
18#[cfg(feature = "trace")]
19mod ring_buffer;
20
21#[cfg(feature = "trace")]
22mod app_protocol;
23
24#[cfg(feature = "trace")]
25mod implementation {
26 use std::sync::Arc;
27
28 pub use super::context::TraceContext;
30 pub use super::event::{Event, EventData, TraceId, socket_addr_to_bytes};
31 pub use super::query::{ConnectionAnalysis, TraceQuery};
32 pub use super::ring_buffer::{EventLog, TraceConfig};
33
34 #[cfg(feature = "trace")]
35 pub use super::app_protocol::{
36 AppProtocol, AppRegistry as AppProtocolRegistry, DataMapProtocol,
37 };
38
39 static EVENT_LOG: once_cell::sync::Lazy<Arc<EventLog>> =
41 once_cell::sync::Lazy::new(|| Arc::new(EventLog::new()));
42
43 pub fn global_log() -> Arc<EventLog> {
45 EVENT_LOG.clone()
46 }
47
48 #[cfg(feature = "trace")]
49 static APP_REGISTRY: once_cell::sync::Lazy<AppProtocolRegistry> =
50 once_cell::sync::Lazy::new(AppProtocolRegistry::new);
51
52 #[cfg(feature = "trace")]
53 pub fn global_app_registry() -> &'static AppProtocolRegistry {
55 &APP_REGISTRY
56 }
57}
58
59#[cfg(not(feature = "trace"))]
61mod implementation {
62 use std::sync::Arc;
63
64 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
66 pub struct TraceId;
67
68 impl Default for TraceId {
69 fn default() -> Self {
70 Self::new()
71 }
72 }
73
74 impl TraceId {
75 pub fn new() -> Self {
77 Self
78 }
79 }
80
81 #[derive(Debug)]
83 pub struct Event;
84
85 #[derive(Debug, Default, Clone)]
87 pub struct EventLog;
88
89 impl EventLog {
90 pub fn new() -> Self {
92 Self
93 }
94 pub fn log(&self, _event: Event) {}
96 pub fn recent_events(&self, _count: usize) -> Vec<Event> {
98 Vec::new()
99 }
100 pub fn get_events_by_trace(&self, _trace_id: TraceId) -> Vec<Event> {
102 Vec::new()
103 }
104 }
105
106 #[derive(Debug, Clone)]
108 pub struct TraceContext;
109
110 impl TraceContext {
111 pub fn new(_trace_id: TraceId) -> Self {
113 Self
114 }
115 pub fn trace_id(&self) -> TraceId {
117 TraceId
118 }
119 }
120
121 #[derive(Debug, Clone, Copy)]
123 pub struct TraceFlags;
124
125 impl Default for TraceFlags {
126 fn default() -> Self {
127 Self
128 }
129 }
130
131 pub fn global_log() -> Arc<EventLog> {
133 Arc::new(EventLog)
134 }
135}
136
137pub use implementation::*;
139
140#[cfg(feature = "trace")]
142#[allow(clippy::panic)]
144pub fn timestamp_now() -> u64 {
145 use std::time::{SystemTime, UNIX_EPOCH};
146 SystemTime::now()
147 .duration_since(UNIX_EPOCH)
148 .unwrap_or_else(|_| panic!("current time should always be after UNIX epoch"))
149 .as_micros() as u64
150}
151
152#[cfg(not(feature = "trace"))]
153pub fn timestamp_now() -> u64 {
155 0
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn test_zero_sized_types() {
164 #[cfg(not(feature = "trace"))]
166 {
167 assert_eq!(std::mem::size_of::<TraceId>(), 0);
168 assert_eq!(std::mem::size_of::<Event>(), 0);
169 assert_eq!(std::mem::size_of::<EventLog>(), 0);
170 assert_eq!(std::mem::size_of::<TraceContext>(), 0);
171 assert_eq!(std::mem::size_of::<TraceFlags>(), 0);
172 }
173 }
174
175 #[test]
176 fn test_no_op_operations() {
177 let log = EventLog::new();
178 #[cfg(not(feature = "trace"))]
179 log.log(Event); #[cfg(feature = "trace")]
181 {
182 let event = Event::default();
184 log.log(event);
185 }
186
187 let trace_id = TraceId::new();
188 let _context = TraceContext::new(trace_id);
189 }
190}