Пример #1
0
        public OpenTracingSpanContext NewRootSpanContext()
        {
            var traceId      = GuidFactory.Create();
            var spanId       = GuidFactory.Create();
            var shouldSample = ShouldSample(spanId);

            var baggage = new Dictionary <string, string>()
            {
            };

            return(new OpenTracingSpanContext(traceId, 0, spanId, shouldSample, baggage));
        }
Пример #2
0
        public OpenTracingSpanContext NewChildSpanContext(OpenTracingSpanContext spanContext)
        {
            var parentTraceContext = spanContext;

            var traceId      = parentTraceContext.TraceId;
            var parentId     = parentTraceContext.SpanId;
            var spanId       = GuidFactory.Create();
            var shouldSample = ShouldSample(spanId);

            var baggage = parentTraceContext.GetBaggageItems().ToDictionary(p => p.Key, p => p.Value);

            var childSpanContext = new OpenTracingSpanContext(traceId, parentId, spanId, shouldSample, baggage);

            return(childSpanContext);
        }
        public ContextMapToResult <OpenTracingSpanContext> MapTo(TextMapFormat data)
        {
            OpenTracingSpanContext spanContext = null;

            var lowercaseProperties = data.ToDictionary(p => p.Key.ToLower(), p => p.Value);

            if (!lowercaseProperties.ContainsKey(fieldNameTraceID) ||
                !lowercaseProperties.ContainsKey(fieldNameSpanID))
            {
                return(new ContextMapToResult <OpenTracingSpanContext>(new Exception("No key found for traceId and/or spanId")));
            }

            ulong traceId;
            var   traceIdParceResult = ulong.TryParse(lowercaseProperties[fieldNameTraceID], out traceId);

            ulong parentId;
            var   parentIdParseResult = ulong.TryParse(lowercaseProperties[fieldNameSpanID], out parentId);

            if (!traceIdParceResult || !parentIdParseResult)
            {
                return(new ContextMapToResult <OpenTracingSpanContext>(new Exception("Found but could not parse traceId and/or spanId")));
            }

            bool sampled;

            if (lowercaseProperties.ContainsKey(fieldNameSampled))
            {
                sampled = bool.TryParse(lowercaseProperties[fieldNameSampled], out sampled) ? sampled : false;
            }
            else
            {
                sampled = false;
            }

            var baggage = lowercaseProperties
                          .Where(p => p.Key.StartsWith(prefixBaggage))
                          .ToDictionary(p => p.Key.Substring(prefixBaggage.Length, p.Key.Length), p => p.Value);

            spanContext = new OpenTracingSpanContext(traceId, parentId, GuidFactory.Create(), sampled, baggage);

            return(new ContextMapToResult <OpenTracingSpanContext>(spanContext));
        }