Class MicrometerMetricsRecorder

java.lang.Object
com.fayupable.logged.spring.metrics.MicrometerMetricsRecorder
All Implemented Interfaces:
MetricsRecorder

public class MicrometerMetricsRecorder extends Object implements MetricsRecorder
MetricsRecorder implementation backed by Micrometer.

Every invocation is recorded against three meters:

  • method.invocations: a counter tagged with class, method, and outcome (success or error), tracking how many times a method was called.
  • method.duration: a timer tagged with class and method, tracking how long calls took, with a percentile histogram published for latency dashboards.
  • method.errors: a counter tagged with class, method, and exception, registered only when an invocation fails, tracking which exceptions occur and how often.

All tags are bounded to a small, fixed set of low-cardinality values derived from the invoked method itself. Method arguments and return values are never used as tags, since they could otherwise produce an unbounded number of time series and degrade the metrics backend.

Meters are looked up once per distinct tag combination and cached afterward. MeterRegistry's own register call is not prohibitively expensive, but it still allocates a Meter.Id and performs a map lookup on every call; caching avoids repeating that work on every single invocation of a hot @Logged method.

  • Constructor Details

    • MicrometerMetricsRecorder

      public MicrometerMetricsRecorder(io.micrometer.core.instrument.MeterRegistry meterRegistry)
  • Method Details

    • record

      public void record(String className, String methodName, long durationNanos, boolean success, String exceptionType)
      Records the given invocation against the invocation counter, the duration timer, and, if the call failed, the error counter.
      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