Search in sources :

Example 6 with TraceState

use of io.opentelemetry.api.trace.TraceState 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, instrumentationScopeInfo, 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)

Example 7 with TraceState

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

the class SpanLinkMarshaler method create.

// Visible for testing
static SpanLinkMarshaler create(LinkData link) {
    TraceState traceState = link.getSpanContext().getTraceState();
    byte[] traceStateUtf8 = traceState.isEmpty() ? EMPTY_BYTES : encodeTraceState(traceState).getBytes(StandardCharsets.UTF_8);
    return new SpanLinkMarshaler(link.getSpanContext().getTraceId(), link.getSpanContext().getSpanId(), traceStateUtf8, KeyValueMarshaler.createRepeated(link.getAttributes()), link.getTotalAttributeCount() - link.getAttributes().size());
}
Also used : TraceState(io.opentelemetry.api.trace.TraceState) W3CTraceContextEncoding.encodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState)

Example 8 with TraceState

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

the class W3CTraceContextPropagator method inject.

@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
    if (context == null || setter == null) {
        return;
    }
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    if (!spanContext.isValid()) {
        return;
    }
    char[] chars = TemporaryBuffers.chars(TRACEPARENT_HEADER_SIZE);
    chars[0] = VERSION.charAt(0);
    chars[1] = VERSION.charAt(1);
    chars[2] = TRACEPARENT_DELIMITER;
    String traceId = spanContext.getTraceId();
    traceId.getChars(0, traceId.length(), chars, TRACE_ID_OFFSET);
    chars[SPAN_ID_OFFSET - 1] = TRACEPARENT_DELIMITER;
    String spanId = spanContext.getSpanId();
    spanId.getChars(0, spanId.length(), chars, SPAN_ID_OFFSET);
    chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER;
    String traceFlagsHex = spanContext.getTraceFlags().asHex();
    chars[TRACE_OPTION_OFFSET] = traceFlagsHex.charAt(0);
    chars[TRACE_OPTION_OFFSET + 1] = traceFlagsHex.charAt(1);
    setter.set(carrier, TRACE_PARENT, new String(chars, 0, TRACEPARENT_HEADER_SIZE));
    TraceState traceState = spanContext.getTraceState();
    if (traceState.isEmpty()) {
        // No need to add an empty "tracestate" header.
        return;
    }
    setter.set(carrier, TRACE_STATE, encodeTraceState(traceState));
}
Also used : W3CTraceContextEncoding.decodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.decodeTraceState) W3CTraceContextEncoding.encodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 9 with TraceState

use of io.opentelemetry.api.trace.TraceState in project ApplicationInsights-Java by microsoft.

the class RpConfigurationPollingTest method getCurrentSamplingPercentage.

private static double getCurrentSamplingPercentage() {
    SpanContext spanContext = SpanContext.create("12341234123412341234123412341234", "1234123412341234", TraceFlags.getSampled(), TraceState.getDefault());
    Context parentContext = Context.root().with(Span.wrap(spanContext));
    SamplingResult samplingResult = DelegatingSampler.getInstance().shouldSample(parentContext, "12341234123412341234123412341234", "my span name", SpanKind.SERVER, Attributes.empty(), Collections.emptyList());
    TraceState traceState = samplingResult.getUpdatedTraceState(TraceState.getDefault());
    return Double.parseDouble(traceState.get(TelemetryUtil.SAMPLING_PERCENTAGE_TRACE_STATE));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult)

Aggregations

TraceState (io.opentelemetry.api.trace.TraceState)9 SpanContext (io.opentelemetry.api.trace.SpanContext)5 W3CTraceContextEncoding.encodeTraceState (io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState)4 Context (io.opentelemetry.context.Context)3 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)3 Attributes (io.opentelemetry.api.common.Attributes)2 TraceStateBuilder (io.opentelemetry.api.trace.TraceStateBuilder)2 W3CTraceContextEncoding.decodeTraceState (io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.decodeTraceState)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SamplingDecision (io.opentelemetry.sdk.trace.samplers.SamplingDecision)2 io.opencensus.trace (io.opencensus.trace)1 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 TraceFlags (io.opentelemetry.api.trace.TraceFlags)1 KeyValueMarshaler (io.opentelemetry.exporter.internal.otlp.KeyValueMarshaler)1 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)1 Test (org.junit.jupiter.api.Test)1