Skip to content

feat(mux): populate client_rtt and server_rtt on the H2 access log - #1242

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
feat/mux-access-log-rtt
May 4, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
feat/mux-access-log-rtt

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented May 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The HTTP/2 mux access log emitted client_rtt: None, server_rtt: None because the mux Stream type owns no socket reference: the frontend socket lives on the parent Mux/connection and the backend socket lives on Router.backends.get(token) keyed off StreamState::Linked(token). The H1 (kawa_h1), Pipe (TCP/WS) and TCP-frontend access logs already populate both fields, so the H2 path was the only asymmetric outlier under the "Improve metrics and access logs" umbrella.

This PR snapshots both RTTs at every access-log emission boundary and threads them as parameters into Stream::generate_access_log, matching the inline pattern already used by kawa_h1, pipe and the TCP frontend. Schema unchanged — ProtobufAccessLog tags 9 and 10 were already declared optional. TCP_INFO via socket::stats::socket_rtt ships real implementations on Linux, FreeBSD, NetBSD, OpenBSD, DragonFly, macOS and iOS; non-Unix stubs return None.

Refs #937.

Implementation

  • Stream::generate_access_log gains client_rtt: Option<Duration> and server_rtt: Option<Duration> parameters; the previous None placeholders in mux/stream.rs are replaced.
  • New Stream::linked_token() -> Option<Token> accessor so each capture site collapses to a single linked_token().and_then(|t| endpoint.socket(t)).and_then(socket_rtt) chain.
  • The Endpoint trait gains a small socket(token) -> Option<&TcpStream> method. EndpointServer returns the frontend socket (token ignored). EndpointClient looks up the backend by token through the existing Router.backends map. Both impls are one line.
  • New ConnectionH2::snapshot_rtts(position, socket, endpoint, linked_token) associated function consolidates the five Position::Server-gated H2 capture blocks into one place. Takes individual field references (not &self) so it composes with H2BlockConverter holding &mut self.encoder during the per-stream write loop, mirroring the shape of the existing try_recycle_server_stream.
  • try_recycle_server_stream and complete_server_stream gain matching client_rtt/server_rtt parameters that are forwarded to Stream::generate_access_log.

Sites covered

Eight access-log emission paths plus the Mux session-close path:

  • lib/src/protocol/mux/mod.rs — Mux session-close per-stream finalisation. Frontend RTT is hoisted above the per-stream loop because the frontend socket is the same for every stream — one getsockopt(TCP_INFO) syscall per session close instead of N.
  • lib/src/protocol/mux/h1.rs — H1::Upgrade, H1::EarlyHint, H1::Complete, all inside the pre-existing Position::Server arm. H1::EarlyHint uses the standard linked_token().and_then(...) chain; the else arm of the outer if let Linked already guarantees linked_token() == None, so server_rtt resolves to None naturally.
  • lib/src/protocol/mux/h2.rs — H2::Complete via complete_server_stream (both try_recycle_server_stream callers — the expect_write flush path and the per-stream write loop). The snapshot is gated to Position::Server at the call site so backend H2 connections cannot capture upstream RTT under client_rtt and frontend RTT under server_rtt (try_recycle_server_stream returns None for Position::Client, but the shared Stream.metrics would otherwise have been written with swapped values that the matching frontend's later snapshot could fail to overwrite).
  • lib/src/protocol/mux/h2.rs — H2::IdleTimeout, H2::ResetFrame, H2::Reset. The snapshot is hoisted ABOVE endpoint.end_stream(token, ...) so the server_rtt lookup does not depend on EndpointClient::end_stream continuing to leave entries in Router.backends. Order is monotonic ("snapshot, then tear down") and matches the kawa_h1/pipe pattern.

Test plan

  • cargo build --all-features --locked — clean
  • cargo +nightly fmt --all -- --check — clean
  • cargo clippy --all-targets --all-features --locked — no findings
  • cargo test -p sozu-lib --all-features --lib — 518 passed
  • cargo test -p sozu-e2e h2_basic — 4 passed (H2 happy-path call sites exercised on the new code paths)

Documentation

  • CHANGELOG.md — bullet under ### 🌟 Added linked to Improve metrics and access logs #937, capturing the Position::Server gating, the snapshot-before-end_stream contract, and the schema-unchanged Endpoint::socket() addition.
  • doc/configure.md — new "Round-trip-time fields on access logs" subsection documenting tags switch println to a log framework - choose a log framework #9/Handle "100 Continue" answers #10, the getsockopt(TCP_INFO) source (Linux SOL_TCP, BSD IPPROTO_TCP, Darwin IPPROTO_TCP opt 0x106), the µs unit (Darwin's tcpi_srtt is converted from milliseconds inside socket_rtt), and the platform matrix.
  • doc/h2_mux_internals.md — refreshes the generate_access_log snippet to the new 5-arg signature and shows the snapshot_rtts call alongside distribute_overhead.
  • command/src/command.proto — schema unchanged; ProtobufAccessLog tags 9 and 10 were already declared optional.

Out of scope

  • E2E RTT-cell assertion. e2e/src/ has no access-log capture infrastructure today (the aggregator at e2e/src/mock/aggregator.rs counts backend requests/responses, not worker access logs). Building access-log capture is its own task with broad utility for future access-log work and is deliberately deferred. The new RTT capture sites are exercised by every passing happy-path H2 e2e test, so a regression in the call graph would surface there.
  • HAProxy-style cluster_response_time field name. The umbrella issue listed it as outstanding; semantically it maps to backend_response_time, already emitted as the response_time access-log field (tag 21) populated from metrics.backend_response_time(). Doc/alias decision belongs under Improve metrics and access logs #937 rather than this PR.

@FlorentinDUBOIS FlorentinDUBOIS self-assigned this May 2, 2026
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the feat/mux-access-log-rtt branch from ae9b505 to 2f9ea3f Compare May 3, 2026 10:55
The HTTP/2 mux access log emitted `client_rtt: None, server_rtt: None`
because the mux Stream type owns no socket reference: the frontend
socket lives on the parent `Mux`/connection and the backend socket
lives on `Router.backends.get(token)` keyed off
`StreamState::Linked(token)`. The H1 (`kawa_h1`), Pipe (TCP/WS) and
TCP-frontend access logs already populate both fields, so the H2 path
was the only asymmetric outlier under the "Improve metrics and access
logs" umbrella.

This change snapshots both RTTs at every access-log emission boundary
and threads them as parameters into `Stream::generate_access_log`,
matching the inline pattern already used by `kawa_h1`, `pipe` and the
TCP frontend. Schema unchanged — `ProtobufAccessLog` tags 9 and 10
were already declared optional. TCP_INFO via `socket::stats::socket_rtt`
ships real implementations on Linux, FreeBSD, NetBSD, OpenBSD,
DragonFly, macOS and iOS; non-Unix stubs return `None`.

Implementation:

- `Stream::generate_access_log` gains `client_rtt: Option<Duration>`
  and `server_rtt: Option<Duration>` parameters.
- New `Stream::linked_token() -> Option<Token>` accessor so each
  capture site is a single
  `linked_token().and_then(|t| endpoint.socket(t)).and_then(socket_rtt)`
  chain instead of an inline `if let StreamState::Linked(token) = ...
  else None` block.
- The `Endpoint` trait gains a small `socket(token) -> Option<&TcpStream>`
  method. `EndpointServer` returns the frontend socket (token ignored).
  `EndpointClient` looks up the backend by token through the existing
  `Router.backends` map. Both impls are one line.
- New `ConnectionH2::snapshot_rtts(position, socket, endpoint, linked_token)`
  associated function consolidates the five `Position::Server`-gated
  H2 capture blocks into one place. Takes individual field references
  (not `&self`) so it composes with `H2BlockConverter` holding
  `&mut self.encoder` during the per-stream write loop, mirroring the
  shape of the existing `try_recycle_server_stream`.
- Eight access-log emission sites updated:
  * `mux/mod.rs` Mux session-close. Frontend RTT is hoisted above the
    per-stream loop because the frontend socket is the same for every
    stream — one `getsockopt(TCP_INFO)` syscall per session close
    instead of N.
  * `mux/h1.rs` `H1::Upgrade`, `H1::EarlyHint`, `H1::Complete` (all
    inside the pre-existing `Position::Server` arm). `H1::EarlyHint`
    uses the same `linked_token().and_then(...)` chain as the other
    sites; the `else` arm of the outer `if let Linked` already
    guarantees `linked_token() == None`, so `server_rtt` resolves to
    `None` naturally.
  * `mux/h2.rs` `H2::Complete` via `complete_server_stream` (both
    `try_recycle_server_stream` callers — the `expect_write` flush
    path and the per-stream write loop). Snapshot is gated to
    `Position::Server` at the call site so backend H2 connections
    cannot capture upstream RTT under `client_rtt` and frontend RTT
    under `server_rtt` (`try_recycle_server_stream` returns `None` for
    `Position::Client`, but the shared `Stream.metrics` would still
    have been written with swapped values that the matching frontend's
    later snapshot could fail to overwrite).
  * `mux/h2.rs` `H2::IdleTimeout`, `H2::ResetFrame`, `H2::Reset`.
    Snapshot is hoisted ABOVE `endpoint.end_stream(token, ...)` so the
    `server_rtt` lookup does not depend on `EndpointClient::end_stream`
    continuing to leave entries in `Router.backends`. Order is
    monotonic ("snapshot, then tear down") and matches the
    `kawa_h1`/`pipe` pattern.
- `try_recycle_server_stream` and `complete_server_stream` gain
  matching `client_rtt`/`server_rtt` parameters that are forwarded to
  `Stream::generate_access_log`.

Refs #937.

Validation:

- `cargo build --all-features --locked`: clean
- `cargo +nightly fmt --all -- --check`: clean
- `cargo clippy --all-targets --all-features --locked`: no issues
- `cargo test -p sozu-lib --all-features --lib`: 518 passed
- `cargo test -p sozu-e2e h2_basic`: 4 passed

Documentation:

- `CHANGELOG.md` bullet under `### 🌟 Added` linked to #937, capturing
  the `Position::Server` gating, snapshot-before-`end_stream` contract,
  and the schema-unchanged `Endpoint::socket()` addition.
- `doc/configure.md` adds a "Round-trip-time fields on access logs"
  subsection documenting tags #9/#10, the `getsockopt(TCP_INFO)` source
  (Linux `SOL_TCP`, BSD `IPPROTO_TCP`, Darwin `IPPROTO_TCP` opt
  `0x106`), the µs unit (Darwin's `tcpi_srtt` is converted from
  milliseconds inside `socket_rtt`), and the platform matrix.
- `doc/h2_mux_internals.md` refreshes the `generate_access_log` snippet
  to the new 5-arg signature and shows the `snapshot_rtts` call
  alongside `distribute_overhead`.
- `command/src/command.proto`: schema unchanged; `ProtobufAccessLog`
  tags 9 and 10 were already declared optional.

Out of scope:

- E2E RTT-cell assertion. `e2e/src/` has no access-log capture
  infrastructure today (the `aggregator` at `e2e/src/mock/aggregator.rs`
  counts backend requests/responses, not worker access logs). Building
  access-log capture is its own task with broad utility for future
  access-log work and is deliberately deferred. The new RTT capture
  sites are exercised by every passing happy-path H2 e2e test, so a
  regression in the call graph would surface there.
- HAProxy-style `cluster_response_time` field name. The umbrella issue
  listed it as outstanding; semantically it maps to
  `backend_response_time`, already emitted as the `response_time`
  access-log field (tag 21) populated from
  `metrics.backend_response_time()`. Doc/alias decision belongs under
  #937 rather than this PR.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the feat/mux-access-log-rtt branch from 2f9ea3f to 41524bc Compare May 3, 2026 17:22
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 0dd4471 into main May 4, 2026
38 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the feat/mux-access-log-rtt branch May 4, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant