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);
}
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());
}
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));
}
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));
}
Aggregations