pub struct Metrics { /* private fields */ }Expand description
Process-wide metric registry. All methods take &self and use relaxed atomics — metrics
are monotonic counters/observations where exact inter-thread ordering doesn’t matter.
Implementations§
Source§impl Metrics
impl Metrics
pub fn new() -> Self
Sourcepub fn record_request(&self, outcome: &str)
pub fn record_request(&self, outcome: &str)
Count one finished request under its outcome label.
Sourcepub fn observe_latency(&self, elapsed: Duration)
pub fn observe_latency(&self, elapsed: Duration)
Observe a request’s end-to-end latency into the histogram.
Sourcepub fn record_llm_latency(&self, ttft: Duration, tpot: Option<Duration>)
pub fn record_llm_latency(&self, ttft: Duration, tpot: Option<Duration>)
Observe a streamed LLM response’s server-side time-to-first-token and, when the response
had more than one output token, its mean time-per-output-token. Called once per streamed
LLM request from the response body’s Drop, after the terminal usage frame is parsed. The
gateway sits in the token stream, so these are measured with no client clock and no in-app
instrumentation — the request-path advantage a trace backend (which only sees span-end
duration) can’t offer.
Sourcepub fn record_ratelimit_hit(&self, scope: &str)
pub fn record_ratelimit_hit(&self, scope: &str)
Count a rate-limit rejection by which limiter scope tripped (ip/route/key).
Sourcepub fn record_waf_hit(&self, class: &str)
pub fn record_waf_hit(&self, class: &str)
Count one WAF rule match by rule class (sqli/xss/path_traversal/custom).
Recorded for both report-only and blocking modes — so a report-first rollout is
visible — while a blocked request is additionally counted under the forbidden
request outcome.
Sourcepub fn record_csp_report(&self)
pub fn record_csp_report(&self)
Count one received CSP violation report.
Sourcepub fn add_usage_request(&self, outcome: &str)
pub fn add_usage_request(&self, outcome: &str)
Count one request toward the drainable usage accumulator (managed mode). Called once per
request from the single finish exit, so every request — proxied or rejected — counts.
outcome is the request’s outcome label; a denial outcome (see [outcome_is_blocked]) also
bumps the drainable blocked accumulator, so the control plane can show what the edge screened.
Sourcepub fn add_usage_bytes(&self, ingress: usize, egress: usize)
pub fn add_usage_bytes(&self, ingress: usize, egress: usize)
Add request (ingress) + response (egress) bytes to the drainable usage accumulator. Called on the proxied path where both bodies are buffered and the counts are known.
Sourcepub fn drain_usage(&self) -> DrainedUsage
pub fn drain_usage(&self) -> DrainedUsage
Atomically read-and-zero the usage accumulators — the delta the usage reporter ships to the control plane (requests + bandwidth + LLM tokens/cost, gateway L4).
Sourcepub fn restore_usage(&self, u: &DrainedUsage)
pub fn restore_usage(&self, u: &DrainedUsage)
Add a previously-drained delta back, e.g. when a usage report failed to send — so the next period reships it instead of losing billable usage. (New requests that arrived during the failed send simply add on top, as intended.)
Sourcepub fn record_llm_usage(&self, model: &str, s: LlmSample)
pub fn record_llm_usage(&self, model: &str, s: LlmSample)
Record one metered LLM request for model: add its four token dimensions and — when the model
was priced — its cost (micro-dollars). cost_micros == None means the model isn’t in the price
book, so tokens are still counted but the request is bucketed unpriced rather than metered.
Also updates the bounded per-model breakdown (edgeguard_llm_model_*).
Sourcepub fn record_llm_team_usage(&self, team: &str, s: &LlmSample)
pub fn record_llm_team_usage(&self, team: &str, s: &LlmSample)
Record one metered LLM request against its team ([llm].team_header value; absent → _none),
for per-team chargeback/showback (edgeguard_llm_team_*). Bounded exactly like the per-model
breakdown. Called alongside Self::record_llm_usage from the request path.
Sourcepub fn record_llm_key_usage(&self, key: &str, s: &LlmSample)
pub fn record_llm_key_usage(&self, key: &str, s: &LlmSample)
Record one metered LLM request against its authenticated key/principal (_anon when
unauthenticated), for per-user cost attribution (edgeguard_llm_key_*). Bounded exactly like
the per-model breakdown. Reuses the existing OSS auth principal as the identity — so per-key
FinOps is reachable without the EE control plane.
Sourcepub fn record_budget_blocked(&self, scope: &str)
pub fn record_budget_blocked(&self, scope: &str)
Record a request blocked by a hard LLM budget, by the budget’s scope (unknown → other).
Sourcepub fn record_budget_consumed(&self, name: &str, ratio: f64)
pub fn record_budget_consumed(&self, name: &str, ratio: f64)
Record the latest consumed ratio (used / limit) for a budget by name — the near-limit
gauge. Last writer wins per name (a coarse “is any budget near its cap” signal). NaN/negative
samples are dropped so a divide-by-zero can’t poison the gauge.
Sourcepub fn record_budget_reconcile_failures(&self, n: usize)
pub fn record_budget_reconcile_failures(&self, n: usize)
Record n budget reconcile/release failures against the shared store (after retries). A
non-zero rate here means the distributed budget counter is drifting — the signal to alert on.
Sourcepub fn record_llm_no_usage(&self)
pub fn record_llm_no_usage(&self)
Record an LLM request whose response carried no usage (error, or a stream the client didn’t
opt into usage on). No tokens/cost, but the request is visible as no_usage.
Sourcepub fn record_keyvault(&self, result: &str)
pub fn record_keyvault(&self, result: &str)
Record a key-vault decision by result (swapped/denied_key/denied_model).
Sourcepub fn record_dlp_finding(&self, category: &str)
pub fn record_dlp_finding(&self, category: &str)
Record one DLP finding under its category (unknown → other).
Sourcepub fn record_dlp_blocked(&self)
pub fn record_dlp_blocked(&self)
Record a request blocked by DLP block mode.