Skip to main content

a2a_protocol_server/
metrics.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F.
3
4//! Metrics hooks for observing handler activity.
5//!
6//! Implement [`Metrics`] to receive callbacks on requests, responses, errors,
7//! and queue depth changes. The default no-op implementation can be overridden
8//! selectively.
9//!
10//! # Example
11//!
12//! ```rust,no_run
13//! use a2a_protocol_server::metrics::Metrics;
14//!
15//! struct MyMetrics;
16//!
17//! impl Metrics for MyMetrics {
18//!     fn on_request(&self, method: &str) {
19//!         println!("request: {method}");
20//!     }
21//! }
22//! ```
23
24/// Trait for receiving metrics callbacks from the handler.
25///
26/// All methods have default no-op implementations so that consumers can
27/// override only the callbacks they care about.
28pub trait Metrics: Send + Sync + 'static {
29    /// Called when a request is received, before processing.
30    fn on_request(&self, _method: &str) {}
31
32    /// Called when a response is successfully sent.
33    fn on_response(&self, _method: &str) {}
34
35    /// Called when a request results in an error.
36    fn on_error(&self, _method: &str, _error: &str) {}
37
38    /// Called when the number of active event queues changes.
39    fn on_queue_depth_change(&self, _active_queues: usize) {}
40}
41
42/// A no-op [`Metrics`] implementation that discards all events.
43#[derive(Debug, Default)]
44pub struct NoopMetrics;
45
46impl Metrics for NoopMetrics {}