Search in sources :

Example 21 with Baggage

use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.

the class AwsXrayPropagator method inject.

@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
    if (context == null) {
        return;
    }
    if (setter == null) {
        return;
    }
    Span span = Span.fromContext(context);
    if (!span.getSpanContext().isValid()) {
        return;
    }
    SpanContext spanContext = span.getSpanContext();
    String otTraceId = spanContext.getTraceId();
    String xrayTraceId = TRACE_ID_VERSION + TRACE_ID_DELIMITER + otTraceId.substring(0, TRACE_ID_FIRST_PART_LENGTH) + TRACE_ID_DELIMITER + otTraceId.substring(TRACE_ID_FIRST_PART_LENGTH);
    String parentId = spanContext.getSpanId();
    char samplingFlag = spanContext.isSampled() ? IS_SAMPLED : NOT_SAMPLED;
    // TODO: Add OT trace state to the X-Ray trace header
    StringBuilder traceHeader = new StringBuilder();
    traceHeader.append(TRACE_ID_KEY).append(KV_DELIMITER).append(xrayTraceId).append(TRACE_HEADER_DELIMITER).append(PARENT_ID_KEY).append(KV_DELIMITER).append(parentId).append(TRACE_HEADER_DELIMITER).append(SAMPLED_FLAG_KEY).append(KV_DELIMITER).append(samplingFlag);
    Baggage baggage = Baggage.fromContext(context);
    // Truncate baggage to 256 chars per X-Ray spec.
    baggage.forEach(new BiConsumer<String, BaggageEntry>() {

        private int baggageWrittenBytes;

        @Override
        public void accept(String key, BaggageEntry entry) {
            if (key.equals(TRACE_ID_KEY) || key.equals(PARENT_ID_KEY) || key.equals(SAMPLED_FLAG_KEY)) {
                return;
            }
            // Size is key/value pair, excludes delimiter.
            int size = key.length() + entry.getValue().length() + 1;
            if (baggageWrittenBytes + size > 256) {
                return;
            }
            traceHeader.append(TRACE_HEADER_DELIMITER).append(key).append(KV_DELIMITER).append(entry.getValue());
            baggageWrittenBytes += size;
        }
    });
    setter.set(carrier, TRACE_HEADER_KEY, traceHeader.toString());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) BaggageEntry(io.opentelemetry.api.baggage.BaggageEntry) Span(io.opentelemetry.api.trace.Span) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 22 with Baggage

use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.

the class JaegerPropagator 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;
    }
    SpanContext spanContext = getSpanContextFromHeader(carrier, getter);
    if (spanContext.isValid()) {
        context = context.with(Span.wrap(spanContext));
    }
    Baggage baggage = getBaggageFromHeader(carrier, getter);
    if (baggage != null) {
        context = context.with(baggage);
    }
    return context;
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 23 with Baggage

use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.

the class SdkMeterProviderTest method viewSdk_capturesBaggageFromContext.

@Test
void viewSdk_capturesBaggageFromContext() {
    InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.COUNTER).setName("test").build();
    InMemoryMetricReader reader = InMemoryMetricReader.create();
    SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, ((ViewBuilderImpl) View.builder().setAggregation(Aggregation.sum())).appendAllBaggageAttributes().build()).build();
    Meter meter = provider.get(SdkMeterProviderTest.class.getName());
    Baggage baggage = Baggage.builder().put("baggage", "value").build();
    Context context = Context.root().with(baggage);
    LongCounter counter = meter.counterBuilder("test").build();
    // Make sure whether or not we explicitly pass baggage, all values have it appended.
    counter.add(1, Attributes.empty(), context);
    // Also check implicit context
    try (Scope ignored = context.makeCurrent()) {
        counter.add(1, Attributes.empty());
    }
    // Now make sure all metrics have baggage appended.
    // Implicitly we should have ONLY ONE metric data point that has baggage appended.
    assertThat(reader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasName("test").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasAttributes(Attributes.builder().put("baggage", "value").build())));
}
Also used : Context(io.opentelemetry.context.Context) LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Attributes(io.opentelemetry.api.common.Attributes) Aggregation(io.opentelemetry.sdk.metrics.view.Aggregation) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) ViewBuilderImpl(io.opentelemetry.sdk.metrics.internal.view.ViewBuilderImpl) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) View(io.opentelemetry.sdk.metrics.view.View) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) LongHistogram(io.opentelemetry.api.metrics.LongHistogram) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Context(io.opentelemetry.context.Context) DoubleUpDownCounter(io.opentelemetry.api.metrics.DoubleUpDownCounter) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Scope(io.opentelemetry.context.Scope) DoubleHistogram(io.opentelemetry.api.metrics.DoubleHistogram) Mockito.when(org.mockito.Mockito.when) InstrumentType(io.opentelemetry.sdk.metrics.common.InstrumentType) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Assertions.entry(org.assertj.core.api.Assertions.entry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Baggage(io.opentelemetry.api.baggage.Baggage) DoubleCounter(io.opentelemetry.api.metrics.DoubleCounter) AttributeKey(io.opentelemetry.api.common.AttributeKey) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) MeterProvider(io.opentelemetry.api.metrics.MeterProvider) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Scope(io.opentelemetry.context.Scope) Meter(io.opentelemetry.api.metrics.Meter) InstrumentSelector(io.opentelemetry.sdk.metrics.view.InstrumentSelector) Baggage(io.opentelemetry.api.baggage.Baggage) ObservableLongCounter(io.opentelemetry.api.metrics.ObservableLongCounter) LongCounter(io.opentelemetry.api.metrics.LongCounter) Test(org.junit.jupiter.api.Test)

Example 24 with Baggage

use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.

the class W3CBaggagePropagatorTest method extract_value_leadingSpaces.

@Test
void extract_value_leadingSpaces() {
    W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
    Context result = propagator.extract(Context.root(), ImmutableMap.of("baggage", "  key=  value1"), getter);
    Baggage expectedBaggage = Baggage.builder().put("key", "value1").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 25 with Baggage

use of io.opentelemetry.api.baggage.Baggage in project opentelemetry-java by open-telemetry.

the class W3CBaggagePropagatorTest method extract_value_multiple_onlySpaces.

@Test
void extract_value_multiple_onlySpaces() {
    W3CBaggagePropagator propagator = W3CBaggagePropagator.getInstance();
    Context result = propagator.extract(Context.root(), ImmutableMap.of("baggage", "key1=     ,key1=val"), getter);
    Baggage expectedBaggage = Baggage.builder().put("key1", "val").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)

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