pub struct LatencyMiddleware { /* private fields */ }Expand description
Middleware that records latency metrics for tool calls.
Stores ToolLatency entries that can be retrieved via LatencyMiddleware::tool_latencies.
Thread-safe and suitable for use across async tasks.
§Examples
ⓘ
use gemini_adk_rs::middleware::{LatencyMiddleware, Middleware};
use gemini_genai_rs::prelude::FunctionCall;
let lat = LatencyMiddleware::new();
// In an async context:
lat.before_tool(&call).await.unwrap();
// ... tool executes ...
lat.after_tool(&call, &result).await.unwrap();
let records = lat.tool_latencies();
println!("Tool {} took {:?}", records[0].name, records[0].elapsed);Implementations§
Source§impl LatencyMiddleware
impl LatencyMiddleware
Sourcepub fn tool_latencies(&self) -> Vec<ToolLatency>
pub fn tool_latencies(&self) -> Vec<ToolLatency>
Returns a snapshot of all recorded tool latencies.
Trait Implementations§
Source§impl Default for LatencyMiddleware
impl Default for LatencyMiddleware
Source§impl Middleware for LatencyMiddleware
impl Middleware for LatencyMiddleware
Source§fn before_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_tool<'life0, 'life1, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before a tool is invoked.
Source§fn after_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
_result: &'life2 Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after_tool<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
_result: &'life2 Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after a tool completes successfully.
Source§fn on_tool_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
_err: &'life2 ToolError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_tool_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
call: &'life1 FunctionCall,
_err: &'life2 ToolError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a tool execution fails.
Source§fn before_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before an agent begins execution.
Source§fn after_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn after_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
_ctx: &'life1 InvocationContext,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called after an agent completes execution.
Source§fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_event<'life0, 'life1, 'async_trait>(
&'life0 self,
_event: &'life1 AgentEvent,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when an agent event is emitted.
Source§fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_err: &'life1 AgentError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_error<'life0, 'life1, 'async_trait>(
&'life0 self,
_err: &'life1 AgentError,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when an agent error occurs.
Source§fn before_model<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_model<'life0, 'life1, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before an LLM model call is made. Return
Some(LlmResponse) to skip the LLM call
and use the returned response instead (e.g., for caching, guardrails). Return None to proceed.Source§fn after_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
_response: &'life2 LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after_model<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_request: &'life1 LlmRequest,
_response: &'life2 LlmResponse,
) -> Pin<Box<dyn Future<Output = Result<Option<LlmResponse>, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after an LLM model call completes. Return
Some(LlmResponse) to replace the model’s
response (e.g., for output filtering, safety). Return None to use the original response.Auto Trait Implementations§
impl !Freeze for LatencyMiddleware
impl !RefUnwindSafe for LatencyMiddleware
impl Send for LatencyMiddleware
impl Sync for LatencyMiddleware
impl Unpin for LatencyMiddleware
impl UnwindSafe for LatencyMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more