public void TraceContextFormatCanParseExampleFromSpec()
        {
            Dictionary <string, string> headers = new Dictionary <string, string>()
            {
                { "traceparent", "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01" },
                { "tracestate", "congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4,rojo=00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01" },
            };

            var f   = new TraceContextFormat();
            var ctx = f.Extract(headers, (h, n) => new string[] { h[n] });

            Assert.Equal(TraceId.FromLowerBase16("0af7651916cd43dd8448eb211c80319c"), ctx.TraceId);
            Assert.Equal(SpanId.FromLowerBase16("b9c7c989f97918e1"), ctx.SpanId);
            Assert.True(ctx.TraceOptions.IsSampled);

            Assert.Equal(2, ctx.Tracestate.Entries.Count());

            var first = ctx.Tracestate.Entries.First();

            Assert.Equal("congo", first.Key);
            Assert.Equal("lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4", first.Value);

            var last = ctx.Tracestate.Entries.Last();

            Assert.Equal("rojo", last.Key);
            Assert.Equal("00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01", last.Value);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public SpanContext Extract <T>(T carrier, Func <T, string, IEnumerable <string> > getter)
        {
            if (carrier == null)
            {
                throw new ArgumentNullException(nameof(carrier));
            }

            if (getter == null)
            {
                throw new ArgumentNullException(nameof(getter));
            }

            try
            {
                TraceId traceId;
                var     traceIdStr = getter(carrier, XB3TraceId)?.FirstOrDefault();
                if (traceIdStr != null)
                {
                    if (traceIdStr.Length == TraceId.Size)
                    {
                        // This is an 8-byte traceID.
                        traceIdStr = UpperTraceId + traceIdStr;
                    }

                    traceId = TraceId.FromLowerBase16(traceIdStr);
                }
                else
                {
                    throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
                }

                SpanId spanId;
                var    spanIdStr = getter(carrier, XB3SpanId)?.FirstOrDefault();
                if (spanIdStr != null)
                {
                    spanId = SpanId.FromLowerBase16(spanIdStr);
                }
                else
                {
                    throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
                }

                var traceOptions = TraceOptions.Default;
                if (SampledValue.Equals(getter(carrier, XB3Sampled)?.FirstOrDefault()) ||
                    FlagsValue.Equals(getter(carrier, XB3Flags)?.FirstOrDefault()))
                {
                    traceOptions = TraceOptions.Builder().SetIsSampled(true).Build();
                }

                return(SpanContext.Create(traceId, spanId, traceOptions, Tracestate.Empty));
            }
            catch (Exception e)
            {
                throw new SpanContextParseException("Invalid input.", e);
            }
        }
Ejemplo n.º 3
0
        public override ISpanContext Extract <C>(C carrier, IGetter <C> getter)
        {
            if (carrier == null)
            {
                throw new ArgumentNullException(nameof(carrier));
            }

            if (getter == null)
            {
                throw new ArgumentNullException(nameof(getter));
            }

            try
            {
                ITraceId traceId;
                string   traceIdStr = getter.Get(carrier, X_B3_TRACE_ID);
                if (traceIdStr != null)
                {
                    if (traceIdStr.Length == TraceId.SIZE)
                    {
                        // This is an 8-byte traceID.
                        traceIdStr = UPPER_TRACE_ID + traceIdStr;
                    }

                    traceId = TraceId.FromLowerBase16(traceIdStr);
                }
                else
                {
                    throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
                }

                ISpanId spanId;
                string  spanIdStr = getter.Get(carrier, X_B3_SPAN_ID);
                if (spanIdStr != null)
                {
                    spanId = SpanId.FromLowerBase16(spanIdStr);
                }
                else
                {
                    throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
                }

                TraceOptions traceOptions = TraceOptions.DEFAULT;
                if (SAMPLED_VALUE.Equals(getter.Get(carrier, X_B3_SAMPLED)) ||
                    FLAGS_VALUE.Equals(getter.Get(carrier, X_B3_FLAGS)))
                {
                    traceOptions = TraceOptions.Builder().SetIsSampled(true).Build();
                }

                return(SpanContext.Create(traceId, spanId, traceOptions));
            }
            catch (Exception e)
            {
                throw new SpanContextParseException("Invalid input.", e);
            }
        }
Ejemplo n.º 4
0
 public void FromLowerBase16()
 {
     Assert.Equal(TraceId.Invalid, TraceId.FromLowerBase16("00000000000000000000000000000000"));
     Assert.Equal(first, TraceId.FromLowerBase16("00000000000000000000000000000061"));
     Assert.Equal(second, TraceId.FromLowerBase16("ff000000000000000000000000000041"));
 }
Ejemplo n.º 5
0
 public void FromLowerBase16()
 {
     Assert.Equal(TraceId.INVALID, TraceId.FromLowerBase16("00000000000000000000000000000000"));
     Assert.Equal(First, TraceId.FromLowerBase16("00000000000000000000000000000061"));
     Assert.Equal(Second, TraceId.FromLowerBase16("ff000000000000000000000000000041"));
 }