Skip to content

Commit 08a6383

Browse files
For long task timers fixes the percentiles above interpolatable line
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
1 parent cd6120e commit 08a6383

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

Diff for: micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultLongTaskTimer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public HistogramSnapshot takeSnapshot() {
141141
CountAtBucket[] countAtBucketsArr = new CountAtBucket[0];
142142

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

147147
percentilesRequested.removeAll(percentilesAboveInterpolatableLine);

Diff for: micrometer-test/src/main/java/io/micrometer/core/tck/DefaultLongTaskTimerTest.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,35 @@
2424
import io.micrometer.core.instrument.internal.DefaultLongTaskTimer;
2525
import io.micrometer.core.instrument.simple.SimpleConfig;
2626
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
27-
import org.junit.jupiter.api.DisplayName;
28-
import org.junit.jupiter.api.Test;
27+
28+
import java.io.ByteArrayOutputStream;
29+
30+
import java.io.PrintStream;
31+
32+
import org.junit.jupiter.api.*;
2933

3034
import java.time.Duration;
3135
import java.util.Arrays;
3236
import java.util.List;
3337
import java.util.concurrent.TimeUnit;
3438

3539
import static io.micrometer.core.instrument.MockClock.clock;
36-
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.*;
3741

3842
public class DefaultLongTaskTimerTest {
3943

44+
final ByteArrayOutputStream myErr = new ByteArrayOutputStream();
45+
46+
@BeforeEach
47+
void setup() {
48+
System.setErr(new PrintStream(myErr));
49+
}
50+
51+
@AfterAll
52+
static void cleanup() {
53+
System.setErr(System.err);
54+
}
55+
4056
@Test
4157
@DisplayName("supports sending histograms of active task duration")
4258
void histogram() {
@@ -97,4 +113,24 @@ protected LongTaskTimer newLongTaskTimer(Meter.Id id,
97113
}
98114
}
99115

116+
@Test
117+
void histogramToStringNotThrowingException() throws InterruptedException {
118+
SimpleMeterRegistry simpleMeterRegistry = new SimpleMeterRegistry();
119+
120+
LongTaskTimer timer = LongTaskTimer.builder("jobrunr.jobs")
121+
.publishPercentiles(0.25, 0.5, 0.75, 0.8, 0.9, 0.95)
122+
.publishPercentileHistogram()
123+
.register(simpleMeterRegistry);
124+
LongTaskTimer.Sample start = timer.start();
125+
Thread.sleep(10);
126+
127+
assertThatNoException().isThrownBy(() -> {
128+
simpleMeterRegistry.getMetersAsString();
129+
start.stop();
130+
simpleMeterRegistry.getMetersAsString();
131+
String standardOutput = myErr.toString();
132+
assertThat(standardOutput).doesNotContain("ArrayIndexOutOfBoundsException");
133+
});
134+
}
135+
100136
}

0 commit comments

Comments
 (0)