feat(usage): record upstream connection and first-byte timings in usage records - #6087
Open
chokhung588-cell wants to merge 1 commit into
Open
chokhung588-cell wants to merge 1 commit into
chokhung588-cell wants to merge 1 commit into
Conversation
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
Expose upstream HTTP connection setup and first-byte response timings on
usage.Record, and propagate them through the usage translation adapter and the Redis usage queue.This provides proxy operators and downstream metrics/analytics consumers with visibility into the latency split between:
Why
When debugging slow streaming requests, operators frequently observe a large gap between client-perceived TTFT and proxy TTFT.
Currently,
usage.Recordonly recordsTTFTandGenerationDuration. However,firstPacketDurationis already measured internally ininternal/runtime/executor/helps/usage_helpers.goduringMarkFirstResponseByte. Furthermore, upstream HTTP transport metrics (DNS, TCP handshake, TLS handshake, and connection reuse viahttptrace) were measured during the attempt but discarded before reaching the published usage event.By capturing
UpstreamTTFB,ConnSetupDuration,ConnReused, andFirstPacketDuration:usage.Recordandpluginapi.UsageRecord.Changes
internal/runtime/executor/helps/usage_helpers.go:UpstreamTTFB,ConnSetup, andConnReusedviahttptrace.ClientTraceinTrackHTTPClient.ObserveUpstreamAttemptwith first-wins semantics for multi-attempt / retry scenarios.FirstPacketonMarkFirstResponseByte.usage.Record.sdk/cliproxy/usage/manager.go:UpstreamTTFB,ConnSetup,FirstPacket(time.Duration), andConnReused(bool) toRecord.sdk/pluginapi/types.go:UsageRecordfor plugin ABI consumption.internal/pluginhost/adapters_usage_translation.go:usage.Recordtopluginapi.UsageRecord.internal/redisqueue/plugin.go:upstream_ttfb_ns,conn_setup_ns,first_packet_ns,conn_reused).internal/runtime/executor/helps/upstream_timing_test.go:TestRoundTripRecordsUpstreamTiming: verifies accurate recording of TTFB, first packet, and connection setup.TestRoundTripRecordsConnReuse: assertsConnReused == trueandConnSetup == 0for pooled keep-alive connections.TestObserveUpstreamAttemptFirstWins: verifies retry first-wins semantics.TestMarkFirstResponseByteRecordsFirstPacket: asserts semantic alignment betweenFirstPacketandTTFT.Validation
gofmt -w .— clean.go build -o test-output ./cmd/server && rm test-output— verified compile passed.go test -v ./internal/runtime/executor/helps -run "Upstream|MarkFirstResponseByte" -count=1— 4/4 passed.go test -v ./internal/runtime/executor/helps ./sdk/cliproxy/usage ./sdk/pluginapi ./internal/pluginhost ./internal/redisqueue -count=1— all passed.go test ./...passed.