Skip to content

Commit

Permalink
Added check on rotate frequency (#4509)
Browse files Browse the repository at this point in the history
without this change you can get an arithmetic exception if you pass a 0. Negative numbers don't make sense

fixes gh-3068
  • Loading branch information
marcingrzejszczak authored Jan 15, 2024
1 parent 597c79f commit 3efb4f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TimeWindowMax(Clock clock, DistributionStatisticConfig config) {

public TimeWindowMax(Clock clock, long rotateFrequencyMillis, int bufferLength) {
this.clock = clock;
this.durationBetweenRotatesMillis = rotateFrequencyMillis;
this.durationBetweenRotatesMillis = checkPositive(rotateFrequencyMillis);
this.lastRotateTimestampMillis = clock.wallTime();
this.currentBucket = 0;

Expand All @@ -64,6 +64,13 @@ public TimeWindowMax(Clock clock, long rotateFrequencyMillis, int bufferLength)
}
}

private static long checkPositive(long rotateFrequencyMillis) {
if (rotateFrequencyMillis <= 0) {
throw new IllegalArgumentException("Rotate frequency must be a positive number");
}
return rotateFrequencyMillis;
}

/**
* For use by timer implementations.
* @param sample The value to record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micrometer.core.instrument.distribution;

import io.micrometer.core.instrument.MockClock;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Duration;
Expand Down Expand Up @@ -68,4 +69,11 @@ void testLongPeriodOfInactivity() {
assertThat(timeWindowMax.poll()).isEqualTo(100500); // 666 | 500 | 100500
}

@Test
void throwsExceptionWhenRotateFrequency0() {
Assertions.assertThatThrownBy(() -> new TimeWindowMax(clock, 0, 3))
.isInstanceOf(IllegalArgumentException.class)
.withFailMessage("Rotate frequency must be a positive number");
}

}

0 comments on commit 3efb4f6

Please sign in to comment.