Skip to content

feat(frontend): metrics and request-end traces for native SGLang generate - #16641

Closed
Aphoh wants to merge 5 commits into
mainfrom
warnold/sglang-generate-observability
Closed

Aphoh wants to merge 5 commits into
mainfrom
warnold/sglang-generate-observability

Conversation

@Aphoh

@Aphoh Aphoh commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Native SGLang /generate now records the same frontend observability as /v1/completions. Closes #16640.

Builds on #16504 (merged: workers send typed output tokens, finish reason and usage). It also includes #16489's commit (agent context from headers on native generate requests) with that PR's review feedback applied; that commit drops out of this diff once #16489 merges. Co-authored with @ShaneGZhu.

Frontend only, reusing existing code:

  • Metrics: /generate records dynamo_frontend_* TTFT, ITL, output tokens and length, ISL, cached tokens and the worker_last_* gauges, in both streaming and unary.
    • It uses the collector vLLM /inference/v1/generate already had, moved into a neutral engine_output_metrics.rs.
    • The request completed log line now carries token counts, TTFT, ITL and worker IDs.
  • Request-end tracing: /generate writes one DYN_REQUEST_TRACE record per request, including on cancellation.
    • Each record carries token counts, timing, worker IDs and replay hashes. With session headers it also carries agent context, x-request-id and the backend finish reason.
    • The new wrap_engine_output_request_end_stream observes typed LLMEngineOutput and reuses the existing request-end emit path. When tracing is off it returns the stream unchanged.
    • Tool-call metadata is not recorded on /generate.
  • fix(frontend): carry agent context on native generate requests #16489 review feedback: agent headers are parsed once, and the context's parsed value is shared with the request (get_optional, so no allocation when the headers are absent).
  • Small changes:
    • The trace builders take any &Context<T>, so the request is no longer moved out of its context and back.
    • The metric model is looked up once per request.
    • The /generate rejection counter now uses the metric model label, as vLLM does.
    • sglang_generate.rs tests move to sglang_generate_tests.rs.
  • Docs: /generate coverage is noted on the request-trace and agent-tracing pages.

Validation

  • cargo clippy -p dynamo-llm --lib --tests -- -D warnings
  • cargo test -p dynamo-llm --lib -- sglang request_trace generate preprocessed_context
  • cargo test -p dynamo-llm --lib --no-default-features request_trace
  • pre-commit on the changed files

GPU (B200), aggregated serving. Stock SGLang 0.5.21 in the container, Qwen/Qwen3-0.6B; the sidecar and this branch's frontend run on the host.

/generate request Frontend metrics Request-end trace
stream, 32 tokens TTFT 1, ITL 31, output 32, ISL 141, cached 128, success 1 1 record: in 141, out 32, cached 128, timing, worker IDs, replay length 141
unary, 32 tokens same as stream same as stream
with session headers and x-request-id same as stream same, plus agent_context (session and parent), x_request_id, finish length
client cancels after 8 tokens output 8, ITL 7, counted as cancelled 1 record with output 8

Client responses are unchanged (text, output_ids, meta_info).

Generated with Devin

@github-actions github-actions Bot added feat documentation Improvements or additions to documentation frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` labels Oct 16, 2026
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

@devin-ai-integration

devin-ai-integration Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

✅ Dynamo PR CI passed — run 38887255376 (attempt 1) on 0d37273371

Gate checks: ✅ backend-status-check · ✅ deploy-status-check · ✅ dynamo-status-check

Posted automatically by Devin for run 38887255376. Updated on every full-CI run of this PR.

Aphoh and others added 5 commits October 16, 2026 08:16
Native SGLang `/generate` only kept an inflight guard, so it recorded no
TTFT, ITL, token, cached-token, or worker metrics, its "request completed"
log carried no token or latency fields, and DYN_REQUEST_TRACE emitted no
request_end rows for it.

Reuse the vLLM `/inference/v1/generate` metric collector and dispatch span
for both the unary and streaming paths. Build request-end trace state from
the same WorkerSet selection that provides the KV block size, and wrap the
raw engine stream with a generic `LLMEngineOutput` observer that records the
backend finish reason and token usage before reusing the shared request-end
emitter. The observer returns the stream unchanged when tracing is off.
Tool calls are not parsed on this route, so its rows carry no tool-call
metadata.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Co-authored-by: ShaneGZhu <1092841848@qq.com>
Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Co-authored-by: ShaneGZhu <1092841848@qq.com>
…model clone

Reuse the context's parsed agent context for the request (its headers are
Arc-backed) instead of parsing the headers twice, as reviewed on #16489,
and keep dispatch's model by move with the request id borrowed from the
engine context.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Co-authored-by: ShaneGZhu <1092841848@qq.com>
- Read the agent context with get_optional to avoid an error-string
  allocation on the common missing-key path.
- Make request-end trace builders generic over Context<T> and drop the
  into_parts/rejoin dance in the SGLang handler and tests.
- Resolve the metric model once in the handler and reuse it for
  cancellation labels, the dispatch span, the inflight guard, the
  collector, and rejection counts.
- Move GenerateMetricCollector and generate_dispatch_span into a neutral
  engine_output_metrics module shared by both generate routes.
- Engine-output request-end tracing leaves ISL to request construction,
  ignores cached tokens from migrated-attempt usage, and keeps the
  backend output index unchanged.
- Move SGLang generate tests to sglang_generate_tests.rs and trim them
  to SGLang-specific wiring.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: ShaneGZhu <1092841848@qq.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
…ervability

Add LLMEngineOutput::cached_tokens_for_prompt and use it for both the
native generate frontend metrics and request-trace token recording,
simplify the engine-output request-end wrapper, drop the unused
get_generate_engine_for_capability helpers, clarify that native
/generate trace rows carry the backend finish reason only with session
headers, and tidy test helpers.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: ShaneGZhu <1092841848@qq.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
@Aphoh
Aphoh force-pushed the warnold/sglang-generate-observability branch from d09bcc6 to 0d37273 Compare October 16, 2026 10:26
@Aphoh

Aphoh commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Closing: #16983 unifies the native generate handlers and takes over the frontend metrics part. The request-end tracing part will be redone as a small follow-up on the unified handler, owned separately.

@Aphoh Aphoh closed this Oct 21, 2026
Aphoh added a commit that referenced this pull request Oct 22, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 22, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 22, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 22, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 22, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 23, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 26, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 26, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 28, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 29, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Oct 30, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Nov 1, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Nov 1, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Aphoh added a commit that referenced this pull request Nov 1, 2026
Adapt request-end tracing from #16641 at the shared generate handler. Preserve cancellation and prefill phase semantics, and exclude opaque multimodal payloads from replay.

Part of #16640

Co-authored-by: ShaneGZhu <1092841848@qq.com>

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: gongshaotian <gstian5555@outlook.com>
Signed-off-by: William Arnold <7565007+Aphoh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation feat frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: frontend metrics and request-end traces for native SGLang /generate

1 participant