Example #1
0
        private ISpan StartSpanInternal(
            SpanContext parent,
            string name,
            ISampler sampler,
            IEnumerable <ISpan> parentLinks,
            bool recordEvents,
            Timer timestampConverter)
        {
            ITraceParams        activeTraceParams = this.Options.TraceConfig.ActiveTraceParams;
            IRandomGenerator    random            = this.Options.RandomHandler;
            ITraceId            traceId;
            ISpanId             spanId       = SpanId.GenerateRandomId(random);
            ISpanId             parentSpanId = null;
            TraceOptionsBuilder traceOptionsBuilder;

            if (parent == null || !parent.IsValid)
            {
                // New root span.
                traceId             = TraceId.GenerateRandomId(random);
                traceOptionsBuilder = TraceOptions.Builder();
            }
            else
            {
                // New child span.
                traceId             = parent.TraceId;
                parentSpanId        = parent.SpanId;
                traceOptionsBuilder = TraceOptions.Builder(parent.TraceOptions);
            }

            traceOptionsBuilder.SetIsSampled(
                MakeSamplingDecision(
                    parent,
                    name,
                    sampler,
                    parentLinks,
                    traceId,
                    spanId,
                    activeTraceParams));
            TraceOptions traceOptions = traceOptionsBuilder.Build();
            SpanOptions  spanOptions  = SpanOptions.None;

            if (traceOptions.IsSampled || recordEvents)
            {
                spanOptions = SpanOptions.RecordEvents;
            }

            ISpan span = Span.StartSpan(
                SpanContext.Create(traceId, spanId, traceOptions, parent?.Tracestate ?? Tracestate.Empty),
                spanOptions,
                name,
                this.Kind,
                parentSpanId,
                activeTraceParams,
                this.Options.StartEndHandler,
                timestampConverter);

            LinkSpans(span, parentLinks);
            return(span);
        }
Example #2
0
        /// <inheritdoc/>
        public ISpan StartSpan()
        {
            SpanContext         parentContext     = FindParent(this.parentType, this.parent, this.parentSpanContext);
            var                 activeTraceParams = this.options.TraceConfig.ActiveTraceParams;
            var                 random            = this.options.RandomHandler;
            TraceId             traceId;
            var                 spanId       = SpanId.GenerateRandomId(random);
            SpanId              parentSpanId = null;
            TraceOptionsBuilder traceOptionsBuilder;

            if (parentContext == null || !parentContext.IsValid)
            {
                // New root span.
                traceId             = TraceId.GenerateRandomId(random);
                traceOptionsBuilder = TraceOptions.Builder();
            }
            else
            {
                // New child span.
                traceId             = parentContext.TraceId;
                parentSpanId        = parentContext.SpanId;
                traceOptionsBuilder = TraceOptions.Builder(parentContext.TraceOptions);
            }

            traceOptionsBuilder.SetIsSampled(
                MakeSamplingDecision(
                    parentContext,
                    this.name,
                    this.sampler,
                    this.links,
                    traceId,
                    spanId,
                    activeTraceParams));
            var traceOptions = traceOptionsBuilder.Build();
            var spanOptions  = SpanOptions.None;

            if (traceOptions.IsSampled || this.recordEvents)
            {
                spanOptions = SpanOptions.RecordEvents;
            }

            var span = Span.StartSpan(
                SpanContext.Create(traceId, spanId, traceOptions, parentContext?.Tracestate ?? Tracestate.Empty),
                spanOptions,
                this.name,
                this.kind,
                parentSpanId,
                activeTraceParams,
                this.options.StartEndHandler,
                this.timestampConverter);

            LinkSpans(span, this.links);
            return(span);
        }