Search in sources :

Example 11 with TestClock

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));
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 12 with TestClock

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));
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Random(java.util.Random) Test(org.junit.jupiter.api.Test)

Example 13 with TestClock

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)));
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 14 with TestClock

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!");
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 15 with TestClock

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();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Aggregations

TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 Test (org.junit.jupiter.api.Test)18 Attributes (io.opentelemetry.api.common.Attributes)3 SpanContext (io.opentelemetry.api.trace.SpanContext)2 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 SpanKind (io.opentelemetry.api.trace.SpanKind)1 Context (io.opentelemetry.context.Context)1 Resource (io.opentelemetry.sdk.resources.Resource)1 EventData (io.opentelemetry.sdk.trace.data.EventData)1 LinkData (io.opentelemetry.sdk.trace.data.LinkData)1 SpanData (io.opentelemetry.sdk.trace.data.SpanData)1 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1