Skip to content

Commit

Permalink
For long task timers fixes the percentiles above interpolatable line
Browse files Browse the repository at this point in the history
without this change for the provided test one of the histogram buckets gets removed and an ArrayIndexOutOfBoundsException is thrown
with this change the exception is not thrown anymore

fixes gh-3877
  • Loading branch information
marcingrzejszczak committed Dec 27, 2023
1 parent 15755a1 commit 421ff5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public HistogramSnapshot takeSnapshot() {
CountAtBucket[] countAtBucketsArr = new CountAtBucket[0];

List<Double> percentilesAboveInterpolatableLine = percentilesRequested.stream()
.filter(p -> p * (activeTasks.size() + 1) > activeTasks.size())
.filter(p -> p * (activeTasks.size() + 1) >= activeTasks.size())
.collect(Collectors.toList());

percentilesRequested.removeAll(percentilesAboveInterpolatableLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,35 @@
import io.micrometer.core.instrument.internal.DefaultLongTaskTimer;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;

import java.io.PrintStream;

import org.junit.jupiter.api.*;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static io.micrometer.core.instrument.MockClock.clock;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

public class DefaultLongTaskTimerTest {

final ByteArrayOutputStream myErr = new ByteArrayOutputStream();

@BeforeEach
void setup() {
System.setErr(new PrintStream(myErr));
}

@AfterAll
static void cleanup() {
System.setErr(System.err);
}

@Test
@DisplayName("supports sending histograms of active task duration")
void histogram() {
Expand Down Expand Up @@ -97,4 +113,24 @@ protected LongTaskTimer newLongTaskTimer(Meter.Id id,
}
}

@Test
void histogramToStringNotThrowingException() 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(10);

assertThatNoException().isThrownBy(() -> {
simpleMeterRegistry.getMetersAsString();
start.stop();
simpleMeterRegistry.getMetersAsString();
String standardOutput = myErr.toString();
assertThat(standardOutput).doesNotContain("ArrayIndexOutOfBoundsException");
});
}

}

0 comments on commit 421ff5b

Please sign in to comment.