ArrayIndexOutOfBoundsException when using LongTaskTimer (original) (raw)

Describe the bug / it may be a question
I'm seeing ArrayIndexOutOfBoundsException in my logs when using LongTaskTimer.

Environment

To Reproduce

@Test
void reproduceIssue() throws InterruptedException {
    SimpleMeterRegistry simpleMeterRegistry = new SimpleMeterRegistry();

    LongTaskTimer timer = LongTaskTimer.builder("jobrunr.jobs")
            .publishPercentiles(0.25, 0.5, 0.75, 0.8, 0.9, 0.95)
            .publishPercentileHistogram()
            .register(simpleMeterRegistry);
    LongTaskTimer.Sample start = timer.start();
    Thread.sleep(3640);
    System.out.println(simpleMeterRegistry.getMetersAsString());
    Thread.sleep(2640);
    System.out.println(start.stop());

    System.out.println(simpleMeterRegistry.getMetersAsString());
}

This results in the following output:

08:46:23.327 [Test worker] WARN  i.m.c.i.internal.DefaultGauge - Failed to apply the value function for the gauge 'jobrunr.jobs.percentile'. Note that subsequent logs will be logged at debug level.
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
    at io.micrometer.core.instrument.distribution.HistogramGauges.lambda$new$9(HistogramGauges.java:119)
    at io.micrometer.core.instrument.internal.DefaultGauge.value(DefaultGauge.java:53)
    at io.micrometer.core.instrument.Measurement.getValue(Measurement.java:54)
    at io.micrometer.core.instrument.simple.SimpleMeterRegistry.toString(SimpleMeterRegistry.java:206)
    at io.micrometer.core.instrument.simple.SimpleMeterRegistry.lambda$toString$1(SimpleMeterRegistry.java:195)
jobrunr.jobs(LONG_TASK_TIMER)[]; active_tasks=1.0, duration=3.651603291 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.25']; value=3.653785708 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.9']; value=3.653818208 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.8']; value=3.653818208 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.5']; value=3.653818208 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.95']; value=NaN seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.75']; value=3.653818208 seconds
6322804041
jobrunr.jobs(LONG_TASK_TIMER)[]; active_tasks=0.0, duration=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.25']; value=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.9']; value=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.8']; value=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.5']; value=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.95']; value=0.0 seconds
jobrunr.jobs.percentile(GAUGE)[phi='0.75']; value=0.0 seconds

Expected behavior
No ArrayIndexOutOfBoundsException should happen.

Additional context
NA