Search in sources :

Example 6 with TraceId

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

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

Example 8 with TraceId

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

the class SpanBuilderImplTest method startRemoteChildSpan_WithProbabilitySamplerDefaultSampler.

@Test
public void startRemoteChildSpan_WithProbabilitySamplerDefaultSampler() {
    when(traceConfig.getActiveTraceParams()).thenReturn(TraceParams.DEFAULT);
    // This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
    // is not less than probability * Long.MAX_VALUE;
    TraceId traceId = TraceId.fromBytes(new byte[] { (byte) 0x8F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0 });
    // If parent is sampled then the remote child must be sampled.
    Span childSpan = SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, SpanContext.create(traceId, SpanId.generateRandomId(randomHandler.current()), TraceOptions.builder().setIsSampled(true).build()), spanBuilderOptions).startSpan();
    assertThat(childSpan.getContext().isValid()).isTrue();
    assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId);
    assertThat(childSpan.getContext().getTraceOptions().isSampled()).isTrue();
    childSpan.end();
    assertThat(traceConfig.getActiveTraceParams()).isEqualTo(TraceParams.DEFAULT);
    // If parent is not sampled then the remote child must be not sampled.
    childSpan = SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, SpanContext.create(traceId, SpanId.generateRandomId(randomHandler.current()), TraceOptions.DEFAULT), spanBuilderOptions).startSpan();
    assertThat(childSpan.getContext().isValid()).isTrue();
    assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId);
    assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
    childSpan.end();
}
Also used : TraceId(io.opencensus.trace.TraceId) Span(io.opencensus.trace.Span) Test(org.junit.Test)

Aggregations

TraceId (io.opencensus.trace.TraceId)8 SpanId (io.opencensus.trace.SpanId)6 TraceOptions (io.opencensus.trace.TraceOptions)5 SpanContextParseException (io.opencensus.trace.propagation.SpanContextParseException)4 Span (io.opencensus.trace.Span)2 Tracestate (io.opencensus.trace.Tracestate)2 Test (org.junit.Test)2 Timestamp (io.opencensus.common.Timestamp)1 TimestampConverter (io.opencensus.implcore.internal.TimestampConverter)1 Span (io.opencensus.proto.trace.v1.Span)1 SpanKind (io.opencensus.proto.trace.v1.Span.SpanKind)1 Sampler (io.opencensus.trace.Sampler)1 Kind (io.opencensus.trace.Span.Kind)1 SpanContext (io.opencensus.trace.SpanContext)1 TraceParams (io.opencensus.trace.config.TraceParams)1 Random (java.util.Random)1