Class BatchMetrics
java.lang.Object
org.springframework.batch.core.observability.BatchMetrics
Central class for batch metrics. It provides:
- the main entry point to interact with Micrometer's API to create common metrics
such as
Timer
andLongTaskTimer
. - Some utility methods like calculating durations and formatting them in a human readable format.
- Since:
- 4.2
- Author:
- Mahmoud Ben Hassine, Glenn Renfro
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Duration
calculateDuration
(LocalDateTime startTime, LocalDateTime endTime) Calculate the duration between two dates.static io.micrometer.core.instrument.Counter
createCounter
(io.micrometer.core.instrument.MeterRegistry meterRegistry, String name, String description, io.micrometer.core.instrument.Tag... tags) Create aCounter
.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 newLongTaskTimer
.static io.micrometer.observation.Observation
createObservation
(String name, BatchJobContext context, io.micrometer.observation.ObservationRegistry observationRegistry) Create a newObservation
.static io.micrometer.observation.Observation
createObservation
(String name, BatchStepContext context, io.micrometer.observation.ObservationRegistry observationRegistry) Create a newObservation
.static io.micrometer.core.instrument.Timer
createTimer
(io.micrometer.core.instrument.MeterRegistry meterRegistry, String name, String description, io.micrometer.core.instrument.Tag... tags) Create aTimer
.static io.micrometer.core.instrument.Timer.Sample
createTimerSample
(io.micrometer.core.instrument.MeterRegistry meterRegistry) Create a newTimer.Sample
.static String
formatDuration
(Duration duration) Format a duration in a human readable format like: 2h32m15s10ms.
-
Field Details
-
METRICS_PREFIX
- See Also:
-
STATUS_SUCCESS
- See Also:
-
STATUS_FAILURE
- See Also:
-
-
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 aTimer
.- Parameters:
meterRegistry
- the meter registry to usename
- of the timer. Will be prefixed withMETRICS_PREFIX
.description
- of the timertags
- 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 aCounter
.- Parameters:
meterRegistry
- the meter registry to usename
- of the counter. Will be prefixed withMETRICS_PREFIX
.description
- of the countertags
- 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 newObservation
. It's not started, you must explicitly callObservation.start()
to start it.Remember to register the
DefaultMeterObservationHandler
via theMetrics.globalRegistry.withTimerObservationHandler()
in the user code. Otherwise you won't observe any metrics.- Parameters:
name
- of the observationcontext
- 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 newObservation
. It's not started, you must explicitly callObservation.start()
to start it.Remember to register the
DefaultMeterObservationHandler
via theMetrics.globalRegistry.withTimerObservationHandler()
in the user code. Otherwise you won't observe any metrics.- Parameters:
name
- of the observationcontext
- 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 newTimer.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 newLongTaskTimer
.- Parameters:
meterRegistry
- the meter registry to usename
- of the long task timer. Will be prefixed withMETRICS_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 timeendTime
- the end time- Returns:
- the duration between start time and end time
-
formatDuration
Format a duration in a human readable format like: 2h32m15s10ms.- Parameters:
duration
- to format- Returns:
- A human readable duration
-