pub trait Metrics:
Send
+ Sync
+ 'static {
Show 17 methods
// Provided methods
fn on_request(&self, _method: &str) { ... }
fn on_response(&self, _method: &str) { ... }
fn on_error(&self, _method: &str, _error_kind: &str) { ... }
fn on_latency(&self, _method: &str, _duration: Duration) { ... }
fn on_queue_depth_change(&self, _active_queues: usize) { ... }
fn on_connection_pool_stats(&self, _stats: &ConnectionPoolStats) { ... }
fn on_persistence_error(&self, _operation: &str, _error_kind: &str) { ... }
fn on_store_retry(&self, _operation: &str, _cause: &str) { ... }
fn on_push_delivery(&self, _outcome: &str) { ... }
fn on_rpc_call(&self, _call: &RpcCall<'_>) { ... }
fn on_executor_run(&self, _outcome: &str, _duration: Duration) { ... }
fn on_refusal(&self, _cause: &str) { ... }
fn on_lease(&self, _event: &str) { ... }
fn on_halt(&self, _scope: &str, _action: &str, _stopped: usize) { ... }
fn on_steering(&self, _outcome: &str) { ... }
fn on_claim(&self, _event: &str) { ... }
fn on_subscribe_wake(&self, _cause: &str) { ... }
}Expand description
Trait for receiving metrics callbacks from the handler.
All methods have default no-op implementations so that consumers can override only the callbacks they care about.
Provided Methods§
Sourcefn on_request(&self, _method: &str)
fn on_request(&self, _method: &str)
Called when a request is received, before processing.
Sourcefn on_response(&self, _method: &str)
fn on_response(&self, _method: &str)
Called when a response is successfully sent.
Sourcefn on_error(&self, _method: &str, _error_kind: &str)
fn on_error(&self, _method: &str, _error_kind: &str)
Called when a request results in an error.
error_kind is a bounded, low-cardinality discriminant (e.g.
ServerError::metric_label), never
the free-form error message. Implementations may use it as a metric
label/attribute; the caller guarantees it draws from a small fixed set,
so a client cannot inflate metric cardinality through it.
Sourcefn on_latency(&self, _method: &str, _duration: Duration)
fn on_latency(&self, _method: &str, _duration: Duration)
Called when a request completes (successfully or not) with the wall-clock duration from receipt to response.
This is the #1 production observability metric — use it to feed histograms, percentile trackers, or SLO dashboards.
Sourcefn on_queue_depth_change(&self, _active_queues: usize)
fn on_queue_depth_change(&self, _active_queues: usize)
Called when the number of active event queues changes.
Sourcefn on_connection_pool_stats(&self, _stats: &ConnectionPoolStats)
fn on_connection_pool_stats(&self, _stats: &ConnectionPoolStats)
Called with connection pool statistics when available.
Useful for monitoring connection pool health and detecting exhaustion.
Sourcefn on_persistence_error(&self, _operation: &str, _error_kind: &str)
fn on_persistence_error(&self, _operation: &str, _error_kind: &str)
Called when the background event processor fails to persist a task.
§Why this is not just a log line
This is the SDK’s one path that can lose data without the client
noticing. The streaming reader is a separate subscriber to the event
queue, so it receives an event whether or not the store accepted it: a
caller watching the stream sees the artifact arrive and the task
complete, while a later GetTask returns a task without it.
Until this callback existed, the only report of that was a
tracing::error! — and tracing was not then a default feature of
this crate, so a default build lost the record silently. A metrics
callback is always compiled, so the signal cannot be feature-gated
away.
Treat any non-zero rate here as data loss in progress. The usual causes are a full disk, an unreachable database, or a store rejecting writes under its own capacity limit.
operation and error_kind are both bounded, low-cardinality
discriminants — operation is one of the constants in
persistence_operation, and error_kind comes from
A2aError::metric_label.
Neither carries a task id or a free-form message, so a client cannot
inflate metric cardinality through them.
Sourcefn on_store_retry(&self, _operation: &str, _cause: &str)
fn on_store_retry(&self, _operation: &str, _cause: &str)
Called each time a store operation is run again because it could not
be done the first time for a reason that passes: the store answered
“not now” (cause = "unavailable", a turn’s event write, retried for
up to HandlerLimits::store_write_retry), or a PostgreSQL statement
lost its connection and ran once more on a fresh one
(operation = "statement", cause = "connection_lost").
A retry that succeeded loses nothing, which is why this is not
on_persistence_error; a rising rate is
the store degrading before it starts losing data. operation is one
of persistence_operation; both labels are bounded.
Sourcefn on_push_delivery(&self, _outcome: &str)
fn on_push_delivery(&self, _outcome: &str)
Called after each attempt to deliver a push notification.
outcome is one of delivered, failed, or timeout.
Push delivery is outward-facing and asynchronous: nothing in the
request path observes it, and a webhook that has been refusing every
delivery for a day looks exactly like one that was never configured.
As with on_persistence_error, the
previous report was a tracing macro that a default build compiles
away.
Sourcefn on_rpc_call(&self, _call: &RpcCall<'_>)
fn on_rpc_call(&self, _call: &RpcCall<'_>)
Called once per inbound A2A call, on every binding, when it finishes —
with what the OpenTelemetry semantic conventions’
rpc.server.call.duration records. See RpcCall.
Unlike on_request and its siblings, this sees
a call the handler never reached: a body that is not JSON-RPC, an
unknown method, a call the peer abandoned. Those are the failures a
dashboard built on the handler’s callbacks alone could not show.
Sourcefn on_executor_run(&self, _outcome: &str, _duration: Duration)
fn on_executor_run(&self, _outcome: &str, _duration: Duration)
Called once when an agent executor’s run ends, with how it ended and how long the run took.
The run is the executor’s work for one turn of a task, which usually
outlives the call that started it (a streaming send, a task that
waits for input), so on_rpc_call’s duration
does not measure it. outcome is one of the constants in
executor_outcome. Reported for every executor, whatever it does
inside; an agent built on the tool loop also has the gen_ai.*
metrics for its model and tool calls.
Sourcefn on_refusal(&self, _cause: &str)
fn on_refusal(&self, _cause: &str)
Called when a request is refused before any work, with why: one of
control::refusal. The same refusal is also an
on_error of class overloaded; this says which
limit or store refused it (audit H11).
Sourcefn on_lease(&self, _event: &str)
fn on_lease(&self, _event: &str)
Called on a task lease event: one of control::lease.
Sourcefn on_halt(&self, _scope: &str, _action: &str, _stopped: usize)
fn on_halt(&self, _scope: &str, _action: &str, _stopped: usize)
Called when a halt or a resume is applied: scope is one of
control::halt_scope, action one of control::halt_action, and
stopped how many running tasks a halt stopped (0 for a resume).
Sourcefn on_steering(&self, _outcome: &str)
fn on_steering(&self, _outcome: &str)
Called once for each steering message (ADR 0036) the server decides
about, with how it ended: one of control::steering.
A steering message is a send that never reaches the executor’s start,
so neither on_executor_run nor a task
state shows it; this is the one place a refused or lost one is
counted. Not overriding it costs exactly that: a full inbox, a turn
that ended before it read what was sent, and a handed-off message that
never arrived are then visible only in the logs (the call’s own
on_error still counts the refusals). The
bundled OtelMetrics exports it as a2a.server.steering.
Sourcefn on_claim(&self, _event: &str)
fn on_claim(&self, _event: &str)
Called on a turn-claim event: one of control::claim.
Sourcefn on_subscribe_wake(&self, _cause: &str)
fn on_subscribe_wake(&self, _cause: &str)
Called when a SubscribeToTask stream that waits on a task’s event
log (a stream on a replica that does not run the task) stops waiting
to read the store again: cause is one of subscribe_wake.
Reported only for a store that can wake a stream
(TaskStore::watch_event_log
returned a watch): its notification against its timeout is how
much of the waiting the store’s notifications did, and a store that
cannot wake a stream reports nothing, as it wakes none. Not
overriding it loses that ratio, and with it the sign that the
notifications stopped arriving and every stream is back to polling.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T> Metrics for Arc<T>
Blanket implementation: Arc<T> implements Metrics if T does.
impl<T> Metrics for Arc<T>
Blanket implementation: Arc<T> implements Metrics if T does.
This eliminates the need for wrapper types like MetricsForward when
sharing a metrics instance across multiple handlers or tasks.