Search in sources :

Example 6 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class FixedSizeExemplarReservoirTest method noMeasurement_returnsEmpty.

@Test
public void noMeasurement_returnsEmpty() {
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 1, RandomSupplier.platformDefault());
    assertThat(reservoir.collectAndReset(Attributes.empty())).isEmpty();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 7 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class HistogramBucketExemplarReservoirTest method noMeasurement_returnsEmpty.

@Test
public void noMeasurement_returnsEmpty() {
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new HistogramBucketExemplarReservoir(clock, new double[] {});
    assertThat(reservoir.collectAndReset(Attributes.empty())).isEmpty();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 8 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class HistogramBucketExemplarReservoirTest method oneBucket_samplesEverything.

@Test
public void oneBucket_samplesEverything() {
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new HistogramBucketExemplarReservoir(clock, new double[] {});
    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));
    // only latest measurement is kept per-bucket
    clock.advance(Duration.ofSeconds(1));
    reservoir.offerMeasurement(3L, Attributes.empty(), Context.root());
    reservoir.offerMeasurement(4L, Attributes.empty(), Context.root());
    assertThat(reservoir.collectAndReset(Attributes.empty())).hasSize(1).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasFilteredAttributes(Attributes.empty()).hasValue(4));
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 9 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class SdkSpanTest method testAsSpanData.

@Test
void testAsSpanData() {
    String name = "GreatSpan";
    SpanKind kind = SpanKind.SERVER;
    String traceId = this.traceId;
    String spanId = this.spanId;
    String parentSpanId = this.parentSpanId;
    SpanLimits spanLimits = SpanLimits.getDefault();
    SpanProcessor spanProcessor = NoopSpanProcessor.getInstance();
    TestClock clock = TestClock.create();
    Resource resource = this.resource;
    Attributes attributes = TestUtils.generateRandomAttributes();
    AttributesMap attributesWithCapacity = new AttributesMap(32, Integer.MAX_VALUE);
    attributes.forEach((key, value) -> attributesWithCapacity.put((AttributeKey) key, value));
    Attributes event1Attributes = TestUtils.generateRandomAttributes();
    Attributes event2Attributes = TestUtils.generateRandomAttributes();
    SpanContext context = SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault());
    LinkData link1 = LinkData.create(context, TestUtils.generateRandomAttributes());
    SdkSpan readableSpan = SdkSpan.startSpan(context, name, instrumentationLibraryInfo, kind, parentSpanId != null ? Span.wrap(SpanContext.create(traceId, parentSpanId, TraceFlags.getDefault(), TraceState.getDefault())) : Span.getInvalid(), Context.root(), spanLimits, spanProcessor, clock, resource, attributesWithCapacity, Collections.singletonList(link1), 1, 0);
    long startEpochNanos = clock.now();
    clock.advance(Duration.ofMillis(4));
    long firstEventEpochNanos = clock.now();
    readableSpan.addEvent("event1", event1Attributes);
    clock.advance(Duration.ofMillis(6));
    long secondEventTimeNanos = clock.now();
    readableSpan.addEvent("event2", event2Attributes);
    clock.advance(Duration.ofMillis(100));
    readableSpan.end();
    long endEpochNanos = clock.now();
    List<EventData> events = Arrays.asList(EventData.create(firstEventEpochNanos, "event1", event1Attributes, event1Attributes.size()), EventData.create(secondEventTimeNanos, "event2", event2Attributes, event2Attributes.size()));
    SpanData result = readableSpan.toSpanData();
    verifySpanData(result, attributesWithCapacity, events, Collections.singletonList(link1), name, startEpochNanos, endEpochNanos, StatusData.unset(), /* hasEnded= */
    true);
    assertThat(result.getTotalRecordedLinks()).isEqualTo(1);
    assertThat(result.getSpanContext().isSampled()).isEqualTo(false);
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Resource(io.opentelemetry.sdk.resources.Resource) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) SpanKind(io.opentelemetry.api.trace.SpanKind) EventData(io.opentelemetry.sdk.trace.data.EventData) TestClock(io.opentelemetry.sdk.testing.time.TestClock) AttributeKey(io.opentelemetry.api.common.AttributeKey) Test(org.junit.jupiter.api.Test)

Example 10 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class FixedSizeExemplarReservoirTest method oneMeasurement_filtersAttributes.

@Test
public void oneMeasurement_filtersAttributes() {
    Attributes all = Attributes.builder().put("one", 1).put("two", "two").put("three", true).build();
    Attributes partial = Attributes.builder().put("three", true).build();
    Attributes remaining = Attributes.builder().put("one", 1).put("two", "two").build();
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 1, RandomSupplier.platformDefault());
    reservoir.offerMeasurement(1L, all, Context.root());
    assertThat(reservoir.collectAndReset(partial)).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasValue(1).hasFilteredAttributes(remaining));
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Attributes(io.opentelemetry.api.common.Attributes) 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