Exemplo n.º 1
0
 protected override void Inject(SpanContext spanContext, ITextMap carrier)
 {
     carrier.Set(_contextKey, EncodedValue(spanContext.ContextAsString()));
     foreach (var entry in spanContext.GetBaggageItems())
     {
         carrier.Set(Keys.PrefixedKey(entry.Key, _baggagePrefix), EncodedValue(entry.Value));
     }
 }
Exemplo n.º 2
0
        public void SpanContext_Constructor_ShouldDefaultToEmptyBaggage_AndSampledFlag()
        {
            var traceId = new TraceId(1, 2);
            var t2      = new SpanContext(traceId);

            Assert.Equal(new Dictionary <string, string>(), t2.GetBaggageItems());
            Assert.Equal(ContextFlags.Sampled, t2.Flags);
        }
Exemplo n.º 3
0
        public void SpanContext_SetBaggageItems()
        {
            var traceId  = new TraceId(1, 2);
            var spanId   = new SpanId(3);
            var parentId = new SpanId(4);
            var baggage  = new Dictionary <string, string> {
                { "key", "value" }
            };

            var sc = new SpanContext(traceId, spanId, parentId, baggage);

            Assert.Equal(baggage, sc.GetBaggageItems());

            var newBaggage = new Dictionary <string, string> {
                { "new", "baggage" }
            };

            sc.SetBaggageItems(newBaggage);

            Assert.Equal(newBaggage, sc.GetBaggageItems());
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public void Inject <TCarrier>(SpanContext context, IFormat <TCarrier> format, TCarrier carrier)
        {
            if (carrier is ITextMap text)
            {
                foreach (var entry in context.GetBaggageItems())
                {
                    text.Set(Keys.BaggagePrefix + entry.Key, entry.Value);
                }

                text.Set(Keys.SpanId, context.SpanId);
                text.Set(Keys.TraceId, context.TraceId);
                text.Set(Keys.SampledId, "true");
            }
            else
            {
                throw new InvalidOperationException($"Unknown carrier {carrier.GetType()}");
            }
        }
Exemplo n.º 5
0
        public void SpanContext_Constructor_ShouldSetAllItemsPassedIn()
        {
            var traceId  = new TraceId(1, 2);
            var spanId   = new SpanId(3);
            var parentId = new SpanId(4);
            var baggage  = new Dictionary <string, string> {
                { "key", "value" }
            };
            ContextFlags flags = (ContextFlags)3;

            var t1 = new SpanContext(traceId, spanId, parentId, baggage, flags);

            Assert.Equal(traceId.High, t1.TraceId.High);
            Assert.Equal(traceId.Low, t1.TraceId.Low);
            Assert.Equal(spanId, t1.SpanId);
            Assert.Equal(parentId, t1.ParentId);
            Assert.Equal(baggage, t1.GetBaggageItems());
            Assert.Equal(flags, t1.Flags);
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public void Inject <TCarrier>(SpanContext context, IFormat <TCarrier> format, TCarrier carrier)
        {
            _logger.Trace($"Injecting {context} of {format.GetType()} to {carrier.GetType()}");
            if (carrier is ITextMap text)
            {
                foreach (var entry in context.GetBaggageItems())
                {
                    text.Set(Keys.BaggagePrefix + entry.Key, entry.Value);
                }

                text.Set(Keys.SpanId, Convert.ToUInt64(context.SpanId).ToString("X"));
                text.Set(Keys.TraceId, Convert.ToUInt64(context.TraceId).ToString("X"));
                text.Set(Keys.SampledId, "true");
            }
            else
            {
                _logger.Warn($"Unknown carrier during inject.");
                throw new InvalidOperationException($"Unknown carrier {carrier.GetType()}");
            }
        }
 /// <inheritdoc />
 public void Inject <TCarrier>(SpanContext context, IFormat <TCarrier> format, TCarrier carrier)
 {
     _logger.Trace($"Injecting {context} of {format.GetType()} to {carrier.GetType()}");
     if (carrier is IBinary s)
     {
         var ctx = new BinaryCarrier
         {
             BasicCtx = new BasicTracerCarrier
             {
                 SpanId = Convert.ToUInt64(context.SpanId), TraceId = Convert.ToUInt64(context.TraceId), Sampled = true
             }
         };
         foreach (var item in context.GetBaggageItems())
         {
             ctx.BasicCtx.BaggageItems.Add(item.Key, item.Value);
         }
         var ctxArray  = ctx.ToByteArray();
         var ctxStream = new MemoryStream(ctxArray);
         s.Set(ctxStream);
     }
 }