Skip to content

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

fix(server): report stage-router stats for a composite route (#943)

Dmitry Voropaev1e0996978850
diff --git a/crates/switchyard-server/src/stats/algorithms.rs b/crates/switchyard-server/src/stats/algorithms.rs
index f7f0d49ca..9849f8226 100644
--- a/crates/switchyard-server/src/stats/algorithms.rs
+++ b/crates/switchyard-server/src/stats/algorithms.rs
@@ -16,6 +16,10 @@ use stage_router::{StageRouterCumulative, StageRouterStatsSnapshot};
const ADVISOR_GATE: &str = "advisor_gate";
const STAGE_ROUTER: &str = "stage_router";
+// A composite route is a stage route under another name: CompositeRouter is
+// build_stage_route(..).with_name("composite") and its StageClassifier records
+// the same switchyard.stage_router.* instruments.
+const COMPOSITE: &str = "composite";
/// Owns algorithm metric baselines behind the generic server stats interface.
pub(super) struct AlgorithmStats {
@@ -47,9 +51,9 @@ impl AlgorithmStats {
advisor_gate_baseline: algorithms
.contains(ADVISOR_GATE)
.then(|| AdvisorGateCumulative::collect(&families)),
- stage_router_baseline: algorithms
- .contains(STAGE_ROUTER)
- .then(|| StageRouterCumulative::collect(&families)),
+ stage_router_baseline: (algorithms.contains(STAGE_ROUTER)
+ || algorithms.contains(COMPOSITE))
+ .then(|| StageRouterCumulative::collect(&families)),
registry,
}
}
diff --git a/crates/switchyard-server/src/stats/algorithms/stage_router.rs b/crates/switchyard-server/src/stats/algorithms/stage_router.rs
index 4f5b9e70c..5b1c020d8 100644
--- a/crates/switchyard-server/src/stats/algorithms/stage_router.rs
+++ b/crates/switchyard-server/src/stats/algorithms/stage_router.rs
@@ -279,4 +279,37 @@ mod tests {
assert_eq!(after_reset.routing_decisions["override"].total, 1);
assert!(!after_reset.routing_decisions.contains_key("dimensions"));
}
+
+ #[test]
+ fn a_composite_route_reports_its_stage_router_stats() {
+ // CompositeRouter is build_stage_route(..).with_name("composite"), and its
+ // StageClassifier records the same instruments, so the block has to appear
+ // for a deployment whose only route calls itself composite.
+ let registry = Registry::new();
+ let exporter = opentelemetry_prometheus::exporter()
+ .with_registry(registry.clone())
+ .build()
+ .unwrap_or_else(|error| panic!("failed to build metrics exporter: {error}"));
+ let provider = SdkMeterProvider::builder().with_reader(exporter).build();
+ let meter = provider.meter("switchyard");
+ let stats = StatsAccumulator::new(registry, ["composite"]);
+
+ meter
+ .u64_counter("switchyard.stage_router.routing_decisions")
+ .build()
+ .add(
+ 3,
+ &[
+ KeyValue::new("decision_source", "llm-classifier"),
+ KeyValue::new("target_name", "model/capable"),
+ ],
+ );
+
+ let snapshot = stats.snapshot();
+ let stage = snapshot
+ .algorithm_stats
+ .stage_router
+ .unwrap_or_else(|| panic!("stage-router stats missing for a composite route"));
+ assert_eq!(stage.routing_decisions["llm-classifier"].total, 3);
+ }
}