예제 #1
0
        public void ShouldBeSampled()
        {
            // Arrange
            List <ShouldBeSampledCondition> testScenarios = new List <ShouldBeSampledCondition>()
            {
                // sampledFlag has a valid bool string value
                { new ShouldBeSampledCondition("0", null, 0, new List <string>(), false) },
                { new ShouldBeSampledCondition("1", null, 0, new List <string>(), true) },
                { new ShouldBeSampledCondition("false", null, 0, new List <string>(), false) },
                { new ShouldBeSampledCondition("true", null, 0, new List <string>(), true) },
                { new ShouldBeSampledCondition("FALSE", null, 0, new List <string>(), false) },
                { new ShouldBeSampledCondition("TRUE", null, 0, new List <string>(), true) },
                { new ShouldBeSampledCondition("FalSe", null, 0, new List <string>(), false) },
                { new ShouldBeSampledCondition("TrUe", null, 0, new List <string>(), true) },
                // sampledFlag has an invalid bool string value and requestPath is IsInDontSampleList
                { new ShouldBeSampledCondition(null, "/x", 0, new List <string> {
                        "/x"
                    }, false) },
                { new ShouldBeSampledCondition("", "/x", 0, new List <string> {
                        "/x"
                    }, false) },
                { new ShouldBeSampledCondition("invalidValue", "/x", 0, new List <string>()
                    {
                        "/x"
                    }, false) },
                // sampledFlag has an invalid bool string value, requestPath not in IsInDontSampleList, and sample rate is 0
                { new ShouldBeSampledCondition(null, null, 0, new List <string>(), false) },
                { new ShouldBeSampledCondition(null, "/x", 0, new List <string>(), false) },
                // sampledFlag has an invalid bool string value, requestPath not in IsInDontSampleList, and sample rate is 1
                { new ShouldBeSampledCondition(null, null, 1, new List <string>(), true) },
            };

            foreach (var testScenario in testScenarios)
            {
                var fixture = new Fixture();
                _sut = new ZipkinConfig
                {
                    ExcludedPathList = testScenario.ExcludedPathList,
                    SampleRate       = testScenario.SampleRate
                };

                // Act
                var result = _sut.ShouldBeSampled(testScenario.SampledFlag, testScenario.RequestPath);

                // Assert
                Assert.AreEqual(
                    testScenario.ExpectedOutcome,
                    result,
                    "Scenario: " +
                    $"SampledFlag({testScenario.SampledFlag ?? "null"}), " +
                    $"RequestPath({testScenario.RequestPath ?? "null"}), " +
                    $"SampleRate({testScenario.SampleRate}), " +
                    $"ExcludedPathList({string.Join(",", testScenario.ExcludedPathList)}),");
            }
        }
예제 #2
0
        public bool ShouldBeSampled(string sampledFlag, string requestPath, double sampleRate, string excludedPath)
        {
            var config = new ZipkinConfig(new Uri("http://localhost"))
            {
                SampleRate = sampleRate
            };

            if (excludedPath != null)
            {
                config.ExcludedPathList.Add(excludedPath);
            }

            return(config.ShouldBeSampled(sampledFlag, requestPath));
        }
예제 #3
0
        private TraceInfo ReadTraceInfo(HttpContext context)
        {
            string headerTraceId      = context.Request.Headers[TraceInfo.TraceIdHeaderName];
            string headerSpanId       = context.Request.Headers[TraceInfo.SpanIdHeaderName];
            string headerParentSpanId = context.Request.Headers[TraceInfo.ParentSpanIdHeaderName];
            string headerSampled      = context.Request.Headers[TraceInfo.SampledHeaderName];
            var    requestPath        = context.Request.Path.ToString();

            var traceId      = headerTraceId.IsParsableTo128Or64Bit() ? headerTraceId : TraceIdHelper.GenerateNewTraceId(_zipkinConfig.Create128BitTraceId);
            var spanId       = headerSpanId.IsParsableToLong() ? headerSpanId : TraceIdHelper.GenerateHexEncodedInt64Id();
            var parentSpanId = headerParentSpanId.IsParsableToLong() ? headerParentSpanId : string.Empty;
            var isSampled    = _zipkinConfig.ShouldBeSampled(headerSampled, requestPath);
            var domain       = _zipkinConfig.Domain(context.Request);
            var isJoinedSpan = spanId.Equals(headerSpanId);

            var traceInfo = new TraceInfo(traceId, spanId, isSampled, isJoinedSpan, domain, context.Connection.LocalIpAddress, parentSpanId);

            context.Items[TraceInfo.TraceInfoKey] = traceInfo;
            _traceInfoAccessor.TraceInfo          = traceInfo;

            return(traceInfo);
        }