Class SlidingWindowStatistics
java.lang.Object
org.j7m.middleware.metrics.SlidingWindowStatistics
Thread-safe bounded sliding-window statistics collector.
The collector stores only the most recent capacity events and
aggregate counters. It is intended for runtime diagnostics and HP scoring,
not durable accounting.
-
Constructor Summary
ConstructorsConstructorDescriptionSlidingWindowStatistics(int capacity) Creates a bounded statistics window.SlidingWindowStatistics(int capacity, Duration retentionDuration, LongSupplier clock) Creates a bounded statistics window with time-based retention.SlidingWindowStatistics(int capacity, Duration retentionDuration, LongSupplier clock, int maxCounters) Creates a bounded statistics window with time and counter-cardinality limits. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a delta to one named counter.voidIncrements one named counter.voidrecord(String key, long timestampMillis, long latencyNanos, boolean success, org.j7m.middleware.api.MiddlewareErrorCode errorCode) Records one timed event.snapshot()Returns a point-in-time immutable snapshot.doubleReturns success rate for recent aggregate counters.
-
Constructor Details
-
SlidingWindowStatistics
public SlidingWindowStatistics(int capacity) Creates a bounded statistics window.- Parameters:
capacity- maximum retained events
-
SlidingWindowStatistics
Creates a bounded statistics window with time-based retention.The supplied clock is used only for stale-event eviction during
snapshot()andsuccessRate(). Recorded event timestamps remain caller supplied so deterministic runtimes and tests can use an injected clock without relying on wall-clock time.- Parameters:
capacity- maximum retained eventsretentionDuration- maximum age of retained events; non-positive values retain events for at least one millisecondclock- clock returning the current wall-clock timestamp in milliseconds
-
SlidingWindowStatistics
public SlidingWindowStatistics(int capacity, Duration retentionDuration, LongSupplier clock, int maxCounters) Creates a bounded statistics window with time and counter-cardinality limits.- Parameters:
capacity- maximum retained eventsretentionDuration- maximum age of retained events; non-positive values retain events for at least one millisecondclock- clock returning the current wall-clock timestamp in millisecondsmaxCounters- maximum named counters retained by this collector
-
-
Method Details
-
record
public void record(String key, long timestampMillis, long latencyNanos, boolean success, org.j7m.middleware.api.MiddlewareErrorCode errorCode) Records one timed event.- Parameters:
key- event key, such as service or object idtimestampMillis- event timestamplatencyNanos- latency in nanosecondssuccess- whether the event succeedederrorCode- optional error code
-
increment
-
add
Adds a delta to one named counter.- Parameters:
name- counter namedelta- counter delta
-
snapshot
Returns a point-in-time immutable snapshot.- Returns:
- statistics snapshot
-
successRate
public double successRate()Returns success rate for recent aggregate counters.- Returns:
- success rate in
0.0..1.0
-