Repository navigation
feat(mux): populate client_rtt and server_rtt on the H2 access log - #1242
Merged
Merged
Conversation
FlorentinDUBOIS
force-pushed
the
feat/mux-access-log-rtt
branch
from
May 3, 2026 10:55
ae9b505 to
2f9ea3f
Compare
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
force-pushed
the
feat/mux-access-log-rtt
branch
from
May 3, 2026 17:22
2f9ea3f to
41524bc
Compare
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The HTTP/2 mux access log emitted
client_rtt: None, server_rtt: Nonebecause the muxStreamtype owns no socket reference: the frontend socket lives on the parentMux/connection and the backend socket lives onRouter.backends.get(token)keyed offStreamState::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 bykawa_h1,pipeand the TCP frontend. Schema unchanged —ProtobufAccessLogtags 9 and 10 were already declared optional. TCP_INFO viasocket::stats::socket_rttships real implementations on Linux, FreeBSD, NetBSD, OpenBSD, DragonFly, macOS and iOS; non-Unix stubs returnNone.Refs #937.
Implementation
Stream::generate_access_loggainsclient_rtt: Option<Duration>andserver_rtt: Option<Duration>parameters; the previousNoneplaceholders inmux/stream.rsare replaced.Stream::linked_token() -> Option<Token>accessor so each capture site collapses to a singlelinked_token().and_then(|t| endpoint.socket(t)).and_then(socket_rtt)chain.Endpointtrait gains a smallsocket(token) -> Option<&TcpStream>method.EndpointServerreturns the frontend socket (token ignored).EndpointClientlooks up the backend by token through the existingRouter.backendsmap. Both impls are one line.ConnectionH2::snapshot_rtts(position, socket, endpoint, linked_token)associated function consolidates the fivePosition::Server-gated H2 capture blocks into one place. Takes individual field references (not&self) so it composes withH2BlockConverterholding&mut self.encoderduring the per-stream write loop, mirroring the shape of the existingtry_recycle_server_stream.try_recycle_server_streamandcomplete_server_streamgain matchingclient_rtt/server_rttparameters that are forwarded toStream::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 — onegetsockopt(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-existingPosition::Serverarm.H1::EarlyHintuses the standardlinked_token().and_then(...)chain; theelsearm of the outerif let Linkedalready guaranteeslinked_token() == None, soserver_rttresolves toNonenaturally.lib/src/protocol/mux/h2.rs—H2::Completeviacomplete_server_stream(bothtry_recycle_server_streamcallers — theexpect_writeflush path and the per-stream write loop). The snapshot is gated toPosition::Serverat the call site so backend H2 connections cannot capture upstream RTT underclient_rttand frontend RTT underserver_rtt(try_recycle_server_streamreturnsNoneforPosition::Client, but the sharedStream.metricswould 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 ABOVEendpoint.end_stream(token, ...)so theserver_rttlookup does not depend onEndpointClient::end_streamcontinuing to leave entries inRouter.backends. Order is monotonic ("snapshot, then tear down") and matches thekawa_h1/pipepattern.Test plan
cargo build --all-features --locked— cleancargo +nightly fmt --all -- --check— cleancargo clippy --all-targets --all-features --locked— no findingscargo test -p sozu-lib --all-features --lib— 518 passedcargo test -p sozu-e2e h2_basic— 4 passed (H2 happy-path call sites exercised on the new code paths)Documentation
CHANGELOG.md— bullet under### 🌟 Addedlinked to Improve metrics and access logs #937, capturing thePosition::Servergating, the snapshot-before-end_streamcontract, and the schema-unchangedEndpoint::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, thegetsockopt(TCP_INFO)source (LinuxSOL_TCP, BSDIPPROTO_TCP, DarwinIPPROTO_TCPopt0x106), the µs unit (Darwin'stcpi_srttis converted from milliseconds insidesocket_rtt), and the platform matrix.doc/h2_mux_internals.md— refreshes thegenerate_access_logsnippet to the new 5-arg signature and shows thesnapshot_rttscall alongsidedistribute_overhead.command/src/command.proto— schema unchanged;ProtobufAccessLogtags 9 and 10 were already declared optional.Out of scope
e2e/src/has no access-log capture infrastructure today (theaggregatorate2e/src/mock/aggregator.rscounts 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.cluster_response_timefield name. The umbrella issue listed it as outstanding; semantically it maps tobackend_response_time, already emitted as theresponse_timeaccess-log field (tag 21) populated frommetrics.backend_response_time(). Doc/alias decision belongs under Improve metrics and access logs #937 rather than this PR.