gemini_adk_rs/telemetry/
metrics.rs

1//! Prometheus metric definitions for agent lifecycle — counters, histograms.
2//!
3//! Feature-gated behind `metrics`. When disabled, all functions compile to no-ops.
4
5/// Record that an agent has started.
6#[cfg(feature = "metrics")]
7pub fn record_agent_started(agent_name: &str) {
8    metrics::counter!("gemini_agent_started_total", "agent" => agent_name.to_string()).increment(1);
9}
10
11/// Record that an agent has completed, with its duration.
12#[cfg(feature = "metrics")]
13pub fn record_agent_completed(agent_name: &str, duration_ms: f64) {
14    metrics::counter!("gemini_agent_completed_total", "agent" => agent_name.to_string())
15        .increment(1);
16    metrics::histogram!("gemini_agent_duration_ms", "agent" => agent_name.to_string())
17        .record(duration_ms);
18}
19
20/// Record an agent error.
21#[cfg(feature = "metrics")]
22pub fn record_agent_error(agent_name: &str, error_type: &str) {
23    metrics::counter!("gemini_agent_errors_total", "agent" => agent_name.to_string(), "error_type" => error_type.to_string())
24        .increment(1);
25}
26
27/// Record that a tool was dispatched by an agent.
28#[cfg(feature = "metrics")]
29pub fn record_agent_tool_dispatched(agent_name: &str, tool_name: &str) {
30    metrics::counter!("gemini_agent_tool_dispatched_total", "agent" => agent_name.to_string(), "tool" => tool_name.to_string())
31        .increment(1);
32}
33
34/// Record tool execution duration for an agent.
35#[cfg(feature = "metrics")]
36pub fn record_agent_tool_duration(agent_name: &str, tool_name: &str, duration_ms: f64) {
37    metrics::histogram!("gemini_agent_tool_duration_ms", "agent" => agent_name.to_string(), "tool" => tool_name.to_string())
38        .record(duration_ms);
39}
40
41/// Record an agent transfer from one agent to another.
42#[cfg(feature = "metrics")]
43pub fn record_agent_transfer(from: &str, to: &str) {
44    metrics::counter!("gemini_agent_transfers_total", "from" => from.to_string(), "to" => to.to_string())
45        .increment(1);
46}
47
48/// Record an agent-as-tool dispatch with duration.
49#[cfg(feature = "metrics")]
50pub fn record_agent_tool_dispatch(parent_agent: &str, child_agent: &str, duration_ms: f64) {
51    metrics::counter!("gemini_agent_tool_dispatch_total", "parent" => parent_agent.to_string(), "child" => child_agent.to_string())
52        .increment(1);
53    metrics::histogram!("gemini_agent_tool_dispatch_duration_ms", "parent" => parent_agent.to_string(), "child" => child_agent.to_string())
54        .record(duration_ms);
55}
56
57/// Record event loop lag (skipped events).
58#[cfg(feature = "metrics")]
59pub fn record_event_loop_lag(agent_name: &str, skipped: u64) {
60    metrics::counter!("gemini_agent_event_loop_lag_total", "agent" => agent_name.to_string())
61        .increment(skipped);
62}
63
64// No-op stubs when metrics feature is disabled.
65/// Record that an agent has started (no-op without `metrics` feature).
66#[cfg(not(feature = "metrics"))]
67pub fn record_agent_started(_: &str) {}
68/// Record that an agent completed (no-op without `metrics` feature).
69#[cfg(not(feature = "metrics"))]
70pub fn record_agent_completed(_: &str, _: f64) {}
71/// Record an agent error (no-op without `metrics` feature).
72#[cfg(not(feature = "metrics"))]
73pub fn record_agent_error(_: &str, _: &str) {}
74/// Record a tool dispatch (no-op without `metrics` feature).
75#[cfg(not(feature = "metrics"))]
76pub fn record_agent_tool_dispatched(_: &str, _: &str) {}
77/// Record tool execution duration (no-op without `metrics` feature).
78#[cfg(not(feature = "metrics"))]
79pub fn record_agent_tool_duration(_: &str, _: &str, _: f64) {}
80/// Record an agent transfer (no-op without `metrics` feature).
81#[cfg(not(feature = "metrics"))]
82pub fn record_agent_transfer(_: &str, _: &str) {}
83/// Record agent-as-tool dispatch (no-op without `metrics` feature).
84#[cfg(not(feature = "metrics"))]
85pub fn record_agent_tool_dispatch(_: &str, _: &str, _: f64) {}
86/// Record event loop lag (no-op without `metrics` feature).
87#[cfg(not(feature = "metrics"))]
88pub fn record_event_loop_lag(_: &str, _: u64) {}
89
90/// Record LLM call with duration and token counts.
91#[cfg(feature = "metrics")]
92pub fn record_llm_call(
93    model_id: &str,
94    agent_name: &str,
95    duration_ms: f64,
96    prompt_tokens: u32,
97    completion_tokens: u32,
98) {
99    metrics::counter!("gemini_llm_calls_total", "model" => model_id.to_string(), "agent" => agent_name.to_string())
100        .increment(1);
101    metrics::histogram!("gemini_llm_call_duration_ms", "model" => model_id.to_string(), "agent" => agent_name.to_string())
102        .record(duration_ms);
103    metrics::counter!("gemini_llm_prompt_tokens_total", "model" => model_id.to_string(), "agent" => agent_name.to_string())
104        .increment(prompt_tokens as u64);
105    metrics::counter!("gemini_llm_completion_tokens_total", "model" => model_id.to_string(), "agent" => agent_name.to_string())
106        .increment(completion_tokens as u64);
107}
108
109/// Record total token usage.
110#[cfg(feature = "metrics")]
111pub fn record_token_usage(model_id: &str, prompt_tokens: u32, completion_tokens: u32) {
112    metrics::counter!("gemini_token_usage_prompt_total", "model" => model_id.to_string())
113        .increment(prompt_tokens as u64);
114    metrics::counter!("gemini_token_usage_completion_total", "model" => model_id.to_string())
115        .increment(completion_tokens as u64);
116}
117
118/// Record a phase transition.
119#[cfg(feature = "metrics")]
120pub fn record_phase_transition(from: &str, to: &str) {
121    metrics::counter!("gemini_phase_transitions_total", "from" => from.to_string(), "to" => to.to_string())
122        .increment(1);
123}
124
125/// Record extraction duration.
126#[cfg(feature = "metrics")]
127pub fn record_extraction_duration(extractor: &str, duration_ms: f64) {
128    metrics::histogram!("gemini_extraction_duration_ms", "extractor" => extractor.to_string())
129        .record(duration_ms);
130}
131
132/// Record session persistence duration.
133#[cfg(feature = "metrics")]
134pub fn record_persistence_duration(backend: &str, duration_ms: f64) {
135    metrics::histogram!("gemini_persistence_duration_ms", "backend" => backend.to_string())
136        .record(duration_ms);
137}
138
139// No-op stubs for new metrics when feature is disabled.
140/// Record LLM call (no-op without `metrics` feature).
141#[cfg(not(feature = "metrics"))]
142pub fn record_llm_call(_: &str, _: &str, _: f64, _: u32, _: u32) {}
143/// Record total token usage (no-op without `metrics` feature).
144#[cfg(not(feature = "metrics"))]
145pub fn record_token_usage(_: &str, _: u32, _: u32) {}
146/// Record a phase transition (no-op without `metrics` feature).
147#[cfg(not(feature = "metrics"))]
148pub fn record_phase_transition(_: &str, _: &str) {}
149/// Record extraction duration (no-op without `metrics` feature).
150#[cfg(not(feature = "metrics"))]
151pub fn record_extraction_duration(_: &str, _: f64) {}
152/// Record session persistence duration (no-op without `metrics` feature).
153#[cfg(not(feature = "metrics"))]
154pub fn record_persistence_duration(_: &str, _: f64) {}