Search in sources :

Example 1 with SpanKind

use of io.opentelemetry.api.trace.SpanKind 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 SpanKind

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

the class Instrumenter method shouldStart.

/**
 * Returns whether instrumentation should be applied for the {@link REQUEST}. If {@code true},
 * call {@link #start(Context, Object)} and {@link #end(Context, Object, Object, Throwable)}
 * around the operation being instrumented, or if {@code false} execute the operation directly
 * without calling those methods.
 */
public boolean shouldStart(Context parentContext, REQUEST request) {
    if (disabled) {
        return false;
    }
    SpanKind spanKind = spanKindExtractor.extract(request);
    boolean suppressed = spanSuppressionStrategy.shouldSuppress(parentContext, spanKind);
    if (suppressed) {
        supportability.recordSuppressedSpan(spanKind, instrumentationName);
    }
    return !suppressed;
}
Also used : SpanKind(io.opentelemetry.api.trace.SpanKind)

Example 3 with SpanKind

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

the class SupportabilityMetrics method report.

// visible for testing
void report() {
    suppressionCounters.forEach((instrumentationName, countsByKind) -> {
        for (SpanKind kind : SpanKind.values()) {
            long value = countsByKind.getAndReset(kind);
            if (value > 0) {
                reporter.accept("Suppressed Spans by '" + instrumentationName + "' (" + kind + ") : " + value);
            }
        }
    });
    counters.forEach((counterName, counter) -> {
        long value = counter.getAndSet(0);
        if (value > 0) {
            reporter.accept("Counter '" + counterName + "' : " + value);
        }
    });
}
Also used : SpanKind(io.opentelemetry.api.trace.SpanKind)

Example 4 with SpanKind

use of io.opentelemetry.api.trace.SpanKind 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 5 with SpanKind

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

the class SdkSpanBuilderTest method sampler_updatedTraceState.

@Test
void sampler_updatedTraceState() {
    String samplerAttributeName = "sampler-attribute";
    AttributeKey<String> samplerAttributeKey = stringKey(samplerAttributeName);
    SdkSpan span = (SdkSpan) SdkTracerProvider.builder().setSampler(new Sampler() {

        @Override
        public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
            return new SamplingResult() {

                @Override
                public SamplingDecision getDecision() {
                    return SamplingDecision.RECORD_AND_SAMPLE;
                }

                @Override
                public Attributes getAttributes() {
                    return Attributes.empty();
                }

                @Override
                public TraceState getUpdatedTraceState(TraceState parentTraceState) {
                    return parentTraceState.toBuilder().put("newkey", "newValue").build();
                }
            };
        }

        @Override
        public String getDescription() {
            return "test sampler";
        }
    }).build().get("test").spanBuilder(SPAN_NAME).setAttribute(samplerAttributeKey, "none").startSpan();
    try {
        assertThat(span.getSpanContext().isSampled()).isTrue();
        assertThat(span.toSpanData().getAttributes().get(samplerAttributeKey)).isNotNull();
        assertThat(span.toSpanData().getSpanContext().getTraceState()).isEqualTo(TraceState.builder().put("newkey", "newValue").build());
    } finally {
        span.end();
    }
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) TraceState(io.opentelemetry.api.trace.TraceState) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Attributes(io.opentelemetry.api.common.Attributes) SpanKind(io.opentelemetry.api.trace.SpanKind) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) SamplingDecision(io.opentelemetry.sdk.trace.samplers.SamplingDecision) Test(org.junit.jupiter.api.Test)

Aggregations

SpanKind (io.opentelemetry.api.trace.SpanKind)8 Attributes (io.opentelemetry.api.common.Attributes)4 Context (io.opentelemetry.context.Context)4 SpanContext (io.opentelemetry.api.trace.SpanContext)3 Test (org.junit.jupiter.api.Test)3 Span (io.opentelemetry.api.trace.Span)2 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SpanData (io.opentelemetry.sdk.trace.data.SpanData)2 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)2 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 TraceState (io.opentelemetry.api.trace.TraceState)1 TracerProvider (io.opentelemetry.api.trace.TracerProvider)1 Resource (io.opentelemetry.sdk.resources.Resource)1 TestClock (io.opentelemetry.sdk.testing.time.TestClock)1 EventData (io.opentelemetry.sdk.trace.data.EventData)1 SamplingDecision (io.opentelemetry.sdk.trace.samplers.SamplingDecision)1 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)1 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)1 Instant (java.time.Instant)1