Search in sources :

Example 11 with Baggage

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;
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 12 with Baggage

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");
}
Also used : Baggage(io.opentelemetry.api.baggage.Baggage) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 13 with Baggage

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");
}
Also used : Context(io.opentelemetry.context.Context) Baggage(io.opentelemetry.api.baggage.Baggage) Test(org.junit.jupiter.api.Test)

Example 14 with 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");
}
Also used : Context(io.opentelemetry.context.Context) Baggage(io.opentelemetry.api.baggage.Baggage) Test(org.junit.jupiter.api.Test)

Example 15 with Baggage

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);
}
Also used : Test(org.junit.jupiter.api.Test) Context(io.opentelemetry.context.Context) Baggage(io.opentelemetry.api.baggage.Baggage) OpenTelemetryAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat) Attributes(io.opentelemetry.api.common.Attributes) Context(io.opentelemetry.context.Context) Baggage(io.opentelemetry.api.baggage.Baggage) Test(org.junit.jupiter.api.Test)

Aggregations

Baggage (io.opentelemetry.api.baggage.Baggage)40 Test (org.junit.jupiter.api.Test)33 Context (io.opentelemetry.context.Context)28 LinkedHashMap (java.util.LinkedHashMap)7 SpanContext (io.opentelemetry.api.trace.SpanContext)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 AttributeKey (io.opentelemetry.api.common.AttributeKey)2 Attributes (io.opentelemetry.api.common.Attributes)2 Span (io.opentelemetry.api.trace.Span)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 BaggageBuilder (io.opentelemetry.api.baggage.BaggageBuilder)1 BaggageEntry (io.opentelemetry.api.baggage.BaggageEntry)1 DoubleCounter (io.opentelemetry.api.metrics.DoubleCounter)1 DoubleHistogram (io.opentelemetry.api.metrics.DoubleHistogram)1 DoubleUpDownCounter (io.opentelemetry.api.metrics.DoubleUpDownCounter)1 LongCounter (io.opentelemetry.api.metrics.LongCounter)1 LongHistogram (io.opentelemetry.api.metrics.LongHistogram)1 LongUpDownCounter (io.opentelemetry.api.metrics.LongUpDownCounter)1