Repository navigation
perf(prometheus): reduce temporary scrape allocations - #139535
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 5, 2026, 8:07 PM ET / September 6, 2026, 00:07 UTC. ClawSweeper reviewWhat this changesThe Prometheus metrics plugin reduces temporary scrape allocations by sorting fresh entry arrays and reusing formatted histogram labels. Merge readiness✅ Ready for maintainer review Keep open as a useful performance improvement absent from pinned main. No introduced correctness or security defects were found; this maintainer-authored PR remains a viable landing candidate. Priority: P2 Review scores
Verification
How this fits togetherThe Prometheus plugin aggregates diagnostic events into bounded in-memory metrics. Its authenticated HTTP endpoint renders those metrics for monitoring systems. flowchart LR
A[Diagnostic events] --> B[Trusted event filter]
B --> C[Bounded metric store]
D[Authenticated scrape request] --> E[Scrape renderer]
C --> E
E --> F[Prometheus text response]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource -6. Total -6 across 1 file. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep the optimization within the existing synchronous renderer while preserving metric bytes, bounded retention, and lifecycle semantics. Do we have a high-confidence way to reproduce the issue? Not applicable as a functional bug reproduction: this is an allocation optimization, and current-main source directly shows the redundant copying and repeated formatting. Is this the best way to solve the issue? Yes. Reusing scrape-local arrays and label fragments removes redundant work without adding cache invalidation, changing public APIs, or mutating canonical metric maps. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 8964a4d8dc2f. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Merged via squash.
|
What Problem This Solves
Resolves repeated copying and label formatting during Prometheus scrapes, especially near the exporter’s series cap. Rendering copied metric maps, expanded them into arrays, copied those arrays again for sorting, and repeatedly sorted/escaped histogram labels for buckets, sum, and count.
Why This Change Was Made
extensions/diagnostics-prometheus/src/service.tscreates fresh scrape-local entry arrays directly from its canonical private maps, sorts those arrays, shares counter/gauge rendering, and reuses sorted/escaped histogram-label fragments within a scrape. Projection remains synchronous; the former map snapshots were shallow and did not isolate sample objects.The current-main composition preserves the original optimization and main’s newly added
openclaw_gateway_build_infogauge. That existing gauge reserves one slot in the same 2,048-sample cap, captures process identity and optional build ID once at enabled startup, and clears/repopulates on stop/restart. It still uses ordinary gauge rendering. Runtime identity remains with the service host’s lease-validated callback; the optimization changes neither identity lookup nor admission/retention.Production remains +46/-52 lines (net -6); tests/support: +0/-0. No persistent cache, runtime state, dependency, or identity capability is added by this PR.
User Impact
Scrapes allocate fewer temporary objects while preserving metric names, labels, escaping, buckets, privacy, cap/drop accounting, build identity, reset behavior, and HTTP metadata. Existing series continue updating after saturation, and the reserved build-info sample survives saturation.
Evidence
The new sealed proof compares main
3dff8818ef42872cecc3899b41dffd95942fe70fwith that baseline plus the exact reviewed performance edits. Eleven complete body pairs match byte-for-byte, covering startup/restart, mixed metrics, stop/disabled diagnostics, RPC cap/update/overflow, model cap, process-only identity, and unavailable identity capability. The original four-run measurements remain historical; no original output is reused as current-composition proof.The actual exporter receives synthetic events through the real diagnostic dispatcher. Controlled runtime identity preserves exact comparisons. The build-info sample appears once with value 1 and correctly ordered labels; process/build IDs stay confined to it. With the lifecycle counter and gauge admitted first, RPC drops remain 89 after existing-series updates and rise to 91 for two unseen samples; model drops are 2,154 in both arms.
Six HTTP states per arm exercise real local GET/HEAD/POST, including saturation and optional/disabled identity cases. GET/HEAD return 200, POST returns 405 with
Allow: GET, HEAD, HEAD is empty, and Content-Length matches each GET body—including the 31,140-byte mixed response and zero-byte disabled response. All 12 servers close; stop removes subscriptions. Five enabled identity-bearing starts make five identity reads despite repeated scrapes; disabled startup makes none.One fresh allocation observation per arm on Node 26.8.1/macOS arm64:
These 4 KiB cumulative V8 samples include collected objects after five warmups; imports, event seeding, and HTTP are excluded. The single shared-host pair is descriptive corroboration, not retained heap/RSS, statistical significance, whole-Gateway performance, or authentication/installation proof.
Both arms pass 32 tests: 31 plugin tests and one host lease/identity test. Candidate type-aware plugin lint and formatting pass. Independent P2 review of this composition records no actionable findings. Broad duplicate gates were not run; final exact-head required CI/native landing and applicable integration gates remain with publication.