pub struct ApiMetrics { /* private fields */ }Expand description
Thread-safe metrics collector for API endpoints.
Tracks per-path request counts, response times, error counts, and active request gauge.
§Example
use auth_framework::api::metrics::ApiMetrics;
use std::time::Duration;
use axum::http::StatusCode;
let m = ApiMetrics::new();
m.record_request("/login");
m.record_response("/login", Duration::from_millis(5), StatusCode::OK);
let snap = m.get_metrics();
assert_eq!(snap.total_requests, 1);Implementations§
Source§impl ApiMetrics
impl ApiMetrics
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty metrics collector.
§Example
use auth_framework::api::metrics::ApiMetrics;
let m = ApiMetrics::new();
assert_eq!(m.get_metrics().total_requests, 0);Sourcepub fn record_request(&self, path: &str)
pub fn record_request(&self, path: &str)
Record an incoming request for path.
Increments the request counter and the active-requests gauge.
§Example
use auth_framework::api::metrics::ApiMetrics;
let m = ApiMetrics::new();
m.record_request("/health");Sourcepub fn record_response(
&self,
path: &str,
duration: Duration,
status: StatusCode,
)
pub fn record_response( &self, path: &str, duration: Duration, status: StatusCode, )
Record a completed response for path.
Stores the response duration, increments error counters for
4xx/5xx status codes, and decrements the active-requests gauge.
§Example
use auth_framework::api::metrics::ApiMetrics;
use std::time::Duration;
use axum::http::StatusCode;
let m = ApiMetrics::new();
m.record_request("/api");
m.record_response("/api", Duration::from_millis(12), StatusCode::OK);Sourcepub fn get_metrics(&self) -> MetricsSnapshot
pub fn get_metrics(&self) -> MetricsSnapshot
Snapshot all collected metrics.
Returns a MetricsSnapshot with uptime, totals, and per-endpoint
statistics including average, p95, and p99 response times.
§Example
use auth_framework::api::metrics::ApiMetrics;
let m = ApiMetrics::new();
let snap = m.get_metrics();
assert_eq!(snap.total_requests, 0);Sourcepub fn reset(&self)
pub fn reset(&self)
Reset all counters and timers.
Clears request counts, response times, error counts, and resets the uptime clock.
§Example
use auth_framework::api::metrics::ApiMetrics;
use std::time::Duration;
use axum::http::StatusCode;
let m = ApiMetrics::new();
m.record_request("/x");
m.record_response("/x", Duration::from_millis(1), StatusCode::OK);
m.reset();
assert_eq!(m.get_metrics().total_requests, 0);Trait Implementations§
Source§impl Clone for ApiMetrics
impl Clone for ApiMetrics
Source§fn clone(&self) -> ApiMetrics
fn clone(&self) -> ApiMetrics
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ApiMetrics
impl Debug for ApiMetrics
Auto Trait Implementations§
impl Freeze for ApiMetrics
impl RefUnwindSafe for ApiMetrics
impl Send for ApiMetrics
impl Sync for ApiMetrics
impl Unpin for ApiMetrics
impl UnsafeUnpin for ApiMetrics
impl UnwindSafe for ApiMetrics
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more