Skip to content

Add cache hit/miss + bytes-from-cache metrics to CacheEngine #418

Closed

Description

@thiras

Goal

Surface cache hit/miss and bytes-from-cache as decdn_cache_* metrics so operators (and downstream features like decdn node top, #275) can reason about cache behaviour without log scraping.

Background

The cache crate (crates/cache/) currently emits zero metrics. The single read path is CacheEngine::get(&self, hash: Hash) -> CacheResult<Bytes> at crates/cache/src/engine.rs:663 — that's where hit vs. miss is decided and where Bytes flows back to the caller. Node-side metrics live in crates/node/src/metrics.rs (DecdnMetrics, registered against iroh_metrics::Registry).

This issue is a hard prerequisite for #275 (decdn node top), which assumes hit-rate is observable.

Scope

Add the following counters, all in the decdn_cache_* family per ADR 020 naming:

  • decdn_cache_hits_total — incremented when get finds the blob locally.
  • decdn_cache_misses_total — incremented when get triggers an origin pull-through (or returns NotFound if no origin is configured).
  • decdn_cache_bytes_returned_total — bytes returned from get to the caller (bytes.len() on success).
  • decdn_cache_pull_through_bytes_total — bytes fetched from origin during a miss. Independent signal from bytes_returned: useful for distinguishing 'serving from cache' vs. 'paying origin egress'.

Out of scope:

Design sketch (not prescriptive)

The decdn/CLAUDE.md leaf-crate rule means decdn-cache must not depend on decdn-node. Two reasonable shapes:

  1. Cache owns its MetricsGroup. Add iroh-metrics to decdn-cache deps, define pub struct CacheMetrics in cache, and let node register it via Registry::register — mirroring how node::Metrics::register_iroh_endpoint registers iroh's own metrics today (crates/node/src/metrics.rs:110). Recommended.
  2. Observer trait. Cache defines trait CacheObserver { fn on_hit(...); fn on_miss(...); ... }; node provides an impl wired to its metrics. More indirection; useful only if a non-metrics observer also wants the events.

(1) is closer to the existing pattern and easier to test in isolation.

Test plan

  • Unit test: open a CacheEngine, put a blob, call get for present + absent hashes, assert hit/miss counters move correctly. The cache crate's existing tests in engine.rs already cover the underlying behavior — these additions are pure observability.
  • Integration: ensure node registers the cache metrics and they appear in /metrics output (extend crates/node/tests/ or admin tests).

Acceptance

  • All four counters defined and exported from decdn-cache.
  • Counters increment from inside CacheEngine::get and the pull-through path; no caller (node, tests) needs to manually call them.
  • decdn-node registers the cache metrics group; counters appear in /metrics output with decdn_cache_ prefix.
  • Tests cover hit, miss, and pull-through paths.
  • No new dependency from decdn-cache on decdn-node (leaf-crate rule).

Refs: #275 (consumer), #317 (network-side bytes-served counter, separate concern).

Activity

  1. added
    kind:feature
    rust
    on May 6, 2026
  2. self-assigned this
    on May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind:featurerust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions