refactor: split format/cos.go view-model build from render (#4657)#4684
Merged
Conversation
format/cos.go had grown to 865 LOC with FormatCoSInterfaceSummary interleaving the config+runtime view-model join with every render block (per-interface header, binding telemetry, the tabwriter queue grid, and the interleaved per-queue Drops / Sojourn / Surplus / Equal-flow / OwnerProfile / DrainShape / Waterfill detail rows). This mirrors the #4656 status.go aggregate-then-render split. Pure code motion, output byte-identical. The change: - Move the view-model construction into a new cos_sections.go: configuredCoSInterfaceViews, cosRuntimeIndex + buildCoSRuntimeIndex + lookup + cosRuntimeCandidateNames, buildCoSQueueViews, and isOldJSONSyntheticDefaultQueue are the only places that join the CoS/interface stanzas with the runtime snapshot. - Decompose the FormatCoSInterfaceSummary render loop into per-section helpers (also in cos_sections.go): writeCoSInterfaceHeader (config + runtime + waterfill header), writeCoSQueueTable (the aligned tabwriter grid), and writeCoSQueueDetail (the interleaved detail rows). renderBindingScopedTelemetry and the histogram-percentile helpers move alongside them. - cos.go keeps the cosInterfaceView / cosQueueView types, the FormatCoSInterfaceSummary orchestrator, and the leaf formatters (cos.go 280 LOC, cos_sections.go 632 LOC). The queue view slice is still built exactly once per interface by the orchestrator and shared between the binding-scoped telemetry line and the queue table — no extra fetches, no re-iteration. Validation: added TestFormatCoSInterfaceSummaryGolden (testdata/cos_summary.golden) as the byte-identity gate. The golden was generated from the pre-refactor code with a fixture that exercises every conditional header/queue/detail block, and the refactored code reproduces it byte-for-byte; it reuses the package-level -update flag. go build ./... clean, go vet clean, and go test ./pkg/dataplane/userspace/format/ green (golden + all 24 existing CoS tests). No operator-facing contract change (output unchanged), so no module doc update is needed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015oARShYtiJJ2H4UB4nXGqi
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the CoS interface summary formatter to separate view-model construction from rendering, keeping the output byte-identical while making the code easier to follow and safer to evolve.
Changes:
- Extracted config+runtime joining and per-section rendering helpers from
cos.gointo newcos_sections.go. - Added a golden test (
TestFormatCoSInterfaceSummaryGolden) plustestdata/cos_summary.goldento enforce byte-identical output. - Logged the refactor in
_Log.md.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/dataplane/userspace/format/cos.go | Keeps core types/orchestrator; delegates header/table/detail rendering to section helpers. |
| pkg/dataplane/userspace/format/cos_sections.go | New file containing view-model build + section render helpers and histogram/telemetry helpers. |
| pkg/dataplane/userspace/format/cos_golden_test.go | New golden test guarding byte-identical output for FormatCoSInterfaceSummary. |
| pkg/dataplane/userspace/format/testdata/cos_summary.golden | New golden output fixture used by the golden test. |
| _Log.md | Adds required log entry for the refactor. |
Comment on lines
+333
to
+335
| // `total` is the authoritative sample count (drain_invocations). When | ||
| // `total == 0`, emit "0µs" rather than "nan" or "-" so the field | ||
| // aligns visually across queues and the zero value is obvious. |
Comment on lines
+121
to
+124
| got := FormatCoSInterfaceSummary(testCoSConfig(), goldenCoSSummaryStatus(), "") | ||
| goldenPath := filepath.Join("testdata", "cos_summary.golden") | ||
| if *updateGolden { | ||
| if err := os.MkdirAll("testdata", 0o755); err != nil { |
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.
Closes #4657.
format/cos.gohad grown to 865 LOC withFormatCoSInterfaceSummaryinterleaving the config+runtime view-model join with every render block.
This mirrors the #4656
status.goaggregate-then-render split.Pure code motion — output byte-identical
cos_sections.go:configuredCoSInterfaceViews,cosRuntimeIndex+buildCoSRuntimeIndex+lookup+cosRuntimeCandidateNames,buildCoSQueueViews,isOldJSONSyntheticDefaultQueue— the only places that join theCoS/interface stanzas with the runtime snapshot.
cos_sections.go: theFormatCoSInterfaceSummaryloop was decomposed into
writeCoSInterfaceHeader(config + runtime +waterfill header),
writeCoSQueueTable(the aligned tabwriter grid), andwriteCoSQueueDetail(the interleaved Drops / Sojourn / Surplus /Equal-flow / OwnerProfile / DrainShape / Waterfill rows).
renderBindingScopedTelemetry+ the histogram helpers move alongside.cos.gokeeps thecosInterfaceView/cosQueueViewtypes, theFormatCoSInterfaceSummaryorchestrator, and the leaf formatters(cos.go 280 LOC, cos_sections.go 632 LOC).
The queue view slice is still built exactly once per interface by the
orchestrator and shared between the binding-scoped telemetry line and the
queue table — no extra fetches, no re-iteration.
Byte-identity gate
Added
TestFormatCoSInterfaceSummaryGolden(testdata/cos_summary.golden).The golden was generated from the pre-refactor code with a fixture that
exercises every conditional header/queue/detail block; the refactored code
reproduces it byte-for-byte. It reuses the package-level
-updateflag fromstatus_golden_test.go.Validation
go build ./...cleango vet ./pkg/dataplane/userspace/format/cleango test ./pkg/dataplane/userspace/format/green (golden + all 24 existingCoS tests)
gofmtcleanNo operator-facing contract change (output unchanged), so no module doc update
is needed.
🤖 Generated with Claude Code
https://claude.ai/code/session_015oARShYtiJJ2H4UB4nXGqi