Repository navigation
feat(keepalive): measure manual holds (evictions, size, bytes held) - #1137
Merged
Merged
Conversation
Add metadata-only measures for #1103: an eviction counter by reason, a too-large refusal counter, a body-size histogram at hold time, a per-tenant held-bytes gauge, and one log line when a tenant holds 2 bodies of 8 MiB or more. Refs #1103 Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
force-pushed
the
feat/1103-manual-hold-metrics
branch
from
November 12, 2026 15:08
81ff737 to
d008b18
Compare
Collaborator
Author
|
Integrator note: the owner approved this PR's behavior change on 2026-11-12 ("1103 approve"): new |
amiddavid
added a commit
that referenced
this pull request
Nov 12, 2026
Add checks for too-large refusals, manual-hold evictions by the per-tenant cap, and the held-body size histogram (#1103, #1137). Mark G12 as partly closed, and note how to read /metrics on a hosted deployment with no tenant id in the report. Assisted-By: Claude Opus 5.5 Signed-off-by: DAVID AMID <[email protected]>
amiddavid
commented
Nov 12, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | d008b18f7ab7 |
approve |
Verdict: approve
Findings
None. I could not prove a defect in the changed lines. Details of what I ruled out are under "What I checked".
Non-blocking
- proxy/promexport.go:751-758 — the new process-wide series carry no procCaveat. Every other in-process
cg_keepalive_*series usespromHeaderProc, which appends the "restarts from 0 with the process, summed over every tenant" note. The new counters and histogram use plainpromHeader. The in-code comment says "process-wide, like every other cg_keepalive_* series", but the HELP text differs from theirs. The dashboard-mismatch half of the caveat does not apply here (no dashboard figure exists), so this is a consistency gap, not wrong output. - keepalive/manualmetrics.go:12-15 —
EvictReasonBytes/EvictReasonCountare exported but unused.proxy/promexport.go:753-754hardcodesreason="bytes"andreason="count". The constants and the exporter can drift apart. Use the constants in the exporter, or make them unexported. - keepalive/manualmetrics.go:18 —
ManualHoldBucketsMiBis an exported mutable var. Any importer can change the histogram bounds at run time. A copy for the exporter, or an accessor, would remove that. - Docs scope of
cg_keepalive_refused_total. The data-dependence row says "The manual holds also export ... too-large refusals". The counter also counts refusals of requests on accounts with automatic keep-alive on, which never asked for the Ping button (keeper.go:881-889 sits on the shared path). The HELP text says this explicitly, and the PR body does too. Only the row wording is looser than the code. - Histogram counts one observation per finished request, not per thread. A new request on a held thread replaces the hold, and
recordManualHoldLockedcounts it again.cg_keepalive_manual_hold_body_bytes_counttherefore counts turns, not distinct threads. The HELP text ("at the moment a manual hold started") covers this reading, but a panel author may read "hold" as "thread". Worth one sentence in the reference doc.
What I checked
- Read the full diff (9 files) and the code around each change:
keeper.goRecord path,manualhold.goeviction loop,manualmetrics.go,promexport.gorender, both test files, and the three docs. - Locking.
k.mmis guarded byKeeper.mueverywhere:recordManualHoldLockedandevictManualLockedrun under the lock taken at keeper.go:974; thetooLargeincrement at keeper.go:883-885 takes the lock on a path where it is not held (unlocked at keeper.go:866, and that branch returns before the lock at 974).ManualHoldSnapshotlocks too. No deadlock, no race found. - Double-count risk in the histogram.
Keeper.Recordhas exactly one production caller (proxy/keepalive.go:417), run once per finished request from the defer atproxy/proxy.go:2041. So one observation per turn. The test helperrecordHoldsends two Records by design;holdOnceexists to avoid that. Correct. - Histogram correctness.
recordManualHoldLockedruns afterk.live[key] = e(keeper.go:1048-1056), so the new body is counted in the large-body scan. Bucket bounds use>on the lower side, sobucket[i]means<= bound, matching the comment. The cumulative export in promexport sums cells 0..4 and sets+InftoSizeCount, which equals the sum of all 6 cells. Consistent. - Eviction counters.
evictManualLockedis called only from keeper.go:1057. Thecount-vs-byteslabel pickscountwhen both caps are over; the PR body states this. The counters move only on per-tenant-cap evictions; releases by hold expiry, thread replacement, tenant deletion and the global caps are not counted, which matches the documented meaning. - The log-once
pairmap. I traced the re-arm: after both large bodies are gone, the next hold records withlarge < 2and deletes the flag, so a later second large body logs again.deleteon a nil map is a no-op, so the first hold is safe. The map is bounded by tenant count. - Fail-open. The change adds counters, a log line and an export only. No request-path behavior changes: nothing new can refuse, hold or evict differently. The PR names this at the top, as RELEASING.md asks.
- Data dependence. The row file and the regenerated page carry the same added sentence; the generated diff matches the row. The measures are counts, byte sizes and tenant ids only; the log line carries sizes and the tenant id, no body content (the test checks this).
- Tests. I read all 7 new tests and traced each against the code: 17 holds → 1 count eviction; 2 x 20 MiB → 1 byte eviction and the gauge at 20 MiB; hold-then-expire empties the gauge; a
padBody(MaxBodyBytes)body is over the 32 MiB limit, so it movestoo_largeand not the histogram; bucket placement for a small and a 5 MiB body; the log fires once for 3 large bodies and carries noxxxxxcontent. The proxy test's fixture,register(label "laptop"),h.keeper(always set inNew),registry()and the/metricsloopback-or-token gate all exist as the test assumes.relayServesClaudepasses for the test's empty endpoint because the model id contains "claude". I could not run the tests (live tests not approved); this is a read-only trace. - Doc claims against code. Bucket bounds 1/4/8/16/32 MiB, caps 16 holds and 32 MiB,
LargeManualHoldBytes= 8 MiB, the label set{tenant,label}, and the/metricsaccess rule all match the code.
amiddavid
commented
Nov 12, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | d008b18f7ab7 |
approve |
Verdict: approve
Findings
None. I could not confirm a real defect.
Non-blocking
- keepalive/manualmetrics.go:96 — the WARN line fires while
k.muis held. Every other keeper log call (noteOutcome,breaker.go) runs outside the lock. The default handler writes to stderr or an unbuffered file, and the line fires once per tenant episode, so the hold time is short. Move the log afterk.mu.Unlock()if you want the pattern to match. - keepalive/manualmetrics.go:105 —
pairnever shrinks on expiry or purge. A tenant that logged once stays in the map until one of its own later records sees fewer than 2 large bodies.dropReasonsLockedcleansreasonson a tenant purge;pairhas no such path. One entry is a tenant id and a bool, so the leak is small. It is unbounded in tenant count over a long process life. - keepalive/manualmetrics.go:21 —
ManualHoldBucketsMiBis an exported mutable var. A host can change it at run time and race with the keeper, which reads it underk.muonly. A[...]int64const array is not possible in Go, but an unexported var plus an exported copy would remove the hazard. - docs/reference/reference.md:404 —
cg_keepalive_refused_totalis not manual-hold-only. The counter moves for every too-large refusal, including requests on accounts with automatic keep-alive and no Ping-button opt-in. The HELP text on the series says so. The reference section title ("Manual-hold series") and the data-dependence row ("The manual holds also export ... too-large refusals") do not. A reader of the docs alone will read the series too narrow. - PR body vs code — "once per tenant each time the large-body count reaches 2" is not exact. When the byte cap evicts one large body and a new large body starts, the count reaches 2 again and no line fires:
pairis cleared only when a record sees fewer than 2 large bodies. The code comment ("a replaced thread or a third body does not log again") states the real rule, and the rule is sound. The PR wording overstates. - Test gaps, small. No test that a too-large refusal without
ManualHoldstill movescg_keepalive_refused_total(the HELP promises it). No test for thepairreset path: the line fires again after the count falls below 2 and later reaches 2 again.
What I checked
- Read the full diff (
fa25367e..d008b18f) and the code around every change:Record(keeper.go:775-1067),evictManualLockedandexpireManual(manualhold.go),manualmetrics.go,renderMetricsand the new block (promexport.go:744-785). - Locking: every
k.mmaccess runs underk.mu—tooLargein Record, both counters inevictManualLocked(called from Record with the lock held),recordManualHoldLocked(same), andManualHoldSnapshot.Recorddoes not holdk.muat thetooLargeincrement, so no self-deadlock.renderMetricstakes no lock that the keeper holds. - Histogram math: buckets are per-range, the export makes them cumulative,
+InfusesSizeCount, and the 6th cell catches bodies over 32 MiB (possible whenPolicy.MaxBodyBytesgoes up toMaxBytes). Boundary cases (exactly 1 MiB, exactly 32 MiB) land in the closed upper bucket. - Eviction counters: the loop counts one reason per evicted entry,
countwins when both caps are over, and only per-tenant-cap evictions count — which is what the reference doc promises. Global-cap evictions throughevictLockedcount nowhere; the docs do not claim they do. - The gauge: read live from
k.liveunder the lock, per tenant, sorted, only tenants that hold something. Label lookup copies thecg_tenant_refused_requests_totalpattern (registry().List(),escapeLabel). - Hosted exposure:
metricsAllowedallows the token or a direct loopback peer whenTenants != nil, so the reference claim about the per-tenant gauge holds. - Tests: read all new tests and the helpers they use (
testKeeper,recordHold,holdOnce,padBody,tenantKey,newHostedFixture,register). Traced each assertion through the code: the 17-hold count eviction, the 2 x 20 MiB byte eviction (prices set so the 32 MiB body limit applies), the expiry path, the too-large refusal, the bucket placement, the once-only log, and the/metricsrendering with tenant and label. - Data dependence: the row file and the generated page carry the same added sentence; the PR states metadata-only and no dependence change. The metrics carry counts, sizes and tenant ids only — no body, key or credential. The log line carries tenant id, sizes and the cap.
- Process: the PR names the behavior change at the top (4 read-only series, one WARN line) and asks for the owner's yes. The commit carries
Signed-off-byandAssisted-By. Live tests were not run (not approved); this is a code-only review.
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.
Behavior change (needs the owner's yes before merge)
/metricsseries: 4 families, listed below. They are read-only.Summary
Refs #1103
What changed
New series:
cg_keepalive_manual_hold_evicted_total{reason="bytes"|"count"}. Counts each eviction by the per-tenant cap.countwins when both caps are over.cg_keepalive_refused_total{reason="too_large"}. Counts a body over the per-body limit. No such counter existed. The Ping button reason was the only trace.cg_keepalive_manual_hold_body_bytes(histogram). Body size when a hold starts. Buckets: 1, 4, 8, 16, 32 MiB.cg_tenant_keepalive_manual_hold_bytes{tenant,label}(gauge). Bytes the tenant holds now. Only tenants that hold something appear.Log line: WARN, once per tenant each time the large-body count reaches 2. It logs the tenant id, the body sizes and the cap. No content.
Hosted:
cg_keepalive_*series. They have no tenant label.cg_tenant_refused_requests_total:tenantandlabelfrom the registry./metricsis open to loopback and theMETRICS_TOKENbearer only.Code:
keepalivelibrary (keepalive/manualmetrics.go).Keeper.ManualHoldSnapshot()returns the numbers.proxy/promexport.go).Docs:
docs/reference/reference.md(new section) and theKeep-alive pingsdata-dependence row (regenerated).Data dependence
Metadata only. Counts, byte sizes and tenant ids. No body, key or credential. It works the same at every storage level (off, metadata only, everything). No change in data dependence.
How it was tested
countincrement inevictManualLockedfailsTestManualHoldEvictionByCountMovesTheCountCounter.go test -racepasses for./keepalive/...,./metrics/...,./config/...(includes the doc-drift test) and./proxy/...(in two halves, includes the metrics-coverage test).go vet ./...andgofmt -lare clean.go run ./cmd/gen-datadeprun; the generated page is committed.Assisted-By: Claude Opus 5.5 [email protected]