Search in sources :

Example 1 with Tracestate

use of io.opencensus.trace.Tracestate 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 2 with Tracestate

use of io.opencensus.trace.Tracestate 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)

Aggregations

SpanId (io.opencensus.trace.SpanId)2 TraceId (io.opencensus.trace.TraceId)2 TraceOptions (io.opencensus.trace.TraceOptions)2 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 SpanContextParseException (io.opencensus.trace.propagation.SpanContextParseException)1 Random (java.util.Random)1