Package com.tnsai.resilience
Class ResilienceMetrics
java.lang.Object
com.tnsai.resilience.ResilienceMetrics
In-memory metrics registry for
@Metered actions
(issue #173, Phase 1).
Records per-metric-name counters (total, success, failure) and
an optional latency histogram. Tests can snapshot any metric via
snapshot(java.lang.String); production code reads aggregates via the same
API. Phase 2 ships a MetricsSink SPI that lets consumers
route the same emissions to Micrometer / Prometheus / OpenTelemetry
without changing this class's contract.
Why in-memory in Phase 1
The Phase-1 boundary across this sprint (#171 / #172 / #177 / #173) is "no external storage / no distributed-state SPI". An in-memory registry keyed by metric name is correct for single-instance deployments and safe for tests; multi-node aggregation needs the Phase-2 sink SPI to fan out.Thread safety
ConcurrentHashMap for the metric map, LongAdder
for hot-path counters (better than AtomicLong under
contention), ReentrantLock
around histogram array mutation. Snapshot reads are point-in-time
— concurrent writes during a snapshot are visible-or-not without
tearing.- Since:
- 0.5.4 (issue #173, Phase 1)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordImmutable snapshot of one metric's state at the momentsnapshot(java.lang.String)was called. -
Method Summary
Modifier and TypeMethodDescriptionstatic ResilienceMetricsSingleton — Phase 1 has no SPI fan-out, one registry per JVM.voidRecords a single action invocation.voidTest hook — drops every recorded metric.Snapshots the current state ofname, or empty if it was never recorded.Snapshots every recorded metric.
-
Method Details
-
getInstance
Singleton — Phase 1 has no SPI fan-out, one registry per JVM. -
record
Records a single action invocation.- Parameters:
name- the metric name (defaults toclass.methodwhen blank)success- whether the action returned normallydurationMs- measured wall-clock duration in millisecondshistogram- whether to bucket the duration into the latency histogram (mirrors@Metered.histogram())
-
snapshot
Snapshots the current state ofname, or empty if it was never recorded. -
snapshotAll
Snapshots every recorded metric. Order: insertion (per ConcurrentHashMap iteration). -
resetForTesting
public void resetForTesting()Test hook — drops every recorded metric. Production code should never call this; tests use it between cases to prevent cross-test contamination.
-