Package com.fayupable.logged.test
Class InMemoryMetricsRecorder
java.lang.Object
com.fayupable.logged.test.InMemoryMetricsRecorder
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Discards every metric recorded so far, so this instance can be reused across test methods without leaking state between them.intcount()Returns the number of metrics recorded so far.Returns the most recently recorded metric.metrics()Returns every metric recorded so far, in recording order.voidrecord(String className, String methodName, long durationNanos, boolean success, String exceptionType) Records the outcome and duration of a single method invocation.
-
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:MetricsRecorderRecords the outcome and duration of a single method invocation.- Specified by:
recordin interfaceMetricsRecorder- Parameters:
className- the simple or fully qualified name of the class declaring the invoked methodmethodName- the name of the invoked methoddurationNanos- the wall-clock duration of the invocation, in nanosecondssuccess-trueif the method returned normally,falseif it threw an exceptionexceptionType- the simple class name of the exception thrown by the method, ornullif the call succeeded
-
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
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.
-