use of io.opentelemetry.api.baggage.Baggage 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.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class OtTracePropagatorTest method inject_Baggage.
@Test
void inject_Baggage() {
Map<String, String> carrier = new LinkedHashMap<>();
Baggage baggage = Baggage.builder().put("foo", "bar").put("key", "value").build();
propagator.inject(withSpanContext(SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault()), Context.current().with(baggage)), carrier, setter);
assertThat(carrier).containsEntry(OtTracePropagator.PREFIX_BAGGAGE_HEADER + "foo", "bar");
assertThat(carrier).containsEntry(OtTracePropagator.PREFIX_BAGGAGE_HEADER + "key", "value");
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class AttributesProcessorTest method appendBaggageByKeyName_works.
@Test
public void appendBaggageByKeyName_works() {
AttributesProcessor processor = AttributesProcessor.appendBaggageByKeyName(name -> "keep".equals(name));
Baggage baggage = Baggage.builder().put("baggage", "value").put("keep", "baggage").build();
Context context = Context.root().with(baggage);
assertThat(processor.process(Attributes.builder().put("test", "keep").build(), context)).hasSize(2).containsEntry("test", "keep").containsEntry("keep", "baggage");
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class AttributesProcessorTest method appendBaggage_works.
@Test
public void appendBaggage_works() {
AttributesProcessor processor = AttributesProcessor.appendBaggageByKeyName(ignored -> true);
Baggage baggage = Baggage.builder().put("baggage", "value").build();
Context context = Context.root().with(baggage);
assertThat(processor.process(Attributes.builder().put("test", "keep").build(), context)).hasSize(2).containsEntry("test", "keep").containsEntry("baggage", "value");
}
use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.
the class AttributesProcessorTest method proccessors_joinByThen.
@Test
public void proccessors_joinByThen() {
// Baggage should be added, then all keys filtered.
AttributesProcessor processor = AttributesProcessor.appendBaggageByKeyName(ignored -> true).then(AttributesProcessor.filterByKeyName(name -> "baggage".equals(name)));
Baggage baggage = Baggage.builder().put("baggage", "value").put("keep", "baggage").build();
Context context = Context.root().with(baggage);
assertThat(processor.process(Attributes.builder().put("test", "keep").build(), context)).containsEntry("baggage", "value").hasSize(1);
}
Aggregations