use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class ImmutableSpanContextTest method testWithIdValidationAndValidIds.
@Test
public void testWithIdValidationAndValidIds() {
SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault(), false, false);
assertThat(spanContext.isValid()).isTrue();
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class ImmutableSpanContextTest method testSkipIdValidationAndInvalidSpanId.
@Test
public void testSkipIdValidationAndInvalidSpanId() {
SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, SpanId.getInvalid(), TraceFlags.getDefault(), TraceState.getDefault(), false, true);
assertThat(spanContext.isValid()).isTrue();
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class W3CTraceContextPropagator method extractImpl.
private static <C> SpanContext extractImpl(@Nullable C carrier, TextMapGetter<C> getter) {
String traceParent = getter.get(carrier, TRACE_PARENT);
if (traceParent == null) {
return SpanContext.getInvalid();
}
SpanContext contextFromParentHeader = extractContextFromTraceParent(traceParent);
if (!contextFromParentHeader.isValid()) {
return contextFromParentHeader;
}
String traceStateHeader = getter.get(carrier, TRACE_STATE);
if (traceStateHeader == null || traceStateHeader.isEmpty()) {
return contextFromParentHeader;
}
try {
TraceState traceState = decodeTraceState(traceStateHeader);
return SpanContext.createFromRemoteParent(contextFromParentHeader.getTraceId(), contextFromParentHeader.getSpanId(), contextFromParentHeader.getTraceFlags(), traceState);
} catch (IllegalArgumentException e) {
logger.fine("Unparseable tracestate header. Returning span context without state.");
return contextFromParentHeader;
}
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class OtTracePropagator method extract.
@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
if (context == null) {
return Context.root();
}
if (getter == null) {
return context;
}
String incomingTraceId = getter.get(carrier, TRACE_ID_HEADER);
String traceId = incomingTraceId == null ? TraceId.getInvalid() : StringUtils.padLeft(incomingTraceId, MAX_TRACE_ID_LENGTH);
String spanId = getter.get(carrier, SPAN_ID_HEADER);
String sampled = getter.get(carrier, SAMPLED_HEADER);
SpanContext spanContext = buildSpanContext(traceId, spanId, sampled);
if (!spanContext.isValid()) {
return context;
}
Context extractedContext = context.with(Span.wrap(spanContext));
// Baggage is only extracted if there is a valid SpanContext
if (carrier != null) {
BaggageBuilder baggageBuilder = Baggage.builder();
for (String key : getter.keys(carrier)) {
if (!key.startsWith(PREFIX_BAGGAGE_HEADER)) {
continue;
}
String value = getter.get(carrier, key);
if (value == null) {
continue;
}
baggageBuilder.put(key.replace(OtTracePropagator.PREFIX_BAGGAGE_HEADER, ""), value);
}
Baggage baggage = baggageBuilder.build();
if (!baggage.isEmpty()) {
extractedContext = extractedContext.with(baggage);
}
}
return extractedContext;
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class JaegerPropagator method inject.
@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
if (context == null) {
return;
}
if (setter == null) {
return;
}
SpanContext spanContext = Span.fromContext(context).getSpanContext();
if (spanContext.isValid()) {
injectSpan(spanContext, carrier, setter);
}
injectBaggage(Baggage.fromContext(context), carrier, setter);
}
Aggregations