Search in sources :

Example 1 with TimestampConverter

use of io.opencensus.implcore.internal.TimestampConverter 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

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