Skip to main content

Metrics

Struct Metrics 

Source
pub struct Metrics { /* private fields */ }
Available on crate feature otel only.
Expand description

Container for every metric instrument the SDK records.

The struct is held behind an Arc so call sites that need frequent recording (per LLM call, per tool call) can clone the handle into an async future without re-walking a Mutex.

The streaming TTFC / TPOC histograms live alongside the non-streaming operation_duration because the recorder (agent_loop::llm::process_stream) treats them as paired instruments — one fires once per stream, the other once per post-first chunk.

Implementations§

Source§

impl Metrics

Source

pub fn init(name: &'static str) -> Arc<Self>

Build instruments under the supplied meter scope and cache the resulting Arc<Metrics> for subsequent Metrics::global callers.

If the cache is already populated the supplied name is ignored and the cached handle is returned — the first caller in the process wins. Tests that rotate the global meter provider between cases must call Metrics::reset_for_testing beforehand so the next init rebuilds against the fresh provider.

Source

pub fn global() -> Arc<Self>

Convenience wrapper that initialises the singleton under the agent-sdk meter scope.

Source

pub fn rebind()

Drop the cached instrument singleton so the next Metrics::global call rebuilds against the currently installed global meter provider.

agent_sdk_otel::install_global_provider calls this immediately after global::set_meter_provider. Without it, any telemetry path that lazily built the singleton before the provider was installed (or before a re-install) would stay bound to the no-op meter — or a now-shut-down provider — for the rest of the process, silently dropping every counter / histogram. Calling it at install time is safe because no real data points exist yet; it must NOT be used mid-run, where rebuilding would lose in-flight aggregation.

Source

pub fn reset_for_testing()

Drop the cached singleton.

Test-only escape hatch so tests that rotate the global meter provider between cases force a rebuild against the fresh provider.

Source

pub fn record_chat_token_usage( &self, usage: &Usage, provider_name: &'static str, request_model: &str, response_model: &str, )

Record the gen_ai.client.token.usage histogram for a chat response, splitting one data point per non-zero token type (input / output / cache_read / cache_creation).

This is the single source of truth for the token-usage label set so the in-process agent_loop and the daemon-hosted agent-server worker emit byte-identical labels. Splitting by type keeps the histogram aggregatable in Prometheus / Grafana — collapsing the four types into one record would erase the cache-hit-ratio dimension dashboards care about most.

Source

pub fn record_chat_operation_duration_success( &self, elapsed_secs: f64, provider_name: &'static str, request_model: &str, response_model: &str, )

Record a gen_ai.client.operation.duration sample for a successful chat call. The label set mirrors the success arm of the in-process loop so both code paths land in the same series.

Source

pub fn record_chat_operation_duration_error( &self, elapsed_secs: f64, provider_name: &'static str, request_model: &str, error_type: &'static str, )

Record a gen_ai.client.operation.duration sample for a failed chat call, carrying the stable error.type label in place of the response model. Mirrors the error arm of the in-process loop.

Source

pub fn record_tool_execution( &self, tool_name: &str, tool_kind: &'static str, outcome: &'static str, duration_ms: Option<u64>, )

Record the agent_sdk.tools.execution.count counter and, when a duration is known, the agent_sdk.tools.execution.duration histogram for a single tool invocation.

outcome is one of the stable strings emitted by the loop (success / error / blocked / rejected / awaiting_confirmation). Both instruments share the same three labels (gen_ai.tool.name, agent_sdk.tool.kind, agent_sdk.tool.outcome) so a dashboard can join the count and the duration without translation.

Trait Implementations§

Source§

impl Debug for Metrics

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more