Class InMemoryMetricsRecorder

java.lang.Object
com.fayupable.logged.test.InMemoryMetricsRecorder
All Implemented Interfaces:
MetricsRecorder

public final class InMemoryMetricsRecorder extends Object implements MetricsRecorder
A MetricsRecorder that records every call it is asked to record in memory as a RecordedMetric, instead of forwarding it to a real metrics backend such as Micrometer.

Unlike InMemoryInvocationEventEmitter, which mirrors a sampled, threshold-gated emission, this recorder always receives every invocation, matching MetricsRecorder's own contract; a test asserting on metrics does not need to also configure sampling.

Backed by a CopyOnWriteArrayList for the same reason as InMemoryInvocationEventEmitter: recording may happen from more than one thread when the method under test hands work off to an executor.

  • Constructor Details

    • InMemoryMetricsRecorder

      public InMemoryMetricsRecorder()
  • Method Details

    • record

      public void record(String className, String methodName, long durationNanos, boolean success, String exceptionType)
      Description copied from interface: MetricsRecorder
      Records the outcome and duration of a single method invocation.
      Specified by:
      record in interface MetricsRecorder
      Parameters:
      className - the simple or fully qualified name of the class declaring the invoked method
      methodName - the name of the invoked method
      durationNanos - the wall-clock duration of the invocation, in nanoseconds
      success - true if the method returned normally, false if it threw an exception
      exceptionType - the simple class name of the exception thrown by the method, or null if the call succeeded
    • metrics

      public List<RecordedMetric> metrics()
      Returns every metric recorded so far, in recording order.
      Returns:
      an immutable snapshot of the recorded metrics
    • count

      public int count()
      Returns the number of metrics recorded so far.
      Returns:
      the recorded metric count
    • lastMetric

      public RecordedMetric lastMetric()
      Returns the most recently recorded metric.
      Returns:
      the last recorded metric
      Throws:
      IllegalStateException - if no metric has been recorded yet
    • clear

      public void clear()
      Discards every metric recorded so far, so this instance can be reused across test methods without leaking state between them.