Search in sources :

Example 1 with SamplingDecision

use of io.opentelemetry.sdk.trace.samplers.SamplingDecision 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)

Example 2 with SamplingDecision

use of io.opentelemetry.sdk.trace.samplers.SamplingDecision in project opentelemetry-java by open-telemetry.

the class SdkSpanBuilder method startSpan.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Span startSpan() {
    Context parentContext = parent == null ? Context.current() : parent;
    Span parentSpan = Span.fromContext(parentContext);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    String traceId;
    IdGenerator idGenerator = tracerSharedState.getIdGenerator();
    String spanId = idGenerator.generateSpanId();
    if (!parentSpanContext.isValid()) {
        // New root span.
        traceId = idGenerator.generateTraceId();
    } else {
        // New child span.
        traceId = parentSpanContext.getTraceId();
    }
    List<LinkData> immutableLinks = links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
    // Avoid any possibility to modify the links list by adding links to the Builder after the
    // startSpan is called. If that happens all the links will be added in a new list.
    links = null;
    Attributes immutableAttributes = attributes == null ? Attributes.empty() : attributes;
    SamplingResult samplingResult = tracerSharedState.getSampler().shouldSample(parentContext, traceId, spanName, spanKind, immutableAttributes, immutableLinks);
    SamplingDecision samplingDecision = samplingResult.getDecision();
    TraceState samplingResultTraceState = samplingResult.getUpdatedTraceState(parentSpanContext.getTraceState());
    SpanContext spanContext = ImmutableSpanContext.create(traceId, spanId, isSampled(samplingDecision) ? TraceFlags.getSampled() : TraceFlags.getDefault(), samplingResultTraceState, /* remote= */
    false, tracerSharedState.isIdGeneratorSafeToSkipIdValidation());
    if (!isRecording(samplingDecision)) {
        return Span.wrap(spanContext);
    }
    Attributes samplingAttributes = samplingResult.getAttributes();
    if (!samplingAttributes.isEmpty()) {
        samplingAttributes.forEach((key, value) -> attributes().put((AttributeKey) key, value));
    }
    // Avoid any possibility to modify the attributes by adding attributes to the Builder after the
    // startSpan is called. If that happens all the attributes will be added in a new map.
    AttributesMap recordedAttributes = attributes;
    attributes = null;
    return SdkSpan.startSpan(spanContext, spanName, instrumentationLibraryInfo, spanKind, parentSpan, parentContext, spanLimits, tracerSharedState.getActiveSpanProcessor(), tracerSharedState.getClock(), tracerSharedState.getResource(), recordedAttributes, immutableLinks, totalNumberOfLinksAdded, startEpochNanos);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) ImmutableSpanContext(io.opentelemetry.api.internal.ImmutableSpanContext) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext) ImmutableSpanContext(io.opentelemetry.api.internal.ImmutableSpanContext) LinkData(io.opentelemetry.sdk.trace.data.LinkData) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Attributes(io.opentelemetry.api.common.Attributes) Span(io.opentelemetry.api.trace.Span) AttributeKey(io.opentelemetry.api.common.AttributeKey) SamplingDecision(io.opentelemetry.sdk.trace.samplers.SamplingDecision)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)2 SpanContext (io.opentelemetry.api.trace.SpanContext)2 TraceState (io.opentelemetry.api.trace.TraceState)2 Context (io.opentelemetry.context.Context)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SamplingDecision (io.opentelemetry.sdk.trace.samplers.SamplingDecision)2 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)2 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 ImmutableSpanContext (io.opentelemetry.api.internal.ImmutableSpanContext)1 Span (io.opentelemetry.api.trace.Span)1 SpanKind (io.opentelemetry.api.trace.SpanKind)1 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)1 Test (org.junit.jupiter.api.Test)1