Ejemplo n.º 1
0
        /// <summary>
        /// Extract context from the underlying text map carrier
        /// </summary>
        /// <param name="carrier"></param>
        /// <returns></returns>
        public SpanContext Extract(ITextMap carrier)
        {
            SpanContext context = null;
            Dictionary <string, string> baggage = null;
            string debugId       = null;
            bool   contextParsed = false;

            foreach (var item in carrier.GetEntries())
            {
                if (item.Key.Equals(contextKey))
                {
                    contextParsed = SpanContext.TryParse(Decode(item.Value), out context);
                }
                else if (item.Key.Equals(Constants.DEBUG_ID_HEADER_KEY))
                {
                    debugId = Decode(item.Value);
                }
                else if (item.Key.StartsWith(baggagePrefix))
                {
                    if (baggage == null)
                    {
                        baggage = new Dictionary <string, string>();
                    }
                    baggage.Add(item.Key.MinusPrefix(baggagePrefix), Decode(item.Value));
                }
            }

            if (!contextParsed)
            {
                if (!string.IsNullOrWhiteSpace(debugId))
                {
                    return(SpanContext.Debug(debugId));
                }
                return(null);
            }
            if (baggage == null)
            {
                return(context);
            }
            return(context.SetBaggage(baggage));
        }