Repository navigation
feat(telemetry): request, stage, and delivery metrics with a local Collector (#340) - #375
Conversation
899ed58 to
1e90af0
Compare
### Summary Fixes #377, taking option 1 from the issue: raise the declared MSRV to what the locked dependencies need. The documented MSRV was 1.85, but nothing declared or checked it, and the committed `Cargo.lock` needs more: - The default build needs 1.88: `idna_adapter` 1.2.2 needs 1.86, `process-wrap` 9 (for `rmcp`'s stdio MCP transport) needs 1.87, and `home` 0.5.12 and `icu_*` 2.3 need 1.88. - The `embedded-code-interpreter` feature (#368) needs 1.98.1: `eryx` 0.8.0 declares it, and wasmtime and cranelift 48 need 1.95. `rust-toolchain.toml` is already pinned to 1.98.1 for that feature. Cargo expects `rust-version` to cover every feature, so the MSRV is 1.98.1: - `rust-version = "1.98.1"` in `[workspace.package]`, inherited by every crate, so older toolchains fail with a clear message. `clippy.toml`'s `msrv` matches it. - A new `msrv` CI job reads `rust-version` and fails if `clippy.toml` differs. It prepares the Eryx runtime, because the `eryx` build script needs it, then runs `cargo check --workspace --all-targets --all-features --locked` on that toolchain. - Every workflow toolchain pin is now 1.98.1. Jobs pinned to 1.98.0 had been running 1.98.1 only because `rust-toolchain.toml` overrode them. - The container now builds on cargo-chef 0.1.78 with Rust 1.98.1. It had been building with 1.96.0, because the image doesn't copy `rust-toolchain.toml`, so the new MSRV would have rejected it. cargo-chef publishes Rust 1.98 images only from 0.1.78. - The clippy suggestions enabled by the new MSRV are applied in a separate commit: `if let` chains, `is_none_or`, `as_chunks`, `Duration::from_mins` and `is_multiple_of`. No behavior changes. - The MSRV is updated in `AGENTS.md`, the README badge, the getting-started and container docs, and the embedded code interpreter design doc. That doc now says the toolchain pin and the MSRV are both 1.98.1. The provider-aware reasoning design doc no longer says an MSRV repair is pending. - The observability docs, `OtlpProtocol` and the unsupported-protocol startup error no longer blame the MSRV for the missing gRPC exporter. The error used to say "gRPC is unavailable below Rust 1.88"; it now says the exporter is not compiled in, matching the zstd compression error. - The website's copy of the observability page is a release snapshot that `refresh-site-docs.mjs` regenerates, so it is left for the next refresh. ### Test Plan ```bash # With the Eryx runtime prepared (./scripts/setup-eryx-runtime.sh): cargo +1.98.1 check --workspace --all-targets --all-features --locked # passes cargo +1.98.0 check --workspace --locked # refused: "agentic-* requires rustc 1.98.1" cargo clippy --workspace --all-targets --all-features --locked -- -D warnings # clean with msrv = "1.98.1" cargo clippy --workspace --all-targets --locked -- -D warnings # clean (default features) cargo fmt -- --check cargo test --workspace --locked # 2281 passed, 1 failed, 18 ignored (see below) uvx pre-commit run --from-ref upstream/main --to-ref HEAD # includes the file-size ratchet uvx --with-requirements docs/requirements.txt mkdocs build --strict ``` - Without the Eryx runtime, the all-features check stops in `eryx`'s build script ("Pre-compiled runtime (runtime.cwasm) not found"). The `msrv` job prepares the runtime the same way the `embedded-code-interpreter` job does, and shares its cache key. - The one test failure is `web_fetch::http::tests::the_deadline_bounds_a_slow_origin`. It fails the same way on unmodified `main` under the full suite and passes on its own; this PR doesn't touch `web_fetch`. - I dry-ran the job's version-reading step locally: both files read `1.98.1`. - Container: on the old base (cargo-chef 0.1.77, Rust 1.96.0), `cargo chef cook` is refused with "rustc 1.96.0 is not supported … requires rustc 1.98.1". On the new base, the Rust build stage completes, and both binaries run (`agentic-server 0.9.0`). My local Docker has no BuildKit, so CI's image build covers the later `COPY --chmod` step. - Default features alone would still build on 1.88. On `main` at 2026-09-30, `cargo +1.88.0 check --workspace --all-targets --locked` passed, and on today's `main` every locked dependency outside `eryx`'s tree still declares at most 1.88. The MSRV follows Cargo's expectation that `rust-version` covers every feature. - Rebased onto `main` `8b27941`. The clippy autofix commit was redone against current `main`. #427 removed the `provider-upstream` job, so that job's toolchain pin is gone too. ### Merging alongside #375 #375 (#340) also edits `.github/workflows/rust.yml`; whichever lands second keeps both changes. Under `msrv = "1.98.1"`, #375 must also pass clippy, which may need the same mechanical autofixes. --------- Signed-off-by: Zheng Lu <[email protected]>
Phase 3 of vllm-project#279 (vllm-project#340). The ExecutionSpan guard now records the execution metrics from Drop, on the same paths that finalize the agentic.execute span, so every execution is counted exactly once and metrics do not depend on trace sampling. - ExecutorMetrics, on ExecutionContext (with_metrics for embedders), owns every instrument: execution count, duration and active executions; delivery outcome and wait; stage durations; inference rounds per execution; and per-call token usage. - ExecutionClock records first upstream data, first client event, and first output text. The pipeline marks upstream data; the client-facing stream marks client events and measures delivery wait through InstrumentedStream. - Stage timers wrap rehydration, each inference round, each executed gateway call, compaction summaries, and persistence, including explicit compaction persistence. - Retained (WebSocket) responses take the same timings in their event stream. A multi-agent response records one rounds sample covering every agent, and each round's pipeline shares the execution clock. - The Messages loop and stream record rounds, inference stages, and per-round token usage. - The server records WebSocket connections and queue wait. Signed-off-by: Zheng Lu <[email protected]>
Cover every terminal path on both APIs: completion, upstream failure with and without a stream, a dropped stream, WebSocket requests, disconnects and drains, and a gateway tool round. Each execution is counted once, the first-* timings are ordered, token usage comes from each upstream call, and every exported metric stays within the cardinality allow-list. Also cover the paths added since: a multi-agent response records one rounds sample counting every agent's rounds, and retained WebSocket responses record the first-* timings. The mock upstreams give each response fresh item IDs, as a real upstream does: stored history keys rows by item ID (vllm-project#385). Signed-off-by: Zheng Lu <[email protected]>
deploy/otel runs an OpenTelemetry Collector (and optional Jaeger) that exports metrics to Prometheus and logs every signal. The smoke test starts the gateway against a scripted upstream, sends streamed, blocking, proxied, and WebSocket requests, and checks the Collector's output against the same allow-list as the Rust tests. CI runs it after the CLI end-to-end test. scripts/otel-overhead.py measures the telemetry overhead. Signed-off-by: Zheng Lu <[email protected]>
Describe every instrument, its attributes and boundaries, when each is recorded, the local Collector setup, and the measured overhead. Note that a multi-agent response's rounds sample counts every agent, and that a WebSocket response's delivery wait ends when it finalizes. Signed-off-by: Zheng Lu <[email protected]>
1e90af0 to
e6dff9b
Compare
| let events = ResponseEventSink::stream(receiver); | ||
| let events = Box::pin(InstrumentedStream::delivering( | ||
| ResponseEventSink::stream(receiver, Some(clock.clone())), | ||
| events_span, |
There was a problem hiding this comment.
WebSocket agentic.execute spans export only after the socket closes
An operator with tracing on sees no agentic.execute span for a WebSocket response until the client disconnects, or until a later request on the same socket evicts the route at least 60 s later. Long-lived Codex sessions are the common case. retained::start now wraps ResponseEventSink::stream in InstrumentedStream::delivering(.., events_span, ..). That stream only ends when every sink clone drops, and Controls keeps one in the route after completion, so the span handle lives in the relay task. Evidence: a probe test receives response.completed, keeps the socket open and polls the in-memory exporter. Head e6dff9b: no span after 3 s. Main 5473e02 exported after 34 µs. The existing trace test closes the socket before it looks, so it can't see this. Metrics are unaffected, because the guard drops in the worker task.
Suggested fix
- Add a constructor that times delivery without a span (or pass
Span::none()). The retained orchestration already runs inside the span on its own task, so the relay doesn't need to enter it. - Use it in
retained::startand dropevents_span. Keepdelivering(span, clock)for HTTP, where the stream owns orchestration. - Reword the docs' "finalized when its span closes" to "when its guard drops", since the two can differ.
// sketch, not a tested patch: executor/telemetry.rs
pub(crate) fn timing_delivery(inner: S, clock: ExecutionClock) -> Self {
Self { inner, span: Span::none(), delivery: Some(DeliveryTimer { clock, handed_at: None }) }
}
// executor/engine/retained.rs
let events = Box::pin(InstrumentedStream::timing_delivery(
ResponseEventSink::stream(receiver, Some(clock.clone())),
clock,
));…nalizes retained::start wrapped the response's event stream in InstrumentedStream::delivering with the execution span. That stream ends only when the connection releases the response's sink, which the route keeps after completion. So the agentic.execute span stayed open, and unexported, until the socket closed or a later request evicted the route. Time retained delivery with InstrumentedStream::timing_delivery, which holds no span; the retained execution already runs inside its span on its own task. HTTP streams keep `delivering`, since there the stream owns orchestration. The docs now say an execution is finalized when its guard drops. Add a trace test that keeps the socket open after response.completed; it fails on the previous head. Signed-off-by: Zheng Lu <[email protected]>
haoshan98
left a comment
There was a problem hiding this comment.
Thanks for the fix, lgtm.
## Summary Closes #449, the follow-up to departure 4 in #375. Gateway-executed built-in tools in the Messages loops now record the same telemetry as the Responses executor: - **Span.** Each call that runs opens an `agentic.tool.execute` span under `agentic.execute`, between the two inference rounds, with `agentic.tool.type` as its only attribute. - **Stage sample.** Each call that runs records one `tool` sample in `agentic.stage.duration`. - **Type source.** The type comes from the registry entry that serves the call (`registry.tool_type(&call.name)`), never from the model-supplied name. An operator alias such as Claude Code's `WebSearch` is therefore recorded as `web_search`. - **Failures.** A call that reports a failure, returns an error, has no handler, or times out is a failed stage, `error.type=tool`. A client disconnect while a call runs records `cancelled`, because the stage timer's drop handles it. - **Refused calls.** A call refused at admission (spent `max_uses` budget, or undispatchable arguments) records neither a span nor a sample. - **Plumbing.** `messages_tools::run` now delegates the dispatch to `dispatch`, which carries `#[tracing::instrument]`. Both loops pass `&exec_ctx.metrics` into `execute_gateway_calls`. - **`web_fetch` value.** `stages::tool_type` already mapped `WebFetch` to `web_fetch`, but nothing emitted that value until now. It is added to the documented values and to both test allowlists, `execution_metrics/harness.rs` and `execution_traces/attributes.rs`. The new `web_fetch` test failed on the metrics allowlist before this addition. - **Docs.** `docs/deploying/observability.md` drops "not timed yet" and says which stages cover the Messages executor. ### Overlap with #437 #437 rewrites the same `run`. Both PRs append a trailing parameter to `execute_gateway_calls` and its callers, so a trial merge conflicts mechanically in 4 files: - `messages_tools.rs` - `messages_loop.rs` - `messages_stream.rs` - `messages_stream/blocks.rs` The resolution keeps both parameters. #437's binding lookup, permit wait and `binding.execute` move into `dispatch`, so the span and timer also cover the permit wait. Responses' `run_one` already behaves that way. Whichever PR lands second rebases; I'm happy to do it. ## Test Plan New tests: - `execution_trace/messages_tools.rs`: `messages_{loop,stream}_gateway_tool_is_a_typed_stage_between_rounds`. - Native `web_search` under `max_uses: 1`, with two calls. - Checks for exactly one `agentic.tool.execute` span with type `web_search`, parented to `agentic.execute` and placed between round 0 and round 1. - The stream is drained from an unrelated task. - The model's queries never appear on a span. - `execution_metrics_test.rs`, each in both stream modes unless noted: - `messages_gateway_tool_times_only_the_admitted_call`: one `tool` sample of type `web_search`, no failure. - `messages_gateway_tool_failure_is_a_failed_tool_stage`: the search backend returns 500, giving `error.type=tool`, and the execution still completes. - `messages_aliased_gateway_tool_is_typed_by_the_executor_that_serves_it`: `WebSearch` is recorded as `web_search`. - `messages_web_fetch_is_a_web_fetch_tool_stage` (JSON mode only). - `messages_stream_dropped_while_a_gateway_tool_runs_cancels_its_stage` (stream mode only): the search hangs, then the client is aborted, giving `error.type=cancelled`. Results: - **RED on `634899a`.** The trace tests fail because `agentic.tool.execute` never closes, although both rounds ran. The metrics tests see 0 `tool` samples. - **GREEN with this change.** - **Negative controls.** Each regression below was applied to the fix and was caught: | Regression | Caught by | | --- | --- | | Type taken from the model-supplied name | Alias test | | Refused call timed | Admitted-call test and failure test | | Refused call traced | Both trace tests | | Failure not labelled | Failure test | | Timer started after the dispatch | Cancellation test | Gate: - `cargo fmt -- --check` - `cargo clippy --workspace --all-targets -- -D warnings` - Feature clippy (`--features embedded-code-interpreter`) for `agentic-server-core` and `agentic-server` - Rust file-size ratchet - `pre-commit run --all-files` - `mkdocs build --strict` - `cargo test --workspace --no-fail-fast`: 2348 passed, 1 failed. The failure is `tool::web_fetch::http::tests::the_deadline_bounds_a_slow_origin`. It also fails on unmodified `634899a` on my machine when the whole lib suite runs in parallel: 1.24 s against its 1 s bound, while its origin sleeps 2 s. It passes 5 out of 5 runs in isolation. It's a load-sensitive wall-clock bound in code this PR doesn't touch. --------- Signed-off-by: Zheng Lu <[email protected]>
Summary
Phase 3 of #279. Closes #340.
Rebuilt on
mainat5473e02(2026-10-09), then rebased on 2026-10-12 onto079a9a3(#432, #379).mainreorganized most of the code this PR instruments: #384, #386, #407, #410 and #423 changed the inference loop, multi-agent execution, the WebSocket path and client delivery. So each hook was re-applied at its current owner rather than merged textually, and the paths that did not exist before are now covered too. See "Changes in the rebuild" below.The rebase onto
079a9a3needed two small changes. #432 wrapspersist_if_neededinBox::pinat the two call sites where this PR passes&exec_ctx.metrics; both are kept.engine/multi_agent_tests.rsgets a file-size exception (limit 501) for this PR's two-linemetricsmodule declaration, because #432 brought the file to 499 lines; the tests themselves live inmulti_agent_tests/metrics.rs.This adds operational metrics that stay correct without trace sampling, a backend-neutral local Collector setup, and measured overhead. All instruments are typed in
agentic-server-coreand are recorded by the sameExecutionSpanguard that already finalizesagentic.execute. They therefore share its exactly-once terminal paths: completion, in-band upstream failure, disconnect, and shutdown. They are also independent of the sampling decision.agentic.execution.count,agentic.execution.durationagentic.api,agentic.route,agentic.stream,agentic.execution.outcome,error.typeon failureagentic.execution.activeagentic.api,agentic.route,agentic.streamagentic.delivery.countagentic.api,agentic.route,agentic.stream,agentic.delivery.outcomeagentic.delivery.wait.durationagentic.apiagentic.stage.durationagentic.stage(rehydrate,inference,tool,compaction,persist),agentic.tool.typeon tool samples,error.typeagentic.inference.roundsagentic.apigen_ai.client.token.usagegen_ai.operation.name=chat,gen_ai.token.type,agentic.apiagentic.time_to_first_upstream_data,agentic.time_to_first_client_event,agentic.time_to_first_textagentic.apiagentic.websocket.connections.activeagentic.websocket.queue.wait.durationExecutorMetricslives onExecutionContext. It defaults to the global meter provider, and embedding applications pass their own withExecutionContext::with_metrics. Without a provider every instrument is a no-op.usage, including compaction summaries. Nothing is recorded when the upstream omitsusageor a count, so no zeros are fabricated. For Messages, prompt tokens include cache reads and writes.ExecutionClockmeasured from when the execution was accepted:agentic.inference.roundssample counting every agent's rounds.ResponseEventSink::streamreturns, and measure delivery wait there.deploy/otel/docker-compose.yamlruns digest-pinned collector-contrib 0.161.0 with an OTLP/HTTP receiver,memory_limiter,batch, thedebugexporter, and a Prometheus endpoint carryingservice.name.--env-file deploy/otel/jaeger.envadds Jaeger 2.21.0. All ports bind to 127.0.0.1.scripts/tests/otel-smoke.shsends streamed, blocking, proxied, and WebSocket requests through a scripted upstream. It stops the gateway so it flushes, then checks the Collector'sdebugoutput and Prometheus endpoint. TherustCI job now runs the smoke test and the checker's unit tests.docs/deploying/observability.mdcovers every instrument, attribute value, recording rule, timing anchor, histogram boundary, the cardinality contract, the local Collector, and the overhead table.ARCHITECTURE.mdrecords which pipeline stage owns each telemetry mark, so the refactor: unify synchronous streaming ingestion and item lifecycle #243/refactor: extract StreamRelay and bound client delivery #244 refactors move marks instead of duplicating them.Defaults restated from the #279 design table
These got no reply on #279, so this PR uses the proposed defaults:
agentic.*namespace for project-specific metric and attribute names.Design departures from the #279 plan
first_upstream_data ≤ first_client_event ≤ first_textfor a text response. It also measures what a client sees. Ingestion still owns the classification:StreamEventcarries a typedtextflag set fromSSEEventType::OutputTextDelta.InstrumentedStreammeasures the time between handing a frame to the transport and the transport polling for the next one. That covers HTTP and WebSocket at one boundary with one sample per execution. Boundaries therefore extend to 60 s. A WebSocket response finalizes on its own task, so frames the connection takes after that are not included.gen_ai.request.model,gen_ai.response.model, orgen_ai.provider.name. Per-request-model labels are a feat: request, stage, and delivery metrics with a local Collector example (#279 phase 3) #340 non-goal. The gateway cannot know which provider serves its upstream, so it records no provider name rather than a guessed one. This deviates from the semantic convention's required provider attribute, and the docs say so.toolstage covers Responses gateway calls, one sample per tool call.agentic.execution.activeincludes requests queued behind another on the same stream, and duration includes queue wait.agentic.websocket.queue.wait.durationisolates the wait.Changes in the rebuild
AgentTurn::run_round, which both single-agent and multi-agent rounds use. The file moves the original PR made for size (engine/round.rs,gateway/input.rs,compaction/explicit.rs) are gone.SingleAgentRunandMultiAgentRun, one per execution.MultiAgentRunpasses the execution clock to every round's pipeline.retained.rsgives the root pipeline the clock and wraps its event stream for delivery timing.persist_if_neededtakes the metrics on all four call sites, including the retained one.GatewayScheduler::run_one, the same scope as itsagentic.tool.executespan. Calls refused bymax_tool_callsnever run, so they are not timed. The two timing methods live ingateway/timing.rsto keepgateway.rsunder the size limit.execute_and_emit_output_callstakes&ExecutionContextinstead of a policy and the metrics (clippy::too_many_arguments).StreamFramecarries the typedtextflag on both frame paths (HTTP and retained).messages_stream.rs:open_roundreplacesprime_messages_streamand the later-round branch, as before.MessagesStreamAccumulatorgains a fourth flag, so it takes#[allow(clippy::struct_excessive_bools)]like the other such structs in the repository.RequestPayloadtwice (clippy::large_futures).engine/multi_agent_tests.rs(see the top).ExecutionSpan::startandstart_linkedtake&ExecutorMetrics.Test Plan
web_fetch::http::tests::the_deadline_bounds_a_slow_originis timing-sensitive and fails intermittently on unmodifiedmainunder the full suite; after the rebase it failed in one of two full-suite runs and passes on its own. This PR doesn't touchweb_fetch. Clippy also passes with--features embedded-code-interpreterfor both crates, as in CI.New tests. Every test builds its own in-memory meter provider, so they run in parallel. Every assertion passes through a shared allow-list,
crates/agentic-server-core/tests/execution_metrics/harness.rs, which fails on any instrument, attribute key, or attribute value that isn't listed.agentic-server-core/tests/execution_metrics_test.rs(11 tests, no tracing subscriber installed):text_only/responses/resp-single-gpt-4o-streaming.yaml):first_upstream_data ≤ first_client_event ≤ first_text;usage: null.web_searchtool stage, and one usage sample per upstream call./v1/responses/compacttimes rehydrate, compaction, and persist, and records usage, but no execution.agentic-server/tests/execution_metrics_test.rs(4 tests, production router):response.createplus one failure on one WebSocket gives 4 executions, 4 queue-wait samples, andconnections.active= 1 while open, 0 after close.agentic-server/tests/telemetry_metrics_e2e_test.rsruns the real binary, configured only throughOTEL_*, withOTEL_TRACES_SAMPLER=parentbased_traceidratioandOTEL_TRACES_SAMPLER_ARG=0, exporting to an in-process OTLP stub. It sends HTTP and WebSocket requests, then SIGTERMs the gateway with one HTTP stream and one WebSocket request still in flight:service.name;connections.activeback to 0.multi_agent_execution_records_rounds_and_first_data_once(core unit test). A streamed multi-agent response, a root and two children, records one rounds sample. Its sum equals the number of inference stages across all agents, and first upstream data is recorded once.store: truefails with 500 after #370 #385).scripts/tests/test_otel_fixtures.py(10 tests): the Collector-log checker reads the allow-list from the Rust harness and rejects unlisted keys, values, and instruments.Mutation checks. For the code the rebuild re-placed, each new check fails when its wiring is removed:
The original branch (
9a9b052) had these checks. The tests behind them pass unchanged on the rebuild, but I did not repeat the mutations:ExecutionSpandrop never records execution metricsunwrap_or_default)usage: null; nothing is fabricated"connections.activeOTEL_TRACES_SAMPLER_ARG=1Local Collector.
scripts/tests/otel-smoke.shpasses against the compose stack after the rebase, on five cold Collector starts and a warm rerun: all 15 instruments and 29 data points are allow-listed, with the run'sservice.name. The very first run, which also created the Docker network, reported no metrics even though the Collector logged them a moment later; this did not reproduce. I did not rerun the Jaeger variant after the rebuild. This machine only hasdocker-composev1, so the script falls back to it locally. CI runsdocker composev2.Overhead
Measured on the 2026-10-09 rebuild and not re-measured after the rebase, which changed no instrumented code.
scripts/otel-overhead.py, release builds. Setup:parentbased_always_on) and exports traces and metrics over OTLP/HTTP to the local Collector.Measured again after the rebuild, two runs per build in the same session (second run shown). This branch:
mainat5473e02(traces, no metrics), same benchmark:main, this branch measured 0.01–0.13 ms higher at p50 and 0.01–0.14 ms more CPU per request, with telemetry enabled or disabled.Known gaps and follow-ups
msrv = "1.98.1".