Search in sources :

Example 1 with TraceOptions

use of io.opencensus.trace.TraceOptions in project instrumentation-java by census-instrumentation.

the class BinaryFormatImpl method fromByteArray.

@Override
public SpanContext fromByteArray(byte[] bytes) throws SpanContextParseException {
    checkNotNull(bytes, "bytes");
    if (bytes.length == 0 || bytes[0] != VERSION_ID) {
        throw new SpanContextParseException("Unsupported version.");
    }
    if (bytes.length < REQUIRED_FORMAT_LENGTH) {
        throw new SpanContextParseException("Invalid input: truncated");
    }
    // TODO: the following logic assumes that fields are written in ID order. The spec does not say
    // that. If it decides not to, this logic would need to be more like a loop
    TraceId traceId;
    SpanId spanId;
    TraceOptions traceOptions = TraceOptions.DEFAULT;
    int pos = 1;
    if (bytes[pos] == TRACE_ID_FIELD_ID) {
        traceId = TraceId.fromBytes(bytes, pos + ID_SIZE);
        pos += ID_SIZE + TraceId.SIZE;
    } else {
        // TODO: update the spec to suggest that the trace ID is not actually optional
        throw new SpanContextParseException("Invalid input: expected trace ID at offset " + pos);
    }
    if (bytes[pos] == SPAN_ID_FIELD_ID) {
        spanId = SpanId.fromBytes(bytes, pos + ID_SIZE);
        pos += ID_SIZE + SpanId.SIZE;
    } else {
        // TODO: update the spec to suggest that the span ID is not actually optional.
        throw new SpanContextParseException("Invalid input: expected span ID at offset " + pos);
    }
    // failing.
    if (bytes.length > pos && bytes[pos] == TRACE_OPTION_FIELD_ID) {
        if (bytes.length < ALL_FORMAT_LENGTH) {
            throw new SpanContextParseException("Invalid input: truncated");
        }
        traceOptions = TraceOptions.fromByte(bytes[pos + ID_SIZE]);
    }
    return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) SpanId(io.opencensus.trace.SpanId)

Example 2 with TraceOptions

use of io.opencensus.trace.TraceOptions in project instrumentation-java by census-instrumentation.

the class TraceContextFormat method extract.

@Override
public <C> /*>>> extends @NonNull Object*/
SpanContext extract(C carrier, Getter<C> getter) throws SpanContextParseException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    TraceId traceId;
    SpanId spanId;
    TraceOptions traceOptions;
    String traceparent = getter.get(carrier, TRACEPARENT);
    if (traceparent == null) {
        throw new SpanContextParseException("Traceparent not present");
    }
    try {
        // TODO(bdrutu): Do we need to verify that version is hex and that for the version
        // the length is the expected one?
        checkArgument(traceparent.charAt(TRACE_OPTION_OFFSET - 1) == TRACEPARENT_DELIMITER && (traceparent.length() == TRACEPARENT_HEADER_SIZE || (traceparent.length() > TRACEPARENT_HEADER_SIZE && traceparent.charAt(TRACEPARENT_HEADER_SIZE) == TRACEPARENT_DELIMITER)) && traceparent.charAt(SPAN_ID_OFFSET - 1) == TRACEPARENT_DELIMITER && traceparent.charAt(TRACE_OPTION_OFFSET - 1) == TRACEPARENT_DELIMITER, "Missing or malformed TRACEPARENT.");
        traceId = TraceId.fromLowerBase16(traceparent, TRACE_ID_OFFSET);
        spanId = SpanId.fromLowerBase16(traceparent, SPAN_ID_OFFSET);
        traceOptions = TraceOptions.fromLowerBase16(traceparent, TRACE_OPTION_OFFSET);
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid traceparent: " + traceparent, e);
    }
    String tracestate = getter.get(carrier, TRACESTATE);
    try {
        if (tracestate == null || tracestate.isEmpty()) {
            return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
        }
        Tracestate.Builder tracestateBuilder = Tracestate.builder();
        List<String> listMembers = TRACESTATE_ENTRY_DELIMITER_SPLITTER.splitToList(tracestate);
        checkArgument(listMembers.size() <= TRACESTATE_MAX_MEMBERS, "Tracestate has too many elements.");
        // front of the list.
        for (int i = listMembers.size() - 1; i >= 0; i--) {
            String listMember = listMembers.get(i);
            int index = listMember.indexOf(TRACESTATE_KEY_VALUE_DELIMITER);
            checkArgument(index != -1, "Invalid tracestate list-member format.");
            tracestateBuilder.set(listMember.substring(0, index), listMember.substring(index + 1, listMember.length()));
        }
        return SpanContext.create(traceId, spanId, traceOptions, tracestateBuilder.build());
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid tracestate: " + tracestate, e);
    }
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) Tracestate(io.opencensus.trace.Tracestate) SpanId(io.opencensus.trace.SpanId)

Example 3 with TraceOptions

use of io.opencensus.trace.TraceOptions in project instrumentation-java by census-instrumentation.

the class CloudTraceFormat method extract.

@Override
public <C> /*>>> extends @NonNull Object*/
SpanContext extract(C carrier, Getter<C> getter) throws SpanContextParseException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    try {
        String headerStr = getter.get(carrier, HEADER_NAME);
        if (headerStr == null || headerStr.length() < MIN_HEADER_SIZE) {
            throw new SpanContextParseException("Missing or too short header: " + HEADER_NAME);
        }
        checkArgument(headerStr.charAt(TRACE_ID_SIZE) == SPAN_ID_DELIMITER, "Invalid TRACE_ID size");
        TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0, TRACE_ID_SIZE));
        int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, TRACE_ID_SIZE);
        CharSequence spanIdStr = headerStr.subSequence(SPAN_ID_START_POS, traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);
        SpanId spanId = longToSpanId(UnsignedLongs.parseUnsignedLong(spanIdStr.toString(), 10));
        TraceOptions traceOptions = OPTIONS_NOT_SAMPLED;
        if (traceOptionsPos > 0) {
            String traceOptionsStr = headerStr.substring(traceOptionsPos + TRACE_OPTION_DELIMITER_SIZE);
            if ((UnsignedInts.parseUnsignedInt(traceOptionsStr, 10) & CLOUD_TRACE_IS_SAMPLED) != 0) {
                traceOptions = OPTIONS_SAMPLED;
            }
        }
        return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid input", e);
    }
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) SpanId(io.opencensus.trace.SpanId)

Example 4 with TraceOptions

use of io.opencensus.trace.TraceOptions in project instrumentation-java by census-instrumentation.

the class SpanBuilderImpl method startSpanInternal.

private Span startSpanInternal(@Nullable SpanContext parentContext, @Nullable Boolean hasRemoteParent, String name, @Nullable Sampler sampler, List<Span> parentLinks, @Nullable Boolean recordEvents, @Nullable Kind kind, @Nullable Span parentSpan) {
    TraceParams activeTraceParams = options.traceConfig.getActiveTraceParams();
    Random random = options.randomHandler.current();
    TraceId traceId;
    SpanId spanId = SpanId.generateRandomId(random);
    SpanId parentSpanId = null;
    // TODO(bdrutu): Handle tracestate correctly not just propagate.
    Tracestate tracestate = TRACESTATE_DEFAULT;
    if (parentContext == null || !parentContext.isValid()) {
        // New root span.
        traceId = TraceId.generateRandomId(random);
        // This is a root span so no remote or local parent.
        hasRemoteParent = null;
    } else {
        // New child span.
        traceId = parentContext.getTraceId();
        parentSpanId = parentContext.getSpanId();
        tracestate = parentContext.getTracestate();
    }
    TraceOptions traceOptions = makeSamplingDecision(parentContext, hasRemoteParent, name, sampler, parentLinks, traceId, spanId, activeTraceParams) ? SAMPLED_TRACE_OPTIONS : NOT_SAMPLED_TRACE_OPTIONS;
    if (traceOptions.isSampled() || Boolean.TRUE.equals(recordEvents)) {
        // Pass the timestamp converter from the parent to ensure that the recorded events are in
        // the right order. Implementation uses System.nanoTime() which is monotonically increasing.
        TimestampConverter timestampConverter = null;
        if (parentSpan instanceof RecordEventsSpanImpl) {
            RecordEventsSpanImpl parentRecordEventsSpan = (RecordEventsSpanImpl) parentSpan;
            timestampConverter = parentRecordEventsSpan.getTimestampConverter();
            parentRecordEventsSpan.addChild();
        }
        Span span = RecordEventsSpanImpl.startSpan(SpanContext.create(traceId, spanId, traceOptions, tracestate), name, kind, parentSpanId, hasRemoteParent, activeTraceParams, options.startEndHandler, timestampConverter, options.clock);
        linkSpans(span, parentLinks);
        return span;
    } else {
        return NoRecordEventsSpanImpl.create(SpanContext.create(traceId, spanId, traceOptions, tracestate));
    }
}
Also used : Random(java.util.Random) TimestampConverter(io.opencensus.implcore.internal.TimestampConverter) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) TraceParams(io.opencensus.trace.config.TraceParams) Tracestate(io.opencensus.trace.Tracestate) Span(io.opencensus.trace.Span) SpanId(io.opencensus.trace.SpanId)

Example 5 with TraceOptions

use of io.opencensus.trace.TraceOptions in project instrumentation-java by census-instrumentation.

the class B3Format method extract.

@Override
public <C> /*>>> extends @NonNull Object*/
SpanContext extract(C carrier, Getter<C> getter) throws SpanContextParseException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    try {
        TraceId traceId;
        String traceIdStr = getter.get(carrier, X_B3_TRACE_ID);
        if (traceIdStr != null) {
            if (traceIdStr.length() == TraceId.SIZE) {
                // This is an 8-byte traceID.
                traceIdStr = UPPER_TRACE_ID + traceIdStr;
            }
            traceId = TraceId.fromLowerBase16(traceIdStr);
        } else {
            throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
        }
        SpanId spanId;
        String spanIdStr = getter.get(carrier, X_B3_SPAN_ID);
        if (spanIdStr != null) {
            spanId = SpanId.fromLowerBase16(spanIdStr);
        } else {
            throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
        }
        TraceOptions traceOptions = TraceOptions.DEFAULT;
        if (SAMPLED_VALUE.equals(getter.get(carrier, X_B3_SAMPLED)) || FLAGS_VALUE.equals(getter.get(carrier, X_B3_FLAGS))) {
            traceOptions = TraceOptions.builder().setIsSampled(true).build();
        }
        return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid input.", e);
    }
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) SpanId(io.opencensus.trace.SpanId)

Aggregations

SpanId (io.opencensus.trace.SpanId)5 TraceId (io.opencensus.trace.TraceId)5 TraceOptions (io.opencensus.trace.TraceOptions)5 SpanContextParseException (io.opencensus.trace.propagation.SpanContextParseException)4 Tracestate (io.opencensus.trace.Tracestate)2 TimestampConverter (io.opencensus.implcore.internal.TimestampConverter)1 Span (io.opencensus.trace.Span)1 TraceParams (io.opencensus.trace.config.TraceParams)1 Random (java.util.Random)1