Skip to content

feat(controller): emit result-tagged cache-lookup counters#241

Merged
xytan0056 merged 1 commit into
mainfrom
feat/cache-lookup-metrics
Jul 25, 2026
Merged

feat(controller): emit result-tagged cache-lookup counters#241
xytan0056 merged 1 commit into
mainfrom
feat/cache-lookup-metrics

Conversation

@xytan0056

@xytan0056 xytan0056 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Replace the hit-only cache_hit counters with result-tagged cache-lookup counters at every controller cache-lookup site, so a per-layer hit rate (hit / (hit + miss)) is derivable for each cache.

Three counters, each emitted under its parent RPC op with a result=hit|miss tag:

metric emitted from
treehash_cache_lookup get_target_graph, get_changed_targets
graph_cache_lookup get_target_graph
compared_targets_cache_lookup get_changed_targets

Classification

The result is classified by the lookup error's origin: a not-found is a genuine miss; an infra failure is not a cache miss (it's already tracked by the failure metric) and is left out so it can't skew the hit rate.

site not-found infra error
treehash miss nothing (request fails)
graph miss nothing (request fails)
compared-targets miss nothing (recomputes)

Breaking: the bare controller.get_target_graph.cache_hit and controller.get_changed_targets.cache_hit counters are removed.

Stacked on refactor/share-metrics-buckets (#240) — review/merge that first.

@xytan0056
xytan0056 requested review from a team as code owners July 24, 2026 20:03
@xytan0056
xytan0056 force-pushed the feat/cache-lookup-metrics branch 2 times, most recently from 81a02eb to 95364c0 Compare July 24, 2026 21:27
Comment thread controller/gettargetgraph.go Outdated
// Download the target graph based on treehash.
storageStart := time.Now()
graphReader, err := storage.NewGraphReader(ctx, c.storage, treehashPath)
if err != nil && ctx.Err() != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think you need to check err != nil. No matter what if ctx.Err() I= nil we want to set err = ctx.Err()

Comment thread controller/metrics.go Outdated
// recordCacheLookup emits a result-tagged counter for a cache lookup under the
// given parent op: hit when the payload was usable, miss otherwise (not-found
// or a storage failure both mean the payload must be recomputed).
func recordCacheLookup(e *metrics.Emitter, parentOp, name string, hit bool) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this take in err instead of hit so you can check if err == nil here instead of doing it at all the call sites?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only care if it's a notfounderror? Any other error I don't think we should emit this metric because it's an infra failure then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if err == nil here
that's what I did initially--passing the error in, but there are cases when there's no error to pass like L196

recordCacheLookup(e, opGetChangedTargets, metricComparedTargetsCacheLookup, true)

I can pass in the error itself here

Comment thread controller/metrics.go Outdated
Comment thread controller/gettargetgraph.go Outdated
if err != nil && ctx.Err() != nil {
err = ctx.Err()
}
recordCacheLookup(e, opGetTargetGraph, metricGraphCacheLookup, err == nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we should do a check for if err == nil { cacheHit = true }
And then pass that into recordCacheLookup(e, opGetTargetGraph, metricGraphCacheLookup, cacheHit) for readability purposes. It's hard to tell why err == nil is passed into the function param of hit.
Also I think we should only emit this metric if storage.IsNotFound(err) in line 134 and line 136 separately. If it's neither a NotFoundError or err == nil, should it even emit a cache hit/miss metric?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense. originally I was thinking all errors should be cache miss, but now i'm thinking cache hit should be a effectiveness meassurement not an outcome. The cachehit metrics should be honest here.
The only downside is hits + misses ≠ total attempts

@xytan0056
xytan0056 force-pushed the feat/cache-lookup-metrics branch 3 times, most recently from e267415 to 977eef4 Compare July 24, 2026 22:57
@xytan0056
xytan0056 changed the base branch from refactor/share-metrics-buckets to main July 24, 2026 23:47
Replace the hit-only `cache_hit` counters with `result`-tagged lookup
counters at every controller cache-lookup site, so a per-layer hit rate
(hit / (hit + miss)) is derivable for each cache.

Three counters are emitted under their parent RPC op:
- treehash_cache_lookup          (get_target_graph, get_changed_targets)
- graph_cache_lookup             (get_target_graph)
- compared_targets_cache_lookup  (get_changed_targets)

recordCacheLookup classifies the lookup error: a nil error is a hit, a
not-found error is a miss, and any other error is an infra failure that
emits nothing (it is already tracked by the failure metric and must not
skew the hit rate). In get_changed_targets the treehash lookup emits once
per revision (from inside readTreehash); the write-path treehash reads
that only build the cache key pass a no-op emitter so they do not skew
the hit rate.

BREAKING CHANGE: the bare `controller.get_target_graph.cache_hit` and
`controller.get_changed_targets.cache_hit` counters are removed. Compute
hit rate from the new result-tagged counters instead.
@xytan0056
xytan0056 force-pushed the feat/cache-lookup-metrics branch from 977eef4 to 4eaa916 Compare July 24, 2026 23:49
@xytan0056
xytan0056 merged commit 6bf2a94 into main Jul 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants