use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.
the class FixedSizeExemplarReservoirTest method oneMeasurement_alwaysSamplesFirstMeasurement.
@Test
public void oneMeasurement_alwaysSamplesFirstMeasurement() {
TestClock clock = TestClock.create();
ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 1, RandomSupplier.platformDefault());
reservoir.offerMeasurement(1L, Attributes.empty(), Context.root());
assertThat(reservoir.collectAndReset(Attributes.empty())).hasSize(1).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasFilteredAttributes(Attributes.empty()).hasValue(1));
// Measurement count is reset, we should sample a new measurement (and only one)
clock.advance(Duration.ofSeconds(1));
reservoir.offerMeasurement(2L, Attributes.empty(), Context.root());
assertThat(reservoir.collectAndReset(Attributes.empty())).hasSize(1).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasFilteredAttributes(Attributes.empty()).hasValue(2));
}
use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.
the class FixedSizeExemplarReservoirTest method multiMeasurements_preservesLatestSamples.
@Test
public void multiMeasurements_preservesLatestSamples() {
AttributeKey<Long> key = AttributeKey.longKey("K");
// We cannot mock random in latest jdk, so we create an override.
Random mockRandom = new Random() {
@Override
public int nextInt(int max) {
switch(max) {
// Force one sample in bucket 1 and two in bucket 0.
case 2:
return 1;
default:
return 0;
}
}
};
TestClock clock = TestClock.create();
ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 2, () -> mockRandom);
reservoir.offerMeasurement(1, Attributes.of(key, 1L), Context.root());
reservoir.offerMeasurement(2, Attributes.of(key, 2L), Context.root());
reservoir.offerMeasurement(3, Attributes.of(key, 3L), Context.root());
assertThat(reservoir.collectAndReset(Attributes.empty())).satisfiesExactlyInAnyOrder(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasValue(2), exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasValue(3));
}
use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.
the class HistogramBucketExemplarReservoirTest method multipleBuckets_samplesIntoCorrectBucket.
@Test
public void multipleBuckets_samplesIntoCorrectBucket() {
TestClock clock = TestClock.create();
AttributeKey<Long> bucketKey = AttributeKey.longKey("bucket");
ExemplarReservoir reservoir = new HistogramBucketExemplarReservoir(clock, new double[] { 0, 10, 20 });
reservoir.offerMeasurement(-1, Attributes.of(bucketKey, 0L), Context.root());
reservoir.offerMeasurement(1, Attributes.of(bucketKey, 1L), Context.root());
reservoir.offerMeasurement(11, Attributes.of(bucketKey, 2L), Context.root());
reservoir.offerMeasurement(21, Attributes.of(bucketKey, 3L), Context.root());
assertThat(reservoir.collectAndReset(Attributes.empty())).hasSize(4).satisfiesExactlyInAnyOrder(e -> assertThat(e).hasValue(-1).hasFilteredAttributes(Attributes.of(bucketKey, 0L)), e -> assertThat(e).hasValue(1).hasFilteredAttributes(Attributes.of(bucketKey, 1L)), e -> assertThat(e).hasValue(11).hasFilteredAttributes(Attributes.of(bucketKey, 2L)), e -> assertThat(e).hasValue(21).hasFilteredAttributes(Attributes.of(bucketKey, 3L)));
}
use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.
the class ThrottlingLoggerTest method afterAMinuteLetOneThrough.
@Test
void afterAMinuteLetOneThrough() {
TestClock clock = TestClock.create();
ThrottlingLogger logger = new ThrottlingLogger(realLogger, clock);
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no I should trigger suppression!");
logger.log(Level.WARNING, "oh no I should be suppressed!");
assertThat(logs.getEvents()).hasSize(7);
logs.assertDoesNotContain("oh no I should be suppressed!");
logs.assertContains("oh no I should trigger suppression!");
logs.assertContains("Too many log messages detected. Will only log once per minute from now on.");
clock.advance(Duration.ofMillis(60_001));
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no I should be suppressed!");
assertThat(logs.getEvents()).hasSize(8);
assertThat(logs.getEvents().get(7).getMessage()).isEqualTo("oh no!");
clock.advance(Duration.ofMillis(60_001));
logger.log(Level.WARNING, "oh no!");
logger.log(Level.WARNING, "oh no I should be suppressed!");
assertThat(logs.getEvents()).hasSize(9);
assertThat(logs.getEvents().get(8).getMessage()).isEqualTo("oh no!");
}
use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.
the class RateLimiterTest method testRateLimiterMaxBalance.
@Test
void testRateLimiterMaxBalance() {
TestClock clock = TestClock.create();
RateLimiter limiter = new RateLimiter(0.1, 1.0, clock);
clock.advance(Duration.ofNanos(TimeUnit.MICROSECONDS.toNanos(100)));
assertThat(limiter.trySpend(1.0)).isTrue();
assertThat(limiter.trySpend(1.0)).isFalse();
// move time 20s forward, enough to accumulate credits for 2 messages, but it should still be
// capped at 1
clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(20000)));
assertThat(limiter.trySpend(1.0)).isTrue();
assertThat(limiter.trySpend(1.0)).isFalse();
}
Aggregations