/// <summary>
        /// Sets the baggage key:value on the <see cref="Span"/> and the corresponding
        /// logs. Whether the baggage is set on the span depends on if the key
        /// is allowed to be set by this service.
        /// <para/>
        /// A <see cref="SpanContext"/> is returned with the new baggage key:value set
        /// if key is valid, else returns the existing <see cref="SpanContext"/>
        /// on the <see cref="Span"/>.
        /// </summary>
        /// <param name="span">The span to set the baggage on.</param>
        /// <param name="key">The baggage key to set.</param>
        /// <param name="value">the baggage value to set.</param>
        /// <returns>The <see cref="SpanContext"/> with the baggage set.</returns>
        public SpanContext SetBaggage(Span span, string key, string value)
        {
            Restriction restriction = _restrictionManager.GetRestriction(span.Tracer.ServiceName, key);
            bool        truncated   = false;
            string      prevItem    = null;

            if (!restriction.KeyAllowed)
            {
                _metrics.BaggageUpdateFailure.Inc(1);
                LogFields(span, key, value, prevItem, truncated, restriction.KeyAllowed);
                return(span.Context);
            }

            if (value != null && value.Length > restriction.MaxValueLength)
            {
                truncated = true;
                value     = value.Substring(0, restriction.MaxValueLength);
                _metrics.BaggageTruncate.Inc(1);
            }

            prevItem = span.GetBaggageItem(key);
            LogFields(span, key, value, prevItem, truncated, restriction.KeyAllowed);
            _metrics.BaggageUpdateSuccess.Inc(1);

            return(span.Context.WithBaggageItem(key, value));
        }
Example #2
0
        public void TestInvalidBaggage()
        {
            _mgr.GetRestriction(Service, Key).Returns(new Restriction(false, 0));

            string      value = "value";
            SpanContext ctx   = _setter.SetBaggage(_span, Key, value);

            AssertBaggageLogs(_span, Key, value, false, false, true);
            Assert.Null(ctx.GetBaggageItem(Key));

            Assert.Equal(1, _metricsFactory.GetCounter("jaeger:baggage_updates", "result=err"));
        }
Example #3
0
        public void TestSetAndGetBaggageItem()
        {
            string service = "SamplerTest";
            IBaggageRestrictionManager mgr = Substitute.ForPartsOf <DefaultBaggageRestrictionManager>();
            var tracer = new Tracer.Builder(service)
                         .WithReporter(reporter)
                         .WithSampler(new ConstSampler(true))
                         .WithClock(clock)
                         .WithBaggageRestrictionManager(mgr)
                         .Build();
            var span = (Span)tracer.BuildSpan("some-operation").Start();

            string key   = "key";
            string value = "value";

            mgr.GetRestriction(service, key).Returns(new Restriction(true, 10));
            span.SetBaggageItem(key, "value");
            mgr.Received(1).GetRestriction(service, key);
            Assert.Equal(value, span.GetBaggageItem(key));
        }