pub struct TelemetryHook<M: CompletionModel> { /* private fields */ }Expand description
Per-request hook that emits structured observability events from the five
PromptHook lifecycle methods.
M is the CompletionModel used by the agent. The hook is generic so a
single rig-tap build can attach to OpenAI, Anthropic, Ollama, etc.
§Example
use rig_tap::{TelemetryHook, TelemetryHookConfig};
TelemetryHook::new(TelemetryHookConfig::new("gpt-4o", "thread-1"))Implementations§
Source§impl<M: CompletionModel> TelemetryHook<M>
impl<M: CompletionModel> TelemetryHook<M>
Sourcepub fn new(config: TelemetryHookConfig) -> Self
pub fn new(config: TelemetryHookConfig) -> Self
Build a hook from config.
Sourcepub fn with_defaults(
model: impl Into<String>,
conversation_id: impl Into<String>,
) -> Self
pub fn with_defaults( model: impl Into<String>, conversation_id: impl Into<String>, ) -> Self
Convenience: build a hook stamping events with model and
conversation_id and default truncation.
Sourcepub fn with_conversation_id_resolver<F>(self, resolver: F) -> Self
pub fn with_conversation_id_resolver<F>(self, resolver: F) -> Self
Register a per-request resolver for the conversation ID. The
resolver is consulted on every emission; if it returns Some(id),
that value is stamped on the event instead of
TelemetryHookConfig::conversation_id.
Typical wiring: a tokio::task_local! (or equivalent) set by the
host on every request, read by the closure.
Sourcepub fn with_model_resolver<F>(self, resolver: F) -> Self
pub fn with_model_resolver<F>(self, resolver: F) -> Self
Register a resolver that extracts the concrete model identifier
from each CompletionResponse. When the resolver returns
Some(model), that value is stamped on prompt.completed
instead of TelemetryHookConfig::model.
Use this with routed providers (OpenRouter, Bedrock routing, vendor multi-model endpoints) where the configured model name is a logical alias and the response payload carries the actual model that served the request.
Sourcepub fn with_previous_response_id_resolver<F>(self, resolver: F) -> Self
pub fn with_previous_response_id_resolver<F>(self, resolver: F) -> Self
Register a resolver that returns the chain ancestor
(previous_response_id) sent to the provider for the current turn.
When the resolver returns Some(id), that value is stamped on
prompt.completed’s previous_response_id field.
Use this with stateful endpoints — OpenAI Responses, future Anthropic/Google equivalents — where the host runtime tracks the chain (typically in a task-local or session object) and the provider response payload does not echo the value back.
Sourcepub fn with_sampling_policy(self, policy: Arc<dyn SamplingPolicy>) -> Self
pub fn with_sampling_policy(self, policy: Arc<dyn SamplingPolicy>) -> Self
Install a SamplingPolicy that gates every prompt.* and
tool.* emission from this hook. The default policy is
AlwaysSample.
Pairing: the hook passes the resolved conversation id as the
correlator for prompt.* events and the internal call id for
tool.* events. Policies that hash the correlator (such as
RatePolicy) therefore keep
tool.invoked / tool.completed pairs coherent
automatically.
§Example
use std::sync::Arc;
use rig_tap::{RatePolicy, TelemetryHook, TelemetryHookConfig};
TelemetryHook::new(TelemetryHookConfig::new("gpt-4o", "thread-1"))
.with_sampling_policy(Arc::new(
RatePolicy::new()
.with_rate("tool.invoked", 0.1)
.with_rate("tool.completed", 0.1),
))Sourcepub fn observe_prompt_error(&self, error: &PromptError)
pub fn observe_prompt_error(&self, error: &PromptError)
Observe a failure in the prompt loop. Call this when the agent’s prompt execution returns an error.
Sourcepub fn observe_tool_error(
&self,
tool_name: &str,
call_id: &str,
error: &dyn Error,
)
pub fn observe_tool_error( &self, tool_name: &str, call_id: &str, error: &dyn Error, )
Observe a failure in a tool invocation. Call this when a tool returns a failure.
Trait Implementations§
Source§impl<M: CompletionModel> Clone for TelemetryHook<M>
impl<M: CompletionModel> Clone for TelemetryHook<M>
Source§impl<M: CompletionModel> Debug for TelemetryHook<M>
impl<M: CompletionModel> Debug for TelemetryHook<M>
Source§impl<M> PromptHook<M> for TelemetryHook<M>where
M: CompletionModel,
impl<M> PromptHook<M> for TelemetryHook<M>where
M: CompletionModel,
Source§async fn on_completion_call(
&self,
_prompt: &Message,
history: &[Message],
) -> HookAction
async fn on_completion_call( &self, _prompt: &Message, history: &[Message], ) -> HookAction
Source§async fn on_completion_response(
&self,
_prompt: &Message,
response: &CompletionResponse<M::Response>,
) -> HookAction
async fn on_completion_response( &self, _prompt: &Message, response: &CompletionResponse<M::Response>, ) -> HookAction
Source§async fn on_tool_call(
&self,
tool_name: &str,
tool_call_id: Option<String>,
internal_call_id: &str,
args: &str,
) -> ToolCallHookAction
async fn on_tool_call( &self, tool_name: &str, tool_call_id: Option<String>, internal_call_id: &str, args: &str, ) -> ToolCallHookAction
Source§async fn on_tool_result(
&self,
tool_name: &str,
tool_call_id: Option<String>,
internal_call_id: &str,
_args: &str,
result: &str,
) -> HookAction
async fn on_tool_result( &self, tool_name: &str, tool_call_id: Option<String>, internal_call_id: &str, _args: &str, result: &str, ) -> HookAction
Source§fn on_text_delta(
&self,
_text_delta: &str,
_aggregated_text: &str,
) -> impl Future<Output = HookAction> + Send
fn on_text_delta( &self, _text_delta: &str, _aggregated_text: &str, ) -> impl Future<Output = HookAction> + Send
Source§fn on_tool_call_delta(
&self,
_tool_call_id: &str,
_internal_call_id: &str,
_tool_name: Option<&str>,
_tool_call_delta: &str,
) -> impl Future<Output = HookAction> + Send
fn on_tool_call_delta( &self, _tool_call_id: &str, _internal_call_id: &str, _tool_name: Option<&str>, _tool_call_delta: &str, ) -> impl Future<Output = HookAction> + Send
tool_name is Some on the first delta for a tool call, None on subsequent deltas.