Skip to main content

mojentic/tracer/
null_tracer.rs

1//! Null tracer implementation following the Null Object Pattern
2//!
3//! This module provides a NullTracer that implements the same interface as TracerSystem
4//! but performs no operations. This eliminates the need for conditional checks in client code.
5
6use super::tracer_events::TracerEvent;
7use std::collections::HashMap;
8
9/// A no-op implementation of TracerSystem that silently discards all tracing operations
10///
11/// This class follows the Null Object Pattern to eliminate conditional checks in client code.
12/// All record methods are overridden to do nothing, and all query methods return empty results.
13pub struct NullTracer;
14
15impl NullTracer {
16    /// Create a new null tracer
17    pub fn new() -> Self {
18        Self
19    }
20
21    /// Always returns false for null tracer
22    pub fn is_enabled(&self) -> bool {
23        false
24    }
25
26    /// No-op method for interface compatibility
27    pub fn enable(&self) {
28        // Do nothing
29    }
30
31    /// No-op method for interface compatibility
32    pub fn disable(&self) {
33        // Do nothing
34    }
35
36    /// Do nothing implementation of record_event
37    pub fn record_event(&self, _event: Box<dyn TracerEvent>) {
38        // Do nothing
39    }
40
41    /// Do nothing implementation of record_llm_call
42    #[allow(clippy::too_many_arguments)]
43    pub fn record_llm_call(
44        &self,
45        _model: impl Into<String>,
46        _messages: Vec<HashMap<String, serde_json::Value>>,
47        _temperature: Option<f64>,
48        _tools: Option<Vec<HashMap<String, serde_json::Value>>>,
49        _source: impl Into<String>,
50        _correlation_id: impl Into<String>,
51    ) {
52        // Do nothing
53    }
54
55    /// Do nothing implementation of record_llm_response
56    pub fn record_llm_response(
57        &self,
58        _model: impl Into<String>,
59        _content: impl Into<String>,
60        _tool_calls: Option<Vec<HashMap<String, serde_json::Value>>>,
61        _call_duration_ms: Option<f64>,
62        _source: impl Into<String>,
63        _correlation_id: impl Into<String>,
64    ) {
65        // Do nothing
66    }
67
68    /// Do nothing implementation of record_llm_response_with_evidence
69    #[allow(clippy::too_many_arguments)]
70    pub fn record_llm_response_with_evidence(
71        &self,
72        _model: impl Into<String>,
73        _content: impl Into<String>,
74        _tool_calls: Option<Vec<HashMap<String, serde_json::Value>>>,
75        _call_duration_ms: Option<f64>,
76        _evidence: crate::llm::models::ResponseEvidence,
77        _source: impl Into<String>,
78        _correlation_id: impl Into<String>,
79    ) {
80        // Do nothing
81    }
82
83    /// Do nothing implementation of record_tool_call
84    #[allow(clippy::too_many_arguments)]
85    pub fn record_tool_call(
86        &self,
87        _tool_name: impl Into<String>,
88        _arguments: HashMap<String, serde_json::Value>,
89        _result: serde_json::Value,
90        _caller: Option<String>,
91        _call_duration_ms: Option<f64>,
92        _source: impl Into<String>,
93        _correlation_id: impl Into<String>,
94    ) {
95        // Do nothing
96    }
97
98    /// Do nothing implementation of record_agent_interaction
99    pub fn record_agent_interaction(
100        &self,
101        _from_agent: impl Into<String>,
102        _to_agent: impl Into<String>,
103        _event_type: impl Into<String>,
104        _event_id: Option<String>,
105        _source: impl Into<String>,
106        _correlation_id: impl Into<String>,
107    ) {
108        // Do nothing
109    }
110
111    /// Return an empty vector for any get_event_summaries request
112    pub fn get_event_summaries(
113        &self,
114        _start_time: Option<f64>,
115        _end_time: Option<f64>,
116        _filter_func: Option<&dyn super::EventFilterFn>,
117    ) -> Vec<String> {
118        Vec::new()
119    }
120
121    /// Return an empty vector for any get_last_n_summaries request
122    pub fn get_last_n_summaries(
123        &self,
124        _n: usize,
125        _filter_func: Option<&dyn super::EventFilterFn>,
126    ) -> Vec<String> {
127        Vec::new()
128    }
129
130    /// Return 0 for any count_events request
131    pub fn count_events(
132        &self,
133        _start_time: Option<f64>,
134        _end_time: Option<f64>,
135        _filter_func: Option<&dyn super::EventFilterFn>,
136    ) -> usize {
137        0
138    }
139
140    /// Do nothing implementation of clear method
141    pub fn clear(&self) {
142        // Do nothing
143    }
144
145    /// Always returns 0 for null tracer
146    pub fn len(&self) -> usize {
147        0
148    }
149
150    /// Always returns true for null tracer
151    pub fn is_empty(&self) -> bool {
152        true
153    }
154}
155
156impl Default for NullTracer {
157    fn default() -> Self {
158        Self::new()
159    }
160}
161
162#[cfg(test)]
163mod tests {
164    use super::*;
165
166    #[test]
167    fn test_null_tracer_is_disabled() {
168        let tracer = NullTracer::new();
169        assert!(!tracer.is_enabled());
170    }
171
172    #[test]
173    fn test_null_tracer_enable_disable() {
174        let tracer = NullTracer::new();
175        tracer.enable();
176        assert!(!tracer.is_enabled());
177
178        tracer.disable();
179        assert!(!tracer.is_enabled());
180    }
181
182    #[test]
183    fn test_null_tracer_record_methods() {
184        let tracer = NullTracer::new();
185
186        // All record methods should be no-ops
187        tracer.record_llm_call("llama3.2", vec![], Some(0.7), None, "test", "corr-123");
188
189        tracer.record_llm_response(
190            "llama3.2",
191            "Hello, world!",
192            None,
193            Some(150.5),
194            "test",
195            "corr-456",
196        );
197
198        let mut args = HashMap::new();
199        args.insert("input".to_string(), serde_json::json!("test"));
200
201        tracer.record_tool_call(
202            "example_tool",
203            args,
204            serde_json::json!({"output": "result"}),
205            Some("agent1".to_string()),
206            Some(25.0),
207            "test",
208            "corr-789",
209        );
210
211        tracer.record_agent_interaction(
212            "agent1",
213            "agent2",
214            "message",
215            Some("evt-123".to_string()),
216            "test",
217            "corr-abc",
218        );
219
220        // Should have no events
221        assert_eq!(tracer.len(), 0);
222        assert!(tracer.is_empty());
223    }
224
225    #[test]
226    fn test_null_tracer_query_methods() {
227        let tracer = NullTracer::new();
228
229        // All query methods should return empty results
230        let summaries = tracer.get_event_summaries(None, None, None);
231        assert!(summaries.is_empty());
232
233        let last_summaries = tracer.get_last_n_summaries(10, None);
234        assert!(last_summaries.is_empty());
235
236        let count = tracer.count_events(None, None, None);
237        assert_eq!(count, 0);
238    }
239
240    #[test]
241    fn test_null_tracer_clear() {
242        let tracer = NullTracer::new();
243        tracer.clear();
244        assert!(tracer.is_empty());
245    }
246
247    #[test]
248    fn test_null_tracer_len() {
249        let tracer = NullTracer::new();
250        assert_eq!(tracer.len(), 0);
251    }
252}