Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
feat(switchyard-server): `/v1/stats/reset` (#213)
Graham Kinga6a4dcea7a98
diff --git a/crates/switchyard-server/src/lib.rs b/crates/switchyard-server/src/lib.rsindex eb0a8a898..e97de8f12 100644--- a/crates/switchyard-server/src/lib.rs+++ b/crates/switchyard-server/src/lib.rs@@ -300,6 +300,7 @@ pub fn build_switchyard_router(state: ServerState) -> Router {.route("/v1/messages/count_tokens", post(anthropic_count_tokens)).route("/v1/models", get(models)).route("/v1/stats", get(get_stats))+ .route("/v1/stats/reset", post(reset_stats)).route("/metrics", get(prometheus_metrics)).route("/health", get(health)).fallback(not_found)@@ -766,6 +767,11 @@ async fn get_stats(State(state): State<ServerState>) -> Json<StatsSnapshot> {Json(state.stats.snapshot())}+async fn reset_stats(State(state): State<ServerState>) -> Json<Value> {+ state.stats.reset();+ Json(json!({"status": "reset"}))+}+async fn health() -> Json<Value> {Json(json!({"status": "ok"}))}diff --git a/crates/switchyard-server/src/stats/accumulator.rs b/crates/switchyard-server/src/stats/accumulator.rsindex 2aa924ec7..28e3f80e6 100644--- a/crates/switchyard-server/src/stats/accumulator.rs+++ b/crates/switchyard-server/src/stats/accumulator.rs@@ -134,6 +134,11 @@ impl StatsAccumulator {inner.snapshot()}+ /// Clears all accumulated stats.+ pub(crate) fn reset(&self) {+ *self.lock() = StatsAccumulatorInner::default();+ }+fn lock(&self) -> MutexGuard<'_, StatsAccumulatorInner> {self.inner.lock()}@@ -562,6 +567,24 @@ mod tests {);}+ #[test]+ fn reset_clears_backend_classifier_and_cache_eligibility_state() {+ let stats = StatsAccumulator::default();+ stats.record_success("model/a", 10.0, Some("strong"));+ stats.record_usage("model/a", usage(10, 5), 15.0, Some("strong"));+ stats.record_routing_overhead(5.0);+ stats.record_classifier_success("model/classifier", Some(usage(4, 1)), 2.0);+ let probe = prefix_probe(&json!({+ "messages": [{"role": "user", "content": "repeat me"}],+ }));+ stats.prefix_eligibility("model/a", &probe);++ stats.reset();++ assert_eq!(stats.snapshot(), StatsSnapshot::default());+ assert_eq!(stats.prefix_eligibility("model/a", &probe), 0.0);+ }+#[test]fn theoretical_cache_hit_rate_is_switch_aware() {let stats = StatsAccumulator::default();diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex 0581ee153..69ed372ca 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -296,6 +296,40 @@ async fn stats_accumulates_buffered_success_error_and_shared_routes() -> TestResOk(())}+#[tokio::test]+async fn stats_reset_returns_confirmation_and_clears_all_stats() -> TestResult {+ let (_upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;+ assert_eq!(+ send(+ &app,+ "POST",+ "/v1/chat/completions",+ Some(json!({+ "model": ROUTE_MODEL,+ "messages": [{"role": "user", "content": "hello"}]+ })),+ )+ .await?+ .status,+ StatusCode::OK+ );++ let reset = send(&app, "POST", "/v1/stats/reset", None).await?;+ assert_eq!(reset.status, StatusCode::OK);+ assert_eq!(reset.json()?, json!({"status": "reset"}));++ let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;+ assert_eq!(stats["total_requests"], 0);+ assert_eq!(stats["total_errors"], 0);+ assert_eq!(stats["total_tokens"], empty_token_totals());+ assert_eq!(stats["models"], json!({}));+ assert_eq!(stats["tiers"], json!({}));+ assert_eq!(stats["routing_overhead"]["count"], 0);+ assert_eq!(stats["classifier"]["total_requests"], 0);+ assert_eq!(stats["classifier"]["models"], json!({}));+ Ok(())+}+#[tokio::test]async fn metrics_exposes_switchyard_otel_instruments() -> TestResult {const MODEL: &str = "model/metrics-buffered";