Class BatchMetrics

java.lang.Object
org.springframework.batch.core.observability.BatchMetrics

public final class BatchMetrics extends Object
Central class for batch metrics. It provides:
  • the main entry point to interact with Micrometer's API to create common metrics such as Timer and LongTaskTimer.
  • Some utility methods like calculating durations and formatting them in a human readable format.
Only intended for internal use.
Since:
4.2
Author:
Mahmoud Ben Hassine, Glenn Renfro
  • Field Details

  • Method Details

    • createTimer

      public static io.micrometer.core.instrument.Timer createTimer(io.micrometer.core.instrument.MeterRegistry meterRegistry, String name, String description, io.micrometer.core.instrument.Tag... tags)
      Create a Timer.
      Parameters:
      meterRegistry - the meter registry to use
      name - of the timer. Will be prefixed with METRICS_PREFIX.
      description - of the timer
      tags - of the timer
      Returns:
      a new timer instance
    • createCounter

      public static io.micrometer.core.instrument.Counter createCounter(io.micrometer.core.instrument.MeterRegistry meterRegistry, String name, String description, io.micrometer.core.instrument.Tag... tags)
      Create a Counter.
      Parameters:
      meterRegistry - the meter registry to use
      name - of the counter. Will be prefixed with METRICS_PREFIX.
      description - of the counter
      tags - of the counter
      Returns:
      a new timer instance
    • createObservation

      public static io.micrometer.observation.Observation createObservation(String name, BatchJobContext context, io.micrometer.observation.ObservationRegistry observationRegistry)
      Create a new Observation. It's not started, you must explicitly call Observation.start() to start it.

      Remember to register the DefaultMeterObservationHandler via the Metrics.globalRegistry.withTimerObservationHandler() in the user code. Otherwise you won't observe any metrics.

      Parameters:
      name - of the observation
      context - of the batch job observation
      Returns:
      a new observation instance
      Since:
      5.0
    • createObservation

      public static io.micrometer.observation.Observation createObservation(String name, BatchStepContext context, io.micrometer.observation.ObservationRegistry observationRegistry)
      Create a new Observation. It's not started, you must explicitly call Observation.start() to start it.

      Remember to register the DefaultMeterObservationHandler via the Metrics.globalRegistry.withTimerObservationHandler() in the user code. Otherwise you won't observe any metrics.

      Parameters:
      name - of the observation
      context - of the observation step context
      Returns:
      a new observation instance
      Since:
      5.0
    • createTimerSample

      public static io.micrometer.core.instrument.Timer.Sample createTimerSample(io.micrometer.core.instrument.MeterRegistry meterRegistry)
      Create a new Timer.Sample.
      Parameters:
      meterRegistry - the meter registry to use
      Returns:
      a new timer sample instance
    • createLongTaskTimer

      public static io.micrometer.core.instrument.LongTaskTimer createLongTaskTimer(io.micrometer.core.instrument.MeterRegistry meterRegistry, String name, String description, io.micrometer.core.instrument.Tag... tags)
      Create a new LongTaskTimer.
      Parameters:
      meterRegistry - the meter registry to use
      name - of the long task timer. Will be prefixed with METRICS_PREFIX.
      description - of the long task timer.
      tags - of the timer
      Returns:
      a new long task timer instance
    • calculateDuration

      @Nullable public static Duration calculateDuration(@Nullable LocalDateTime startTime, @Nullable LocalDateTime endTime)
      Calculate the duration between two dates.
      Parameters:
      startTime - the start time
      endTime - the end time
      Returns:
      the duration between start time and end time
    • formatDuration

      public static String formatDuration(@Nullable Duration duration)
      Format a duration in a human readable format like: 2h32m15s10ms.
      Parameters:
      duration - to format
      Returns:
      A human readable duration