Example #1
0
        public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext)
            where TCarrierGetter : struct, ICarrierGetter <TCarrier>
        {
            spanContext = null;

            if (carrier is not IReadOnlyDictionary <string, string?> )
            {
                return(false);
            }

            var traceIdAsUint64 = ParseUtility.ParseUInt64(carrier, carrierGetter, SpanContext.Keys.TraceId);

            if (traceIdAsUint64 == null)
            {
                // a valid traceId is required to use distributed tracing
                return(false);
            }

            var traceId = TraceId.CreateFromUlong(traceIdAsUint64.Value);

            if (traceId == TraceId.Zero)
            {
                // a valid traceId is required to use distributed tracing
                return(false);
            }

            var parentId         = ParseUtility.ParseUInt64(carrier, carrierGetter, SpanContext.Keys.ParentId) ?? 0;
            var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, SpanContext.Keys.SamplingPriority);
            var origin           = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.Origin);
            var rawTraceId       = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.RawTraceId);
            var rawSpanId        = ParseUtility.ParseString(carrier, carrierGetter, SpanContext.Keys.RawSpanId);

            spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin, rawTraceId, rawSpanId);
            return(true);
        }
        public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext)
            where TCarrierGetter : struct, ICarrierGetter <TCarrier>
        {
            spanContext = null;

            var rawTraceId       = ParseUtility.ParseString(carrier, carrierGetter, TraceId)?.Trim();
            var rawSpanId        = ParseUtility.ParseString(carrier, carrierGetter, SpanId)?.Trim();
            var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, Sampled);

            if (IsValidTraceId(rawTraceId) && IsValidSpanId(rawSpanId))
            {
#if NETCOREAPP
                var traceId = rawTraceId.Length == 32 ?
                              ParseUtility.ParseFromHexOrDefault(rawTraceId.AsSpan(16)) :
                              ParseUtility.ParseFromHexOrDefault(rawTraceId);
#else
                var traceId = rawTraceId.Length == 32 ?
                              ParseUtility.ParseFromHexOrDefault(rawTraceId.Substring(16)) :
                              ParseUtility.ParseFromHexOrDefault(rawTraceId);
#endif

                if (traceId == 0)
                {
                    return(false);
                }

                var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId);

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId);
                return(true);
            }

            return(false);
        }
        public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext)
            where TCarrierGetter : struct, ICarrierGetter <TCarrier>
        {
            spanContext = null;
            var rawTraceId       = ParseUtility.ParseString(carrier, carrierGetter, TraceId)?.Trim();
            var rawSpanId        = ParseUtility.ParseString(carrier, carrierGetter, SpanId)?.Trim();
            var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, Sampled);

            if (IsValidTraceId(rawTraceId, out var traceId) && IsValidSpanId(rawSpanId))
            {
                var parentId = ParseUtility.ParseFromHexOrDefault(rawSpanId);

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId);
                return(true);
            }

            return(false);
        }
        public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext)
            where TCarrierGetter : struct, ICarrierGetter <TCarrier>
        {
            spanContext = null;

            var brValue = ParseUtility.ParseString(carrier, carrierGetter, B3)?.Trim();

            if (!string.IsNullOrEmpty(brValue))
            {
                // We found a trace parent (we are reading from the Http Headers)

                // 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90
                // 80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1
                // e457b5a2e4d86bd1-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90
                // e457b5a2e4d86bd1-e457b5a2e4d86bd1-1
                if (brValue !.Length != 68 && brValue !.Length != 51 &&
                    brValue !.Length != 52 && brValue !.Length != 35)
                {
                    return(false);
                }

#if NETCOREAPP
                ReadOnlySpan <char> rawTraceId = null;
                ReadOnlySpan <char> rawSpanId  = null;
                char rawSampled = '0';
                if (brValue.Length > 50 && brValue[32] == '-' && brValue[49] == '-')
                {
                    // 128 bits trace id
                    rawTraceId = brValue.AsSpan(0, 32);
                    rawSpanId  = brValue.AsSpan(33, 16);
                    rawSampled = brValue[50];
                }
                else if (brValue.Length > 34 && brValue[16] == '-' && brValue[33] == '-')
                {
                    // 64 bits trace id
                    rawTraceId = brValue.AsSpan(0, 16);
                    rawSpanId  = brValue.AsSpan(17, 16);
                    rawSampled = brValue[34];
                }
                else
                {
                    return(false);
                }

                var traceId = rawTraceId.Length == 32 ?
                              ParseUtility.ParseFromHexOrDefault(rawTraceId.Slice(16)) :
                              ParseUtility.ParseFromHexOrDefault(rawTraceId);
                var parentId         = ParseUtility.ParseFromHexOrDefault(rawSpanId);
                var samplingPriority = rawSampled == '1' ? 1 : 0;

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId.ToString(), rawSpanId.ToString());
#else
                string?rawTraceId = null;
                string?rawSpanId  = null;
                char   rawSampled = '0';
                if (brValue.Length > 50 && brValue[32] == '-' && brValue[49] == '-')
                {
                    // 128 bits trace id
                    rawTraceId = brValue.Substring(0, 32);
                    rawSpanId  = brValue.Substring(33, 16);
                    rawSampled = brValue[50];
                }
                else if (brValue.Length > 34 && brValue[16] == '-' && brValue[33] == '-')
                {
                    // 64 bits trace id
                    rawTraceId = brValue.Substring(0, 16);
                    rawSpanId  = brValue.Substring(17, 16);
                    rawSampled = brValue[34];
                }
                else
                {
                    return(false);
                }

                var traceId = rawTraceId.Length == 32 ?
                              ParseUtility.ParseFromHexOrDefault(rawTraceId.Substring(16)) :
                              ParseUtility.ParseFromHexOrDefault(rawTraceId);
                var parentId         = ParseUtility.ParseFromHexOrDefault(rawSpanId);
                var samplingPriority = rawSampled == '1' ? 1 : 0;

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId);
#endif

                return(true);
            }

            return(false);
        }
Example #5
0
        public bool TryExtract <TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out SpanContext?spanContext)
            where TCarrierGetter : struct, ICarrierGetter <TCarrier>
        {
            spanContext = null;

            var traceParent = ParseUtility.ParseString(carrier, carrierGetter, TraceParent)?.Trim();

            if (!string.IsNullOrEmpty(traceParent))
            {
                // We found a trace parent (we are reading from the Http Headers)

                /* (https://www.w3.org/TR/trace-context/)
                 *  Valid traceparent when caller sampled this request:
                 *
                 *  Value = 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
                 *  base16(version) = 00
                 *  base16(trace-id) = 4bf92f3577b34da6a3ce929d0e0e4736
                 *  base16(parent-id) = 00f067aa0ba902b7
                 *  base16(trace-flags) = 01  // sampled
                 *  Valid traceparent when caller didn’t sample this request:
                 *
                 *  Value = 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00
                 *  base16(version) = 00
                 *  base16(trace-id) = 4bf92f3577b34da6a3ce929d0e0e4736
                 *  base16(parent-id) = 00f067aa0ba902b7
                 *  base16(trace-flags) = 00  // not sampled
                 */

                if (traceParent !.Length != 55 || traceParent[2] != '-' || traceParent[35] != '-' || traceParent[52] != '-')
                {
                    return(false);
                }

                char w3cSampled = traceParent[54];
                if (traceParent[53] != '0' || (w3cSampled != '0' && w3cSampled != '1'))
                {
                    return(false);
                }

                var samplingPriority = w3cSampled == '0' ? 0 : 1;

#if NETCOREAPP
                var w3cTraceId = traceParent.AsSpan(3, 32);
                var w3cSpanId  = traceParent.AsSpan(36, 16);
                var traceId    = ParseUtility.ParseFromHexOrDefault(w3cTraceId.Slice(16));
                if (traceId == 0)
                {
                    return(false);
                }

                var parentId = ParseUtility.ParseFromHexOrDefault(w3cSpanId);

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, w3cTraceId.ToString(), w3cSpanId.ToString());
#else
                var w3cTraceId = traceParent.Substring(3, 32);
                var w3cSpanId  = traceParent.Substring(36, 16);
                var traceId    = ParseUtility.ParseFromHexOrDefault(w3cTraceId.Substring(16));
                if (traceId == 0)
                {
                    return(false);
                }

                var parentId = ParseUtility.ParseFromHexOrDefault(w3cSpanId);

                spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, w3cTraceId, w3cSpanId);
#endif

                return(true);
            }

            return(false);
        }