pub struct Metrics { /* private fields */ }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
impl Metrics
Sourcepub fn init(name: &'static str) -> Arc<Self>
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.
Sourcepub fn global() -> Arc<Self>
pub fn global() -> Arc<Self>
Convenience wrapper that initialises the singleton under the
agent-sdk meter scope.
Sourcepub fn rebind()
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.
Sourcepub fn reset_for_testing()
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.
Sourcepub fn record_chat_token_usage(
&self,
usage: &Usage,
provider_name: &'static str,
request_model: &str,
response_model: &str,
)
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.
Sourcepub fn record_chat_operation_duration_success(
&self,
elapsed_secs: f64,
provider_name: &'static str,
request_model: &str,
response_model: &str,
)
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.
Sourcepub fn record_chat_operation_duration_error(
&self,
elapsed_secs: f64,
provider_name: &'static str,
request_model: &str,
error_type: &'static str,
)
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.
Sourcepub fn record_tool_execution(
&self,
tool_name: &str,
tool_kind: &'static str,
outcome: &'static str,
duration_ms: Option<u64>,
)
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.