Search in sources :

Example 1 with TraceFlags

use of io.opentelemetry.api.trace.TraceFlags in project opentelemetry-java by open-telemetry.

the class W3CTraceContextPropagator method extractContextFromTraceParent.

private static SpanContext extractContextFromTraceParent(String traceparent) {
    // TODO(bdrutu): Do we need to verify that version is hex and that
    // for the version the length is the expected one?
    boolean isValid = (traceparent.length() == TRACEPARENT_HEADER_SIZE || (traceparent.length() > TRACEPARENT_HEADER_SIZE && traceparent.charAt(TRACEPARENT_HEADER_SIZE) == TRACEPARENT_DELIMITER)) && traceparent.charAt(TRACE_ID_OFFSET - 1) == TRACEPARENT_DELIMITER && traceparent.charAt(SPAN_ID_OFFSET - 1) == TRACEPARENT_DELIMITER && traceparent.charAt(TRACE_OPTION_OFFSET - 1) == TRACEPARENT_DELIMITER;
    if (!isValid) {
        logger.fine("Unparseable traceparent header. Returning INVALID span context.");
        return SpanContext.getInvalid();
    }
    String version = traceparent.substring(0, 2);
    if (!VALID_VERSIONS.contains(version)) {
        return SpanContext.getInvalid();
    }
    if (version.equals(VERSION_00) && traceparent.length() > TRACEPARENT_HEADER_SIZE) {
        return SpanContext.getInvalid();
    }
    String traceId = traceparent.substring(TRACE_ID_OFFSET, TRACE_ID_OFFSET + TraceId.getLength());
    String spanId = traceparent.substring(SPAN_ID_OFFSET, SPAN_ID_OFFSET + SpanId.getLength());
    char firstTraceFlagsChar = traceparent.charAt(TRACE_OPTION_OFFSET);
    char secondTraceFlagsChar = traceparent.charAt(TRACE_OPTION_OFFSET + 1);
    if (!OtelEncodingUtils.isValidBase16Character(firstTraceFlagsChar) || !OtelEncodingUtils.isValidBase16Character(secondTraceFlagsChar)) {
        return SpanContext.getInvalid();
    }
    TraceFlags traceFlags = TraceFlags.fromByte(OtelEncodingUtils.byteFromBase16(firstTraceFlagsChar, secondTraceFlagsChar));
    return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TraceState.getDefault());
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags)

Example 2 with TraceFlags

use of io.opentelemetry.api.trace.TraceFlags in project splunk-otel-java by signalfx.

the class SpanContextualizer method updateContext.

/**
 * This updates the tracked thread context for a given span. This must only be called with
 * ContextAttached events.
 */
public void updateContext(RecordedEvent event) {
    String traceId = event.getString("traceId");
    String spanId = event.getString("spanId");
    long javaThreadId = event.getThread().getJavaThreadId();
    logger.debug("Set thread context: [{}] {} {} at {}", javaThreadId, traceId, spanId, event.getStartTime());
    if (traceId == null || spanId == null) {
        threadSpans.remove(javaThreadId);
    } else {
        TraceFlags traceFlags = TraceFlags.fromByte(event.getByte("traceFlags"));
        SpanContext spanContext = SpanContext.create(traceId, spanId, traceFlags, TraceState.getDefault());
        SpanLinkage linkage = new SpanLinkage(spanContext, javaThreadId);
        threadSpans.put(javaThreadId, linkage);
    }
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags) SpanContext(io.opentelemetry.api.trace.SpanContext)

Aggregations

TraceFlags (io.opentelemetry.api.trace.TraceFlags)2 SpanContext (io.opentelemetry.api.trace.SpanContext)1