gemini_genai_rs/telemetry/
logging.rs

1//! Structured logging helpers.
2//!
3//! All log events carry consistent fields (session_id, phase) for correlation.
4//! Always compiled: the `tracing` facade is an unconditional dependency;
5//! spans are no-ops unless a subscriber is installed.
6
7/// Log a session lifecycle event.
8pub fn log_session_event(session_id: &str, phase: &str, event: &str) {
9    tracing::info!(
10        session_id = session_id,
11        phase = phase,
12        event = event,
13        "Session event"
14    );
15}
16
17/// Log a tool call dispatch.
18pub fn log_tool_call(session_id: &str, function_name: &str, call_count: usize) {
19    tracing::info!(
20        session_id = session_id,
21        event = "tool_call_received",
22        function_name = function_name,
23        function_count = call_count,
24        "Model requested function calls"
25    );
26}
27
28/// Log a WebSocket error (warn level).
29pub fn log_ws_error(session_id: &str, error: &str) {
30    tracing::warn!(
31        session_id = session_id,
32        event = "websocket_error",
33        error = error,
34        "WebSocket error"
35    );
36}
37
38/// Log a jitter buffer underrun (warn level).
39pub fn log_jitter_underrun(session_id: &str, depth_ms: f64) {
40    tracing::warn!(
41        session_id = session_id,
42        event = "jitter_underrun",
43        depth_ms = depth_ms,
44        "Jitter buffer underrun"
45    );
46}
47
48/// Log a reconnection attempt (warn level).
49pub fn log_reconnection(session_id: &str, attempt: u32, delay_ms: u64) {
50    tracing::warn!(
51        session_id = session_id,
52        event = "reconnection",
53        attempt = attempt,
54        delay_ms = delay_ms,
55        "Reconnection attempt"
56    );
57}
58
59/// Log VAD state change (debug level).
60pub fn log_vad_event(session_id: &str, event: &str) {
61    tracing::debug!(
62        session_id = session_id,
63        event = "vad_state_change",
64        vad_event = event,
65        "VAD event"
66    );
67}
68
69/// Log an HTTP request (info level).
70pub fn log_http_request(method: &str, url: &str) {
71    tracing::info!(
72        event = "http_request",
73        http.method = method,
74        http.url = url,
75        "HTTP request"
76    );
77}
78
79/// Log an HTTP response (info level).
80pub fn log_http_response(status: u16, duration_ms: f64) {
81    tracing::info!(
82        event = "http_response",
83        http.status = status,
84        duration_ms = duration_ms,
85        "HTTP response"
86    );
87}
88
89/// Log an HTTP retry attempt (warn level).
90pub fn log_http_retry(url: &str, attempt: u32, delay_ms: u64) {
91    tracing::warn!(
92        event = "http_retry",
93        http.url = url,
94        attempt = attempt,
95        delay_ms = delay_ms,
96        "HTTP retry"
97    );
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn noop_log_functions_compile() {
106        // All log functions should compile and not panic as no-ops.
107        log_session_event("sess-1", "greeting", "connected");
108        log_tool_call("sess-1", "get_weather", 1);
109        log_ws_error("sess-1", "connection reset");
110        log_jitter_underrun("sess-1", 50.0);
111        log_reconnection("sess-1", 1, 1000);
112        log_vad_event("sess-1", "speech_start");
113        log_http_request("GET", "https://example.com");
114        log_http_response(200, 42.5);
115        log_http_retry("https://example.com", 2, 2000);
116    }
117}