public void CreateFromString_CreatesIdCorrectly()
        {
            var traceId     = TraceId.CreateRandom();
            var recreatedId = TraceId.CreateFromString(traceId.ToString());

            recreatedId.Should().Be(traceId);
        }
        private void RunSamplerTest(
            ISampler sampler,
            int iterations,
            float expectedAutoKeepRate,
            float acceptableVariancePercent)
        {
            var sampleSize  = iterations;
            var autoKeeps   = 0;
            int seed        = new Random().Next();
            var idGenerator = new SpanIdGenerator(seed);

            while (sampleSize-- > 0)
            {
                var traceId  = TraceId.CreateRandom();
                var span     = GetMyServiceSpan(traceId);
                var priority = sampler.GetSamplingPriority(span);
                if (priority == SamplingPriority.AutoKeep)
                {
                    autoKeeps++;
                }
            }

            var autoKeepRate = autoKeeps / (float)iterations;

            var lowerLimit = expectedAutoKeepRate * (1 - acceptableVariancePercent);
            var upperLimit = expectedAutoKeepRate * (1 + acceptableVariancePercent);

            Assert.True(
                autoKeepRate >= lowerLimit && autoKeepRate <= upperLimit,
                $"Expected between {lowerLimit} and {upperLimit}, actual rate is {autoKeepRate}. Random generator seeded with {seed}.");
        }
        public void CreateRandom_CreatesValid128BitId()
        {
            var traceId = TraceId.CreateRandom();

            using (new AssertionScope())
            {
                traceId.ToString().Should().HaveLength(32);
                FluentActions.Invoking(() => Convert.ToUInt64(traceId.ToString().Substring(startIndex: 0, length: 16), fromBase: 16)).Should().NotThrow();
                FluentActions.Invoking(() => Convert.ToUInt64(traceId.ToString().Substring(startIndex: 16, length: 16), fromBase: 16)).Should().NotThrow();
            }
        }
        public void Equals_WorksCorrectlyFor128BitId()
        {
            var traceId1 = TraceId.CreateRandom();
            var traceId2 = TraceId.CreateRandom();

            using (new AssertionScope())
            {
                traceId1.Should().Be(TraceId.CreateFromString(traceId1.ToString()));
                traceId2.Should().Be(TraceId.CreateFromString(traceId2.ToString()));
                traceId1.Should().NotBe(TraceId.CreateFromString(traceId2.ToString()));
                traceId2.Should().NotBe(TraceId.CreateFromString(traceId1.ToString()));
            }
        }
Exemple #5
0
        public void SetHeaders_InjectsTheHeadersCorrectly(IHeadersCollection headers)
        {
            var traceId     = TraceId.CreateRandom();
            var spanContext = new SpanContext(traceId, 123, SamplingPriority.AutoKeep);

            ServerTimingHeader.SetHeaders(spanContext, headers, (h, name, value) => h.Add(name, value));

            using (new AssertionScope())
            {
                headers.GetValues("Server-Timing").Should().HaveCount(1);
                headers.GetValues("Server-Timing").Should().Equal($"traceparent;desc=\"00-{traceId.ToString()}-000000000000007b-01\"");
                headers.GetValues("Access-Control-Expose-Headers").Should().HaveCount(1);
                headers.GetValues("Access-Control-Expose-Headers").Should().Equal("Server-Timing");
            }
        }
 public TraceId GenerateNewTraceId() => TraceId.CreateRandom();
        public void Lower_Returns64LowerBitsOfId()
        {
            var traceId = TraceId.CreateRandom();

            traceId.Lower.Should().Be(Convert.ToInt64(traceId.ToString().Substring(startIndex: 16, length: 16), fromBase: 16));
        }
        public void Equals_WorksCorrectly()
        {
            var traceId1 = TraceId.CreateRandom();

            traceId1.Should().Be(TraceId.CreateFromString(traceId1.ToString()));
        }