Class SlidingWindowStatistics

java.lang.Object
org.j7m.middleware.metrics.SlidingWindowStatistics

public final class SlidingWindowStatistics extends Object
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 Details

    • SlidingWindowStatistics

      public SlidingWindowStatistics(int capacity)
      Creates a bounded statistics window.
      Parameters:
      capacity - maximum retained events
    • SlidingWindowStatistics

      public SlidingWindowStatistics(int capacity, Duration retentionDuration, LongSupplier clock)
      Creates a bounded statistics window with time-based retention.

      The supplied clock is used only for stale-event eviction during snapshot() and successRate(). 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 events
      retentionDuration - maximum age of retained events; non-positive values retain events for at least one millisecond
      clock - 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 events
      retentionDuration - maximum age of retained events; non-positive values retain events for at least one millisecond
      clock - clock returning the current wall-clock timestamp in milliseconds
      maxCounters - 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 id
      timestampMillis - event timestamp
      latencyNanos - latency in nanoseconds
      success - whether the event succeeded
      errorCode - optional error code
    • increment

      public void increment(String name)
      Increments one named counter.
      Parameters:
      name - counter name
    • add

      public void add(String name, long delta)
      Adds a delta to one named counter.
      Parameters:
      name - counter name
      delta - counter delta
    • snapshot

      public StatisticsSnapshot 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