pub struct MetricsMiddleware { /* private fields */ }Expand description
Metrics collection middleware for observability.
§Examples
use pmcp::shared::{MetricsMiddleware, AdvancedMiddleware, MiddlewareContext};
use pmcp::types::{JSONRPCRequest, RequestId};
let metrics = MetricsMiddleware::new("pmcp_client".to_string());
let context = MiddlewareContext::default();
let mut request = JSONRPCRequest {
jsonrpc: "2.0".to_string(),
method: "resources.list".to_string(),
params: None,
id: RequestId::from(789i64),
};
// Automatically collects timing and count metrics
metrics.on_request_with_context(&mut request, &context).await?;Implementations§
Source§impl MetricsMiddleware
impl MetricsMiddleware
Sourcepub fn get_request_count(&self, method: &str) -> u64
pub fn get_request_count(&self, method: &str) -> u64
Get request count for a method.
Sourcepub fn get_error_count(&self, method: &str) -> u64
pub fn get_error_count(&self, method: &str) -> u64
Get error count for a method.
Sourcepub fn get_average_duration(&self, method: &str) -> u64
pub fn get_average_duration(&self, method: &str) -> u64
Get average duration for a method in microseconds.
Trait Implementations§
Source§impl AdvancedMiddleware for MetricsMiddleware
impl AdvancedMiddleware for MetricsMiddleware
Source§fn priority(&self) -> MiddlewarePriority
fn priority(&self) -> MiddlewarePriority
Get middleware priority for execution ordering
Source§fn on_request_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 mut JSONRPCRequest,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_request_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
request: &'life1 mut JSONRPCRequest,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called before a request is sent with context.
Source§fn on_response_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 mut JSONRPCResponse,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_response_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
response: &'life1 mut JSONRPCResponse,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called after a response is received with context.
Source§fn on_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
error: &'life1 Error,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_error<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
error: &'life1 Error,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when an error occurs in the chain
Source§fn should_execute<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn should_execute<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Check if middleware should be executed for this context
Source§fn on_send_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_send_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a message is sent with context.
Source§fn on_receive_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_receive_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
message: &'life1 TransportMessage,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when a message is received with context.
Source§fn on_notification_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
notification: &'life1 mut JSONRPCNotification,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn on_notification_with_context<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
notification: &'life1 mut JSONRPCNotification,
context: &'life2 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Called when an unsolicited notification is received with context. Read more
Source§fn on_chain_start<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_chain_start<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when middleware chain starts
Source§fn on_chain_complete<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_chain_complete<'life0, 'life1, 'async_trait>(
&'life0 self,
_context: &'life1 MiddlewareContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called when middleware chain completes
Auto Trait Implementations§
impl !RefUnwindSafe for MetricsMiddleware
impl !UnwindSafe for MetricsMiddleware
impl Freeze for MetricsMiddleware
impl Send for MetricsMiddleware
impl Sync for MetricsMiddleware
impl Unpin for MetricsMiddleware
impl UnsafeUnpin for MetricsMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more