Search in sources :

Example 1 with BaggageEntry

use of io.opentelemetry.api.baggage.BaggageEntry 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)

Aggregations

Baggage (io.opentelemetry.api.baggage.Baggage)1 BaggageEntry (io.opentelemetry.api.baggage.BaggageEntry)1 Span (io.opentelemetry.api.trace.Span)1 SpanContext (io.opentelemetry.api.trace.SpanContext)1