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
Metrics.globalRegistry
with common metrics such asTimer
andLongTaskTimer
. - Some utility methods like calculating durations and formatting them in a human readable format.
- Since:
- 4.2
- Author:
- Mahmoud Ben Hassine
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Duration
calculateDuration
(Date startTime, Date endTime) Calculate the duration between two dates.static io.micrometer.core.instrument.LongTaskTimer
createLongTaskTimer
(String name, String description, io.micrometer.core.instrument.Tag... tags) Create a newLongTaskTimer
.static io.micrometer.observation.Observation
createObservation
(String name, io.micrometer.observation.Observation.Context context) Create a newObservation
.static io.micrometer.core.instrument.Timer
createTimer
(String name, String description, io.micrometer.core.instrument.Tag... tags) Create aTimer
.static io.micrometer.core.instrument.Timer.Sample
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:
-
observationRegistry
public static final io.micrometer.observation.ObservationRegistry observationRegistryGlobalObservationRegistry
. ATimerObservationHandler
is attached to create aTimer
for every finishedObservation
.
-
-
Method Details
-
createTimer
public static io.micrometer.core.instrument.Timer createTimer(String name, String description, io.micrometer.core.instrument.Tag... tags) Create aTimer
.- Parameters:
name
- of the timer. Will be prefixed withMETRICS_PREFIX
.description
- of the timertags
- of the timer- Returns:
- a new timer instance
-
createObservation
public static io.micrometer.observation.Observation createObservation(String name, io.micrometer.observation.Observation.Context context) Create a newObservation
. It's not started, you must explicitly callObservation.start()
to start it. Remember to register theTimerObservationHandler
via theMetrics.globalRegistry.withTimerObservationHandler()
in the user code. Otherwise you won't observe any metrics.- Parameters:
name
- of the observationcontext
- of the observation- Returns:
- a new observation instance
- Since:
- 5.0
-
createTimerSample
public static io.micrometer.core.instrument.Timer.Sample createTimerSample()Create a newTimer.Sample
.- Returns:
- a new timer sample instance
-
createLongTaskTimer
public static io.micrometer.core.instrument.LongTaskTimer createLongTaskTimer(String name, String description, io.micrometer.core.instrument.Tag... tags) Create a newLongTaskTimer
.- Parameters:
name
- 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 Date startTime, @Nullable Date 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
-