Search in sources :

Example 1 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method addAttributes_OnlyViaSampler.

@Test
void addAttributes_OnlyViaSampler() {
    Sampler sampler = new Sampler() {

        @Override
        public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
            return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, Attributes.builder().put("cat", "meow").build());
        }

        @Override
        public String getDescription() {
            return "test";
        }
    };
    TracerProvider tracerProvider = SdkTracerProvider.builder().setSampler(sampler).build();
    // Verify methods do not crash.
    SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    span.end();
    assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1);
    assertThat(span.toSpanData().getAttributes().get(stringKey("cat"))).isEqualTo("meow");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) Attributes(io.opentelemetry.api.common.Attributes) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) SpanKind(io.opentelemetry.api.trace.SpanKind) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Example 2 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method droppingAttributes.

@Test
void droppingAttributes() {
    int maxNumberOfAttrs = 8;
    SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfAttributes(maxNumberOfAttrs).build();
    TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
    // Verify methods do not crash.
    SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
    for (int i = 0; i < 2 * maxNumberOfAttrs; i++) {
        spanBuilder.setAttribute("key" + i, i);
    }
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    try {
        Attributes attrs = span.toSpanData().getAttributes();
        assertThat(attrs.size()).isEqualTo(maxNumberOfAttrs);
        for (int i = 0; i < maxNumberOfAttrs; i++) {
            assertThat(attrs.get(longKey("key" + i))).isEqualTo(i);
        }
    } finally {
        span.end();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Attributes(io.opentelemetry.api.common.Attributes) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Example 3 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method linkAttributeLength.

@Test
void linkAttributeLength() {
    int maxLength = 25;
    TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(SpanLimits.builder().setMaxAttributeValueLength(maxLength).build()).build();
    SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
    String strVal = IntStream.range(0, maxLength).mapToObj(i -> "a").collect(joining());
    String tooLongStrVal = strVal + strVal;
    Attributes attributes = Attributes.builder().put("string", tooLongStrVal).put("boolean", true).put("long", 1L).put("double", 1.0).put(stringArrayKey("stringArray"), Arrays.asList(strVal, tooLongStrVal)).put(booleanArrayKey("booleanArray"), Arrays.asList(true, false)).put(longArrayKey("longArray"), Arrays.asList(1L, 2L)).put(doubleArrayKey("doubleArray"), Arrays.asList(1.0, 2.0)).build();
    spanBuilder.addLink(sampledSpanContext, attributes);
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    try {
        attributes = span.toSpanData().getLinks().get(0).getAttributes();
        assertThat(attributes.get(stringKey("string"))).isEqualTo(strVal);
        assertThat(attributes.get(booleanKey("boolean"))).isEqualTo(true);
        assertThat(attributes.get(longKey("long"))).isEqualTo(1L);
        assertThat(attributes.get(doubleKey("double"))).isEqualTo(1.0);
        assertThat(attributes.get(stringArrayKey("stringArray"))).isEqualTo(Arrays.asList(strVal, strVal));
        assertThat(attributes.get(booleanArrayKey("booleanArray"))).isEqualTo(Arrays.asList(true, false));
        assertThat(attributes.get(longArrayKey("longArray"))).isEqualTo(Arrays.asList(1L, 2L));
        assertThat(attributes.get(doubleArrayKey("doubleArray"))).isEqualTo(Arrays.asList(1.0, 2.0));
    } finally {
        span.end();
    }
}
Also used : Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) IntStream(java.util.stream.IntStream) Strictness(org.mockito.quality.Strictness) TraceFlags(io.opentelemetry.api.trace.TraceFlags) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) SpanId(io.opentelemetry.api.trace.SpanId) StatusCode(io.opentelemetry.api.trace.StatusCode) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Attributes(io.opentelemetry.api.common.Attributes) SamplingDecision(io.opentelemetry.sdk.trace.samplers.SamplingDecision) AttributeKey.booleanKey(io.opentelemetry.api.common.AttributeKey.booleanKey) TraceState(io.opentelemetry.api.trace.TraceState) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) AttributeKey.stringArrayKey(io.opentelemetry.api.common.AttributeKey.stringArrayKey) Nullable(javax.annotation.Nullable) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) Scope(io.opentelemetry.context.Scope) Collections.emptyList(java.util.Collections.emptyList) SpanContext(io.opentelemetry.api.trace.SpanContext) AttributeKey.doubleArrayKey(io.opentelemetry.api.common.AttributeKey.doubleArrayKey) Instant(java.time.Instant) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors.joining(java.util.stream.Collectors.joining) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AttributeKey.doubleKey(io.opentelemetry.api.common.AttributeKey.doubleKey) List(java.util.List) TracerProvider(io.opentelemetry.api.trace.TracerProvider) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) AttributeKey(io.opentelemetry.api.common.AttributeKey) SpanData(io.opentelemetry.sdk.trace.data.SpanData) AttributeKey.booleanArrayKey(io.opentelemetry.api.common.AttributeKey.booleanArrayKey) AttributeKey.longArrayKey(io.opentelemetry.api.common.AttributeKey.longArrayKey) LinkData(io.opentelemetry.sdk.trace.data.LinkData) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) AttributeKey.longKey(io.opentelemetry.api.common.AttributeKey.longKey) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Attributes(io.opentelemetry.api.common.Attributes) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Example 4 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method truncateLinkAttributes.

@Test
void truncateLinkAttributes() {
    SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfAttributesPerLink(1).build();
    TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
    // Verify methods do not crash.
    SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
    Attributes attributes = Attributes.of(stringKey("key0"), "str", stringKey("key1"), "str", stringKey("key2"), "str");
    spanBuilder.addLink(sampledSpanContext, attributes);
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    try {
        assertThat(span.toSpanData().getLinks()).containsExactly(LinkData.create(sampledSpanContext, Attributes.of(stringKey("key0"), "str"), 3));
    } finally {
        span.end();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Attributes(io.opentelemetry.api.common.Attributes) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Example 5 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilderTest method truncateLink.

@Test
void truncateLink() {
    int maxNumberOfLinks = 8;
    SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfLinks(maxNumberOfLinks).build();
    TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
    // Verify methods do not crash.
    SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
    for (int i = 0; i < 2 * maxNumberOfLinks; i++) {
        spanBuilder.addLink(sampledSpanContext);
    }
    SdkSpan span = (SdkSpan) spanBuilder.startSpan();
    try {
        SpanData spanData = span.toSpanData();
        List<LinkData> links = spanData.getLinks();
        assertThat(links).hasSize(maxNumberOfLinks);
        for (int i = 0; i < maxNumberOfLinks; i++) {
            assertThat(links.get(i)).isEqualTo(LinkData.create(sampledSpanContext));
            assertThat(spanData.getTotalRecordedLinks()).isEqualTo(2 * maxNumberOfLinks);
        }
    } finally {
        span.end();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) SpanData(io.opentelemetry.sdk.trace.data.SpanData) LinkData(io.opentelemetry.sdk.trace.data.LinkData) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Aggregations

TracerProvider (io.opentelemetry.api.trace.TracerProvider)7 Test (org.junit.jupiter.api.Test)7 Attributes (io.opentelemetry.api.common.Attributes)5 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)5 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)3 List (java.util.List)3 SpanContext (io.opentelemetry.api.trace.SpanContext)2 SpanKind (io.opentelemetry.api.trace.SpanKind)2 Context (io.opentelemetry.context.Context)2 Resource (io.opentelemetry.sdk.resources.Resource)2 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SpanData (io.opentelemetry.sdk.trace.data.SpanData)2 DeterministicTraceSampler (io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler)1 BaggageSpanProcessor (io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor)1 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)1 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)1 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 AttributeKey.booleanArrayKey (io.opentelemetry.api.common.AttributeKey.booleanArrayKey)1 AttributeKey.booleanKey (io.opentelemetry.api.common.AttributeKey.booleanKey)1