Пример #1
0
        public void Sample_NoMatchingFilters()
        {
            // Both filters will fail to match the incoming HttpRequest here, and defaultSampler should collect the trace.
            var filter1        = new MockSamplingFilter(r => r != null);
            var filter2        = new MockSamplingFilter(r => r != null);
            var defaultSampler = new MockSampler();

            var unit = new RequestFilteringSampler(new ISamplingFilter[]
            {
                filter1,
                filter2
            },
                                                   defaultSampler,
                                                   Mock.Of <IHttpContextAccessor>(a => a.HttpContext == Mock.Of <HttpContext>(c => c.Request == null)));

            unit.Sample("TestOperation", new Jaeger.TraceId(1, 1));

            filter1.ShouldSampleCallLogs.Should().HaveCount(1, "because we only called unit.Sample once");
            filter2.ShouldSampleCallLogs.Should().HaveCount(1, "because we only called unit.Sample once");

            filter1.ShouldSampleCallLogs[0].HttpRequest.Should().BeNull("because our HttpContextAccessor supplied a null request");
            filter1.ShouldSampleCallLogs[0].Result.Should().BeFalse("because the filter should not match a null request");

            filter2.ShouldSampleCallLogs[0].HttpRequest.Should().BeNull("because our HttpContextAccessor supplied a null request");
            filter2.ShouldSampleCallLogs[0].Result.Should().BeFalse("because the filter should not match a null request");

            defaultSampler.Samples.Should().HaveCount(1, "because the sample passed over both filters so should go to our default");
            defaultSampler.Samples[0].Operation.Should().Be("TestOperation", "because this is the sample we sent to our sampler");
        }
Пример #2
0
        public void Sample_MultipleMatchingFilters()
        {
            // Both filters will match the incoming HttpRequest here, but the trace should go to only the first one
            var filter1        = new MockSamplingFilter(r => r == null);
            var filter2        = new MockSamplingFilter(r => r == null);
            var defaultSampler = new MockSampler();

            var unit = new RequestFilteringSampler(new ISamplingFilter[]
            {
                filter1,
                filter2
            },
                                                   defaultSampler,
                                                   Mock.Of <IHttpContextAccessor>(a => a.HttpContext == Mock.Of <HttpContext>(c => c.Request == null)));

            unit.Sample("TestOperation", new Jaeger.TraceId(1, 1));

            filter1.ShouldSampleCallLogs.Should().HaveCount(1, "because we only called unit.Sample once");
            filter2.ShouldSampleCallLogs.Should().BeEmpty(
                "because the only sample was matched by filter1, so should have never made it to a subsequent filter");

            filter1.ShouldSampleCallLogs[0].HttpRequest.Should().BeNull("because our HttpContextAccessor supplied a null request");
            filter1.ShouldSampleCallLogs[0].Result.Should().BeTrue("because the filter matches null request");
            filter1.TargetSamplerMock.Samples.Should().HaveCount(1, "because the filter matched the request, so we should have sampled it");
            filter1.TargetSamplerMock.Samples[0].Operation.Should().Be("TestOperation", "because this is the sample we sent to our sampler");

            defaultSampler.Samples.Should().BeEmpty(
                "because the only sample was matched by filter1, so should have never made it to the default sampler");
        }