pub struct MetricsStore { /* private fields */ }Expand description
Thread-safe, in-memory store for per-route request metrics.
Create a single instance at server startup and share it (via Arc) with
all request-handling filters. Call MetricsStore::record after each
upstream request completes, and MetricsStore::snapshot to produce a
serializable view for the GET /v1/metrics endpoint.
§Persistence
Metrics are currently held in memory only and are lost on process restart. This is intentional for the initial release — the consuming plugin layer (OpenClaw) can handle its own persistence if needed. A future release may back this store with a database or Redis cache.
Implementations§
Source§impl MetricsStore
impl MetricsStore
Sourcepub fn record(&self, event: RequestMetrics)
pub fn record(&self, event: RequestMetrics)
Records a completed request into the store.
This method acquires a write-lock for a very short duration (counter
increments + a Vec::push) so contention should be negligible.
Sourcepub fn record_success(
&self,
route: String,
endpoint: String,
start: Instant,
input_tokens: Option<u32>,
output_tokens: Option<u32>,
)
pub fn record_success( &self, route: String, endpoint: String, start: Instant, input_tokens: Option<u32>, output_tokens: Option<u32>, )
Records a successful generate (non-streaming) request.
This is a convenience wrapper used by provider handlers to reduce duplicated recording logic across OpenAI, Anthropic and Google filters.
Sourcepub fn record_outcome(
&self,
route: String,
endpoint: String,
start: Instant,
is_error: bool,
)
pub fn record_outcome( &self, route: String, endpoint: String, start: Instant, is_error: bool, )
Records a failed or stream-only request.
is_error should be true for upstream failures and false for
streaming requests where token counts are unavailable.
Sourcepub fn snapshot(&self) -> MetricsSnapshot
pub fn snapshot(&self) -> MetricsSnapshot
Produces a serialisable snapshot of all collected metrics.
The read-lock is held only long enough to clone raw counters and latency
vectors. Expensive work (sorting for percentiles, computing averages) is
performed after the lock is released so that concurrent record() calls
are not blocked by a /v1/metrics request.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MetricsStore
impl RefUnwindSafe for MetricsStore
impl Send for MetricsStore
impl Sync for MetricsStore
impl Unpin for MetricsStore
impl UnsafeUnpin for MetricsStore
impl UnwindSafe for MetricsStore
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
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>
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>
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