feat(switchyard-server): Add /v1/routing/session-stats - #225
Conversation
|
@CodeRabbit review |
✅ Action performedReview finished.
|
WalkthroughThe server adds optional JSONL routing logs, records terminal usage for aggregate and streaming responses, and exposes session-level statistics through a conditional endpoint. ChangesSession routing log
Estimated code review effort: 3 (Moderate) | ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/switchyard-server/src/lib.rs`:
- Around line 110-127: The routing-log implementation must avoid blocking async
request workers: update SharedRoutingLog::append to enqueue records through a
bounded dedicated writer rather than locking the file and performing synchronous
write I/O, and update get_session_stats/snapshot_session to use bounded blocking
work while maintaining a per-session aggregate or index instead of rescanning
the JSONL file to EOF for every query. Apply the corresponding changes at
crates/switchyard-server/src/lib.rs lines 110-127 and 864-873.
In `@crates/switchyard-server/src/routing_log.rs`:
- Around line 27-113: Document the routing-log API and lifecycle behavior across
all affected sites: in crates/switchyard-server/src/routing_log.rs lines 27-113,
add /// documentation for RoutingLog::new, RoutingLog::append, snapshot, and
RoutingLogContext::from_headers; in lines 152-219, document the JSON validation
and aggregation performed by SessionStatsSnapshot::add_record and add_counter.
In crates/switchyard-server/src/lib.rs lines 96-128, document that
SharedRoutingLog serializes writers but does not synchronize snapshots. In
crates/switchyard-server/src/usage_metrics.rs lines 17-79, document observe and
add a block comment explaining clean-stream terminal recording and failure
behavior.
- Around line 28-40: Update ServerResult::new to inspect the existing log’s
final byte before opening it for appending, and write a newline when the file is
non-empty and does not already end with '\n', while preserving existing
newline-terminated logs. Add a restart-recovery test covering a partial final
JSON record followed by a new record and verify snapshot() retains both records.
- Around line 36-40: Update the file-opening logic in the routing-log
initialization flow around OpenOptions to enforce owner-only permissions (0600)
for both newly created and existing files before writing records. Use the
platform-appropriate permission-setting API and ensure the permission update
occurs before the file is used, while preserving the existing routing_log_error
handling.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: c92e9501-fd73-45eb-b885-1e21ab4c4d02
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock,!Cargo.lock
📒 Files selected for processing (7)
crates/switchyard-server/Cargo.tomlcrates/switchyard-server/README.mdcrates/switchyard-server/src/cli.rscrates/switchyard-server/src/lib.rscrates/switchyard-server/src/routing_log.rscrates/switchyard-server/src/usage_metrics.rscrates/switchyard-server/tests/server.rs
d6dcfe9 to
6fa0ceb
Compare
To match Python FastAPI server. - Pass flag `--routing-log-file file.jsonl` - Writes one line for each request to that file We now have _three_ ways of reporting basically the same numbers: Prometheus metrics, in-memory stats, and on-disk routing stats. Assisted-by: Codex:GPT 5.6 Sol medium Signed-off-by: Graham King <grahamk@nvidia.com>
6fa0ceb to
b7fd194
Compare
nachiketb-nvidia
left a comment
There was a problem hiding this comment.
LGTM, feature parity with python looks like its exactly what we need, for benchmarking and intake
To match Python FastAPI server.
--routing-log-file file.jsonlWe now have three ways of reporting basically the same numbers:
Prometheus metrics, in-memory stats, and on-disk routing stats.
Assisted-by: Codex:GPT 5.6 Sol medium
Signed-off-by: Graham King grahamk@nvidia.com
Summary by CodeRabbit
New Features
Documentation