use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class SpanBuilderShim method start.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Span start() {
Baggage baggage = null;
io.opentelemetry.api.trace.SpanBuilder builder = tracer().spanBuilder(spanName);
if (ignoreActiveSpan && parentSpan == null && parentSpanContext == null) {
builder.setNoParent();
} else if (parentSpan != null) {
builder.setParent(Context.root().with(parentSpan.getSpan()));
SpanContextShim contextShim = spanContextTable().get(parentSpan);
baggage = contextShim == null ? null : contextShim.getBaggage();
} else if (parentSpanContext != null) {
builder.setParent(Context.root().with(io.opentelemetry.api.trace.Span.wrap(parentSpanContext.getSpanContext())));
baggage = parentSpanContext.getBaggage();
}
for (io.opentelemetry.api.trace.SpanContext link : parentLinks) {
builder.addLink(link);
}
if (spanKind != null) {
builder.setSpanKind(spanKind);
}
if (startTimestampMicros > 0) {
builder.setStartTimestamp(startTimestampMicros, TimeUnit.MICROSECONDS);
}
// so those attributes are available to the Sampling API.
for (int i = 0; i < this.spanBuilderAttributeKeys.size(); i++) {
AttributeKey key = this.spanBuilderAttributeKeys.get(i);
Object value = this.spanBuilderAttributeValues.get(i);
builder.setAttribute(key, value);
}
io.opentelemetry.api.trace.Span span = builder.startSpan();
if (error) {
span.setStatus(StatusCode.ERROR);
}
SpanShim spanShim = new SpanShim(telemetryInfo(), span);
if (baggage != null && baggage != telemetryInfo().emptyBaggage()) {
spanContextTable().create(spanShim, baggage);
}
return spanShim;
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class W3CBaggagePropagatorTest method extract_member_someInvalid.
@Test
void extract_member_someInvalid() {
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
Context input = Context.current();
Context result = propagator.extract(input, ImmutableMap.of("baggage", "key1= v;alsdf;-asdflkjasdf===asdlfkjadsf ,,a sdf9asdf-alue1; metadata-key = " + "value; othermetadata, key2 =value2, key3 =\tvalue3 ; "), getter);
Baggage expectedBaggage = Baggage.builder().put("key1", "v", BaggageEntryMetadata.create("alsdf;-asdflkjasdf===asdlfkjadsf")).put("key2", "value2").put("key3", "value3").build();
assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage);
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class W3CBaggagePropagatorTest method extract_key_duplicate.
@Test
void extract_key_duplicate() {
W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
Context result = propagator.extract(Context.root(), ImmutableMap.of("baggage", "key=value1,key=value2"), getter);
Baggage expectedBaggage = Baggage.builder().put("key", "value2").build();
assertThat(Baggage.fromContext(result)).isEqualTo(expectedBaggage);
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class OtTracePropagator 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;
}
// Lightstep trace id MUST be 64-bits therefore OpenTelemetry trace id is truncated to 64-bits
// by retaining least significant (right-most) bits.
setter.set(carrier, TRACE_ID_HEADER, spanContext.getTraceId().substring(TraceId.getLength() / 2));
setter.set(carrier, SPAN_ID_HEADER, spanContext.getSpanId());
setter.set(carrier, SAMPLED_HEADER, String.valueOf(spanContext.isSampled()));
// Baggage is only injected if there is a valid SpanContext
Baggage baggage = Baggage.fromContext(context);
if (!baggage.isEmpty()) {
// Metadata is not supported by OpenTracing
baggage.forEach((key, baggageEntry) -> setter.set(carrier, PREFIX_BAGGAGE_HEADER + key, baggageEntry.getValue()));
}
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class OtTracePropagatorTest method inject_Baggage_InvalidContext.
@Test
void inject_Baggage_InvalidContext() {
Map<String, String> carrier = new LinkedHashMap<>();
Baggage baggage = Baggage.builder().put("foo", "bar").put("key", "value").build();
propagator.inject(withSpanContext(SpanContext.create(TraceId.getInvalid(), SpanId.getInvalid(), TraceFlags.getSampled(), TraceState.getDefault()), Context.current().with(baggage)), carrier, setter);
assertThat(carrier).isEmpty();
}
Aggregations