fix(metrics): stop double-counting upsert-creates and counting no-ops as engram writes - #887
Open
johanneshauer wants to merge 1 commit into
Open
Conversation
… as engram writes muninndb_engine_writes_total is documented as "Total number of engrams written". Two paths broke that, both introduced together in scrypster#556 when the UPSERT write mode was added: the new branches were instrumented without noticing that the path they delegate to already instruments itself. upsertCreate does not write anything itself -- it delegates to e.Write(), which increments EngineWritesTotal, records the latency sample and observes WriteDuration. upsertCreate repeated all three, so one upsert-create produced two increments, two latency samples and two duration observations for a single write. The duplicated block is removed; the delegated Write remains the single point of instrumentation. The identical-content branch is a documented no-op ("no write, no evolve, no index churn") yet incremented the counter before returning the existing id. Only the increment is removed there -- the latency observations are kept deliberately, because a call was served and that is the only place this fast path is measured. The two hunks differ for that reason: at upsertCreate the latency data duplicates the same work, whereas on the no-op path it is the sole observation of a distinct operation. The upsert-evolve branch keeps its increment: evolveAtInternal does not increment, so that site is the only counter for an evolve. A test guards it. Tests: five delta-based tests over the write paths. Two of them fail against the unfixed code (delta = 2 for upsert-create, delta = 1 for the no-op); the other three pass both before and after, which shows the change is narrow. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
Author
|
CI note: the one red job ( |
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.
fix(metrics): stop double-counting upsert-creates and counting no-ops as engram writes
muninndb_engine_writes_totalis documented as "Total number of engrams written".Two paths break that, both introduced together in #556 when the UPSERT write mode
was added — the new branches were instrumented without noticing that the path they
delegate to already instruments itself.
1. Upsert-create is counted twice
upsertCreatedoes not write anything itself. It delegates:Writethen incrementsEngineWritesTotal, records the latency sample and observesWriteDuration.upsertCreaterepeats all three at the end of the same call, soone upsert-create produces two increments, two latency samples and two duration
observations for a single write.
Fixed by removing the duplicated block; the delegated
Writeremains the singlepoint of instrumentation for that write.
2. The identical-content no-op is counted as a write
The branch whose own comment reads:
increments
EngineWritesTotalbefore returning the existing id. No engram iswritten, so the counter moves for something that by its own description is not a
write.
Fixed by removing only the increment. The latency observations there are kept
deliberately — a call was served, and that is the only place this fast path is
measured. The two changes differ for that reason: at
upsertCreatethe latency datais a duplicate of the same work, whereas on the no-op path it is the sole
observation of a distinct operation.
What is deliberately left alone
evolveAtInternaldoes notincrement, so that site is the only counter for an evolve. A test guards it.
RememberTreestill does not count. It builds nodes withe.store.NewBatch()+batch.WriteEngram(...)and never routes throughWrite/WriteBatch, so tree nodes are absent from this counter entirely. Out ofscope here, but worth knowing — the help text reads as though it covers them.
Happy to fold them in, or narrow the help text, in a follow-up if you have a
preference.
If you disagree on the no-op
Counting a served no-op as request volume is a defensible thing to want; it is
just not what this counter's name and help text promise. If you would rather keep
that visibility, say so and I will drop that hunk and add a separate
muninndb_engine_upsert_noop_totalinstead — the double-count fix stands on its owneither way.
Tests
internal/engine/writes_total_accounting_test.go, five tests, all deltas (thecounter is process-global):
delta = 2delta = 1Verified against
developat34b505f:gofmtclean,go vetclean, full modulego test ./...51 ok / 0 FAIL,go test -race ./internal/engine/clean with 0data races. The three non-bug tests pass both before and after the fix, which is
what shows the change is narrow.