mod context;
mod event;
mod macros;
mod query;
#[cfg(feature = "trace")]
mod ring_buffer;
#[cfg(feature = "trace")]
mod app_protocol;
#[cfg(feature = "trace")]
mod implementation {
use std::sync::Arc;
pub use super::context::TraceContext;
pub use super::event::{Event, EventData, TraceId, socket_addr_to_bytes};
pub use super::query::{ConnectionAnalysis, TraceQuery};
pub use super::ring_buffer::{EventLog, TraceConfig};
#[cfg(feature = "trace")]
pub use super::app_protocol::{
AppProtocol, AppRegistry as AppProtocolRegistry, DataMapProtocol,
};
static EVENT_LOG: once_cell::sync::Lazy<Arc<EventLog>> =
once_cell::sync::Lazy::new(|| Arc::new(EventLog::new()));
pub fn global_log() -> Arc<EventLog> {
EVENT_LOG.clone()
}
#[cfg(feature = "trace")]
static APP_REGISTRY: once_cell::sync::Lazy<AppProtocolRegistry> =
once_cell::sync::Lazy::new(AppProtocolRegistry::new);
#[cfg(feature = "trace")]
pub fn global_app_registry() -> &'static AppProtocolRegistry {
&APP_REGISTRY
}
}
#[cfg(not(feature = "trace"))]
mod implementation {
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TraceId;
impl Default for TraceId {
fn default() -> Self {
Self::new()
}
}
impl TraceId {
pub fn new() -> Self {
Self
}
}
#[derive(Debug)]
pub struct Event;
#[derive(Debug, Default, Clone)]
pub struct EventLog;
impl EventLog {
pub fn new() -> Self {
Self
}
pub fn log(&self, _event: Event) {}
pub fn recent_events(&self, _count: usize) -> Vec<Event> {
Vec::new()
}
pub fn get_events_by_trace(&self, _trace_id: TraceId) -> Vec<Event> {
Vec::new()
}
}
#[derive(Debug, Clone)]
pub struct TraceContext;
impl TraceContext {
pub fn new(_trace_id: TraceId) -> Self {
Self
}
pub fn trace_id(&self) -> TraceId {
TraceId
}
}
#[derive(Debug, Clone, Copy)]
pub struct TraceFlags;
impl Default for TraceFlags {
fn default() -> Self {
Self
}
}
pub fn global_log() -> Arc<EventLog> {
Arc::new(EventLog)
}
}
pub use implementation::*;
#[cfg(feature = "trace")]
#[allow(clippy::panic)]
pub fn timestamp_now() -> u64 {
use std::time::{SystemTime, UNIX_EPOCH};
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_else(|_| panic!("current time should always be after UNIX epoch"))
.as_micros() as u64
}
#[cfg(not(feature = "trace"))]
pub fn timestamp_now() -> u64 {
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zero_sized_types() {
#[cfg(not(feature = "trace"))]
{
assert_eq!(std::mem::size_of::<TraceId>(), 0);
assert_eq!(std::mem::size_of::<Event>(), 0);
assert_eq!(std::mem::size_of::<EventLog>(), 0);
assert_eq!(std::mem::size_of::<TraceContext>(), 0);
assert_eq!(std::mem::size_of::<TraceFlags>(), 0);
}
}
#[test]
fn test_no_op_operations() {
let log = EventLog::new();
#[cfg(not(feature = "trace"))]
log.log(Event); #[cfg(feature = "trace")]
{
let event = Event::default();
log.log(event);
}
let trace_id = TraceId::new();
let _context = TraceContext::new(trace_id);
}
}