Пример #1
0
        // TODO: Make another constructor to accept System.Web.HttpContext for non Owin applications
        /// <summary>
        /// Initializes a new instance of the TraceProvider class.
        /// </summary>
        /// <param name="config">ZipkinConfig instance</param>
        /// <param name="context">the IOwinContext</param>
        internal TraceProvider(IZipkinConfig config, HttpContext context = null)
        {
            string headerTraceId      = null;
            string headerSpanId       = null;
            string headerParentSpanId = null;
            string headerSampled      = null;
            string requestPath        = null;

            if (context != null)
            {
                object value = context.Items[Key];
                if (value != null)
                {
                    // set properties from context's item.
                    var provider = (ITraceProvider)value;
                    TraceId      = provider.TraceId;
                    SpanId       = provider.SpanId;
                    ParentSpanId = provider.ParentSpanId;
                    IsSampled    = provider.IsSampled;
                    return;
                }

                // zipkin use the following X-Headers to propagate the trace information
                headerTraceId      = context.Request.Headers[TraceIdHeaderName];
                headerSpanId       = context.Request.Headers[SpanIdHeaderName];
                headerParentSpanId = context.Request.Headers[ParentSpanIdHeaderName];
                headerSampled      = context.Request.Headers[SampledHeaderName];

                requestPath = context.Request.Path.ToString();
            }

            TraceId      = headerTraceId.IsParsableTo128Or64Bit() ? headerTraceId : GenerateNewTraceId(config.Create128BitTraceId);
            SpanId       = headerSpanId.IsParsableToLong() ? headerSpanId : GenerateHexEncodedInt64Id();
            ParentSpanId = headerParentSpanId.IsParsableToLong() ? headerParentSpanId : string.Empty;
            IsSampled    = config.ShouldBeSampled(headerSampled, requestPath);

            if (SpanId == ParentSpanId)
            {
                throw new ArgumentException("x-b3-SpanId and x-b3-ParentSpanId must not be the same value.");
            }

            context?.Items.Add(Key, this);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the TraceProvider class.
        /// </summary>
        /// <param name="config">ZipkinConfig instance</param>
        /// <param name="context">the IOwinContext</param>
        internal TraceProvider(IZipkinConfig config, IOwinContext context = null)
        {
            string headerTraceId      = null;
            string headerSpanId       = null;
            string headerParentSpanId = null;
            string headerSampled      = null;

            if (context != null)
            {
                object value;
                if (context.Environment.TryGetValue(Key, out value))
                {
                    // set properties from context's item.
                    var provider = (ITraceProvider)value;
                    TraceId      = provider.TraceId;
                    SpanId       = provider.SpanId;
                    ParentSpanId = provider.ParentSpanId;
                    IsSampled    = provider.IsSampled;
                    return;
                }

                // zipkin use the following X-Headers to propagate the trace information
                headerTraceId      = context.Request.Headers[TraceIdHeaderName];
                headerSpanId       = context.Request.Headers[SpanIdHeaderName];
                headerParentSpanId = context.Request.Headers[ParentSpanIdHeaderName];
                headerSampled      = context.Request.Headers[SampledHeaderName];
            }

            TraceId      = Parse(headerTraceId) ? headerTraceId : GenerateHexEncodedInt64FromNewGuid();
            SpanId       = Parse(headerSpanId) ? headerSpanId : TraceId;
            ParentSpanId = Parse(headerParentSpanId) ? headerParentSpanId : string.Empty;
            IsSampled    = config.ShouldBeSampled(context, headerSampled);

            if (SpanId == ParentSpanId)
            {
                throw new ArgumentException("x-b3-SpanId and x-b3-ParentSpanId must not be the same value.");
            }

            context?.Environment.Add(Key, this);
        }