Search in sources :

Example 1 with BaggageBuilder

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

the class W3CBaggagePropagator 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 baggageHeader = getter.get(carrier, FIELD);
    if (baggageHeader == null) {
        return context;
    }
    if (baggageHeader.isEmpty()) {
        return context;
    }
    BaggageBuilder baggageBuilder = Baggage.builder();
    try {
        extractEntries(baggageHeader, baggageBuilder);
    } catch (RuntimeException e) {
        return context;
    }
    return context.with(baggageBuilder.build());
}
Also used : BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder)

Example 2 with BaggageBuilder

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

the class SpanContextShim method newWithKeyValue.

SpanContextShim newWithKeyValue(String key, String value) {
    BaggageBuilder builder = baggage.toBuilder();
    builder.put(key, value, BaggageEntryMetadata.empty());
    return new SpanContextShim(telemetryInfo(), context, builder.build());
}
Also used : BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder)

Example 3 with BaggageBuilder

use of io.opentelemetry.api.baggage.BaggageBuilder 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 4 with BaggageBuilder

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

the class AwsXrayPropagator method getContextFromHeader.

private static <C> Context getContextFromHeader(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
    String traceHeader = getter.get(carrier, TRACE_HEADER_KEY);
    if (traceHeader == null || traceHeader.isEmpty()) {
        return context;
    }
    String traceId = TraceId.getInvalid();
    String spanId = SpanId.getInvalid();
    Boolean isSampled = false;
    BaggageBuilder baggage = null;
    int baggageReadBytes = 0;
    int pos = 0;
    while (pos < traceHeader.length()) {
        int delimiterIndex = traceHeader.indexOf(TRACE_HEADER_DELIMITER, pos);
        String part;
        if (delimiterIndex >= 0) {
            part = traceHeader.substring(pos, delimiterIndex);
            pos = delimiterIndex + 1;
        } else {
            // Last part.
            part = traceHeader.substring(pos);
            pos = traceHeader.length();
        }
        String trimmedPart = part.trim();
        int equalsIndex = trimmedPart.indexOf(KV_DELIMITER);
        if (equalsIndex < 0) {
            logger.fine("Error parsing X-Ray trace header. Invalid key value pair: " + part);
            return context;
        }
        String value = trimmedPart.substring(equalsIndex + 1);
        if (trimmedPart.startsWith(TRACE_ID_KEY)) {
            traceId = parseTraceId(value);
        } else if (trimmedPart.startsWith(PARENT_ID_KEY)) {
            spanId = parseSpanId(value);
        } else if (trimmedPart.startsWith(SAMPLED_FLAG_KEY)) {
            isSampled = parseTraceFlag(value);
        } else if (baggageReadBytes + trimmedPart.length() <= 256) {
            if (baggage == null) {
                baggage = Baggage.builder();
            }
            baggage.put(trimmedPart.substring(0, equalsIndex), value);
            baggageReadBytes += trimmedPart.length();
        }
    }
    if (isSampled == null) {
        logger.fine("Invalid Sampling flag in X-Ray trace header: '" + TRACE_HEADER_KEY + "' with value " + traceHeader + "'.");
        return context;
    }
    SpanContext spanContext = SpanContext.createFromRemoteParent(StringUtils.padLeft(traceId, TraceId.getLength()), spanId, isSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault());
    if (spanContext.isValid()) {
        context = context.with(Span.wrap(spanContext));
    }
    if (baggage != null) {
        context = context.with(baggage.build());
    }
    return context;
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder)

Example 5 with BaggageBuilder

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

the class JaegerPropagator method getBaggageFromHeader.

@Nullable
private static <C> Baggage getBaggageFromHeader(@Nullable C carrier, TextMapGetter<C> getter) {
    BaggageBuilder builder = null;
    Iterable<String> keys = carrier != null ? getter.keys(carrier) : Collections.emptyList();
    for (String key : keys) {
        if (key.startsWith(BAGGAGE_PREFIX)) {
            if (key.length() == BAGGAGE_PREFIX.length()) {
                continue;
            }
            if (builder == null) {
                builder = Baggage.builder();
            }
            String value = getter.get(carrier, key);
            if (value != null) {
                builder.put(key.substring(BAGGAGE_PREFIX.length()), value);
            }
        } else if (key.equals(BAGGAGE_HEADER)) {
            String value = getter.get(carrier, key);
            if (value != null) {
                if (builder == null) {
                    builder = Baggage.builder();
                }
                builder = parseBaggageHeader(value, builder);
            }
        }
    }
    return builder == null ? null : builder.build();
}
Also used : BaggageBuilder(io.opentelemetry.api.baggage.BaggageBuilder) Nullable(javax.annotation.Nullable)

Aggregations

BaggageBuilder (io.opentelemetry.api.baggage.BaggageBuilder)5 SpanContext (io.opentelemetry.api.trace.SpanContext)2 Baggage (io.opentelemetry.api.baggage.Baggage)1 Context (io.opentelemetry.context.Context)1 Nullable (javax.annotation.Nullable)1