gemini_adk_rs/telemetry/
logging.rs

1//! Structured logging helpers for agent lifecycle.
2//!
3//! All log events carry consistent fields for correlation.
4//! Feature-gated behind `tracing-support`.
5
6/// Log that an agent has started.
7#[cfg(feature = "tracing-support")]
8pub fn log_agent_started(agent_name: &str, tool_count: usize) {
9    tracing::info!(
10        agent_name = agent_name,
11        tool_count = tool_count,
12        "Agent started"
13    );
14}
15
16/// Log that an agent has completed.
17#[cfg(feature = "tracing-support")]
18pub fn log_agent_completed(agent_name: &str, duration_ms: f64) {
19    tracing::info!(
20        agent_name = agent_name,
21        duration_ms = duration_ms,
22        "Agent completed"
23    );
24}
25
26/// Log a tool dispatch.
27#[cfg(feature = "tracing-support")]
28pub fn log_tool_dispatch(agent_name: &str, tool_name: &str, tool_class: &str) {
29    tracing::info!(
30        agent_name = agent_name,
31        tool_name = tool_name,
32        tool_class = tool_class,
33        "Tool dispatched"
34    );
35}
36
37/// Log a tool result.
38#[cfg(feature = "tracing-support")]
39pub fn log_tool_result(agent_name: &str, tool_name: &str, success: bool, duration_ms: f64) {
40    tracing::info!(
41        agent_name = agent_name,
42        tool_name = tool_name,
43        success = success,
44        duration_ms = duration_ms,
45        "Tool result"
46    );
47}
48
49/// Log an agent transfer.
50#[cfg(feature = "tracing-support")]
51pub fn log_agent_transfer(from: &str, to: &str) {
52    tracing::info!(from = from, to = to, "Agent transfer");
53}
54
55/// Log an agent error (warn level).
56#[cfg(feature = "tracing-support")]
57pub fn log_agent_error(agent_name: &str, error: &str) {
58    tracing::warn!(agent_name = agent_name, error = error, "Agent error");
59}
60
61/// Log an agent-as-tool dispatch.
62#[cfg(feature = "tracing-support")]
63pub fn log_agent_tool_dispatch(parent: &str, child: &str) {
64    tracing::info!(parent = parent, child = child, "Agent tool dispatch");
65}
66
67/// Log event loop lag (warn level).
68#[cfg(feature = "tracing-support")]
69pub fn log_event_loop_lag(agent_name: &str, skipped: u64) {
70    tracing::warn!(
71        agent_name = agent_name,
72        skipped = skipped,
73        "Event loop lag — skipped events"
74    );
75}
76
77// No-op stubs when tracing is disabled.
78/// Log that an agent has started (no-op without `tracing-support` feature).
79#[cfg(not(feature = "tracing-support"))]
80pub fn log_agent_started(_: &str, _: usize) {}
81/// Log that an agent completed (no-op without `tracing-support` feature).
82#[cfg(not(feature = "tracing-support"))]
83pub fn log_agent_completed(_: &str, _: f64) {}
84/// Log a tool dispatch (no-op without `tracing-support` feature).
85#[cfg(not(feature = "tracing-support"))]
86pub fn log_tool_dispatch(_: &str, _: &str, _: &str) {}
87/// Log a tool result (no-op without `tracing-support` feature).
88#[cfg(not(feature = "tracing-support"))]
89pub fn log_tool_result(_: &str, _: &str, _: bool, _: f64) {}
90/// Log an agent transfer (no-op without `tracing-support` feature).
91#[cfg(not(feature = "tracing-support"))]
92pub fn log_agent_transfer(_: &str, _: &str) {}
93/// Log an agent error (no-op without `tracing-support` feature).
94#[cfg(not(feature = "tracing-support"))]
95pub fn log_agent_error(_: &str, _: &str) {}
96/// Log an agent-as-tool dispatch (no-op without `tracing-support` feature).
97#[cfg(not(feature = "tracing-support"))]
98pub fn log_agent_tool_dispatch(_: &str, _: &str) {}
99/// Log event loop lag (no-op without `tracing-support` feature).
100#[cfg(not(feature = "tracing-support"))]
101pub fn log_event_loop_lag(_: &str, _: u64) {}
102
103/// Log an LLM call.
104#[cfg(feature = "tracing-support")]
105pub fn log_llm_call(
106    model_id: &str,
107    agent_name: &str,
108    prompt_tokens: u32,
109    completion_tokens: u32,
110    duration_ms: f64,
111) {
112    tracing::info!(
113        model_id = model_id,
114        agent_name = agent_name,
115        prompt_tokens = prompt_tokens,
116        completion_tokens = completion_tokens,
117        duration_ms = duration_ms,
118        "LLM call completed"
119    );
120}
121
122/// Log a phase transition.
123#[cfg(feature = "tracing-support")]
124pub fn log_phase_transition(from: &str, to: &str) {
125    tracing::info!(from = from, to = to, "Phase transition");
126}
127
128/// Log an extraction result.
129#[cfg(feature = "tracing-support")]
130pub fn log_extraction_result(extractor: &str, success: bool, duration_ms: f64) {
131    tracing::info!(
132        extractor = extractor,
133        success = success,
134        duration_ms = duration_ms,
135        "Extraction completed"
136    );
137}
138
139/// Log session persistence.
140#[cfg(feature = "tracing-support")]
141pub fn log_session_persisted(session_id: &str, backend: &str, duration_ms: f64) {
142    tracing::info!(
143        session_id = session_id,
144        backend = backend,
145        duration_ms = duration_ms,
146        "Session persisted"
147    );
148}
149
150// No-op stubs for new logging functions when tracing is disabled.
151/// Log an LLM call (no-op without `tracing-support` feature).
152#[cfg(not(feature = "tracing-support"))]
153pub fn log_llm_call(_: &str, _: &str, _: u32, _: u32, _: f64) {}
154/// Log a phase transition (no-op without `tracing-support` feature).
155#[cfg(not(feature = "tracing-support"))]
156pub fn log_phase_transition(_: &str, _: &str) {}
157/// Log an extraction result (no-op without `tracing-support` feature).
158#[cfg(not(feature = "tracing-support"))]
159pub fn log_extraction_result(_: &str, _: bool, _: f64) {}
160/// Log session persistence (no-op without `tracing-support` feature).
161#[cfg(not(feature = "tracing-support"))]
162pub fn log_session_persisted(_: &str, _: &str, _: f64) {}