Skip to content

refactor: pass ModuleCacheMetrics as Arc instead of process-global static #4488

Description

@iduartgomez

Background

PR #4472 introduced MODULE_CACHE_METRICS as a process-global static to share metrics between two components that live on opposite sides of an architectural boundary:

  • Writers (ModuleCache::insert / evict_to_budget / remove) — owned by RuntimePool, behind the contract-handler channel.
  • Reader (ring.rs:1074) — the Ring snapshot task, which has no channel or reference to RuntimePool.

This mirrors the existing TRANSPORT_METRICS static and is correct given the single-RuntimePool-per-process production invariant. The static was the right call for the PR scope.

Problem

Process-global statics make test isolation harder: labeled caches in different unit tests share the same gauge pair, so parallel tests can observe each other's metric state. This is the same problem class that TRANSPORT_METRICS has.

Proposed refactor

Replace the static with an Arc<ModuleCacheMetrics> threaded through the call graph:

  1. Construct Arc<ModuleCacheMetrics> at node startup (in Node::build or equivalent).
  2. Pass it into RuntimePool::new → stored on each ModuleCache via ModuleCache::with_label.
  3. Store the same Arc in RouterSnapshotInfo or Ring so the snapshot task can call .snapshot() from it.

Estimated scope: ~4 function signature changes (RuntimePool::new, ModuleCache::with_label, snapshot-state construction, and one or two call sites).

Acceptance criteria

  • MODULE_CACHE_METRICS static removed.
  • ModuleCacheMetrics constructed once and threaded as Arc through RuntimePool and the Ring snapshot path.
  • Existing metric values / Prometheus labels unchanged (no dashboard breakage).
  • Unit tests for ModuleCache create isolated metric instances without cross-talk.

References

Metadata

Metadata

Assignees

Labels

A-developer-xpArea: developer experienceE-mediumExperience needed to fix/implement: Medium / intermediateT-enhancementType: Improvement to existing functionality

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions