Skip to content

feat(gateway): log one access line per proxied connection - #1589

Merged
kvinwang merged 4 commits into
nextfrom
feat/gateway-access-log
Nov 15, 2026
Merged

kvinwang merged 4 commits into
nextfrom
feat/gateway-access-log

Conversation

@kvinwang

@kvinwang kvinwang commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a default-on dstack_gateway::access info line, written once when each proxied connection ends:

INFO conn{id=1042}: dstack_gateway::access: client="127.0.0.1:60834" peer=127.0.0.1:60834 sni="localhost-38404.gwtest.local" route="terminate" app_id="localhost" port=38404 instance="localhost" bytes_in=769 bytes_out=1466 us=5132 outcome="closed"

Today, seeing who connected to what means enabling the per-connection debug logs. Those emit about 6 lines per connection and enter a span on every poll. Disable this line with RUST_LOG=info,dstack_gateway::access=off.

  • Bytes are read from TCP_INFO on a dup of the inbound socket. That covers the userspace, splice and kTLS relays, including failed connections. For terminated TLS they count wire bytes.
  • instance is the backend instance that won the connect race.
  • The libc requirement is bumped to 0.2.189 for the glibc tcp_info byte fields.

Benchmark (tdxlab, Docker; gateway 4 cores / 4 workers, nginx origin 4 cores, wrk 8 threads / 128 conns with Connection: close; TLS terminate; 20 s per variant, variants interleaved, 6 rounds, first round dropped as warm-up):

Logging req/s gateway CPU per conn
off 42.6k 92.6 µs
conn span only 42.0k 94.0 µs
access line only 39.1k 100.7 µs
span + access line (default) 38.4k 102.6 µs

This is the worst case (tiny responses, one TLS handshake per request); the per-connection cost is ~10 µs, mostly the access line itself. For comparison the current debug logs cost ~20 µs per connection.

Tests: cargo test -p dstack-gateway, test-run/e2e and test-run/proxy-e2e pass. For proxy-e2e, RUST_LOG=info was set locally so the line could be checked. Across baseline/splice/kTLS arms it showed ~1 MiB bytes_out and the expected outcomes.

Seeing who connected to which app meant turning on the per-connection
debug logs: about six lines per terminated connection plus a span entered
on every poll, which costs 17% of the new-connection rate on a 4-core
gateway. Emit a single `dstack_gateway::access` info line when a
connection ends instead: client, peer, SNI, route, app, port, backend
instance, bytes in/out, duration and outcome. It costs about 3% in the
same benchmark and is on by default; `dstack_gateway::access=off` turns
it off, leaving one level check per connection.

Bytes come from TCP_INFO on a dup of the inbound socket, so the spliced
and kTLS paths, whose payload never enters the process, are counted the
same way as the userspace relay, failed connections included. For
terminated connections they are TLS bytes on the wire. The glibc
`tcp_info` fields need libc 0.2.189.

`Proxy::proxy` takes the parsed `DstInfo` rather than its fields to stay
under clippy's argument limit.

Signed-off-by: Kevin Wang <wy721@qq.com>
Most proxied connections finish in a few milliseconds, where whole
milliseconds round away most of the signal.

Signed-off-by: Kevin Wang <wy721@qq.com>
The `conn` span is debug-level, so at the default level it is never
created and the access line carries no id. Allocate the id for every
connection and log it as `conn_id`, so the line can be matched with the
same connection's debug logs when those are on.

Signed-off-by: Kevin Wang <wy721@qq.com>
The access line had its own `conn_id` field because the `conn` span was
debug-level and absent by default. Make the span info-level instead, so
every log line a connection emits carries `conn{id=...}` and the access
line is the one place that names the app, SNI and backend. The span costs
about 1.5us of gateway CPU per connection (~1.5% of TLS-terminate CPS) on
the same 4-core benchmark.

Signed-off-by: Kevin Wang <wy721@qq.com>
@kvinwang
kvinwang merged commit 9b98c96 into next Nov 15, 2026
23 checks passed
@kvinwang
kvinwang deleted the feat/gateway-access-log branch November 15, 2026 15:50
kvinwang added a commit that referenced this pull request Nov 16, 2026
tc-gw-internal-009 checks that the gateway refuses the old listener
locations (#1587), restarts it with proxy_protocol on the admin listener
(#1582), and checks the admin audit (#1588) and access (#1589) lines.

Signed-off-by: Kevin Wang <wy721@qq.com>
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