Search in sources :

Example 16 with Baggage

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;
}
Also used : AttributeKey(io.opentelemetry.api.common.AttributeKey) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 17 with Baggage

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

Example 18 with Baggage

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

Example 19 with Baggage

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

Example 20 with Baggage

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