[fp-enhancer] fp-enhancer: Improve pkg/agentdrain (round 1/20)#26177
[fp-enhancer] fp-enhancer: Improve pkg/agentdrain (round 1/20)#26177
Conversation
- Use slices.Clone for defensive template copying in cluster.go, persist.go (replaces verbose make+copy pattern in 3 places) - Pre-allocate snap.Clusters slice with known capacity in SaveJSON - Use sliceutil.FilterMapKeys in FlattenEvent to replace imperative filter loop with declarative expression Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR applies small functional/immutability refactors in pkg/agentdrain (Drain log template miner), primarily around defensive slice copying and a more declarative map-key filtering approach.
Changes:
- Replace manual
make+copydefensive copies withslices.Clonein cluster creation and persistence. - Pre-allocate the
Snapshot.Clustersslice inSaveJSON. - Simplify
FlattenEventkey filtering usingsliceutil.FilterMapKeys.
Show a summary per file
| File | Description |
|---|---|
| pkg/agentdrain/persist.go | Uses slices.Clone for template copies and pre-allocates Snapshot.Clusters during JSON save/load. |
| pkg/agentdrain/mask.go | Refactors event field key collection via sliceutil.FilterMapKeys before sorting. |
| pkg/agentdrain/cluster.go | Uses slices.Clone for defensive template copying when creating new clusters. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 1
| Config: m.cfg, | ||
| NextID: m.store.nextID, | ||
| Clusters: make([]SnapshotCluster, 0, len(m.store.clusters)), | ||
| } |
There was a problem hiding this comment.
SaveJSON now initializes Snapshot.Clusters with an empty (non-nil) slice. When there are zero clusters this changes the serialized JSON from "clusters": null (previous behavior) to "clusters": [], which is an observable wire-format change (and may diverge from existing persisted snapshots / pkg/agentdrain/data/default_weights.json that currently contains clusters: null for some stages). If compatibility is important, keep Clusters nil when len(m.store.clusters)==0 (only preallocate when non-empty), or explicitly decide on the new format and update the PR description + any committed snapshot data accordingly.
| Config: m.cfg, | |
| NextID: m.store.nextID, | |
| Clusters: make([]SnapshotCluster, 0, len(m.store.clusters)), | |
| } | |
| Config: m.cfg, | |
| NextID: m.store.nextID, | |
| } | |
| if len(m.store.clusters) > 0 { | |
| snap.Clusters = make([]SnapshotCluster, 0, len(m.store.clusters)) | |
| } |
Apply moderate functional/immutability improvements to
pkg/agentdrain— the Drain log template miner package.Summary
3 files changed, 17 insertions(+), 19 deletions(−)
slices.Clonefor defensive copiescluster.go,persist.gosnap.Clustersslicepersist.gosliceutil.FilterMapKeysinFlattenEventmask.goChanges
pkg/agentdrain/cluster.goadd(): replacemake([]string, len)+copywithslices.Clone(template)— expresses defensive copy intent directlypkg/agentdrain/persist.goSaveJSON(): pre-allocateClustersfield in theSnapshotstruct literal withmake([]SnapshotCluster, 0, len(m.store.clusters))to avoid silent re-allocationSaveJSON()+LoadJSON(): replacemake+copywithslices.Clone(2 occurrences)pkg/agentdrain/mask.goFlattenEvent(): replace imperative filter loop withsliceutil.FilterMapKeys— the helper already exists in the codebase and the map predicate reads naturallyBehavior
No behavior changes — all transformations are semantically equivalent.
Testing
pkg/agentdraintests pass (go test ./pkg/agentdrain/)go vetcleango fmtappliedRound-Robin Progress
Processed:
pkg/agentdrain(1 of 20 packages)Next run:
pkg/cliReferences: