Exemple #1
0
        /// <summary>
        /// Replace <see cref="Sampler"/> with a new instance when parameters are updated.
        /// </summary>
        /// <param name="response">Response which contains either a <see cref="ProbabilisticSampler"/>
        /// or <see cref="RateLimitingSampler"/>.</param>
        private void UpdateRateLimitingOrProbabilisticSampler(SamplingStrategyResponse response)
        {
            ISampler sampler;

            if (response.ProbabilisticSampling != null)
            {
                ProbabilisticSamplingStrategy strategy = response.ProbabilisticSampling;
                sampler = new ProbabilisticSampler(strategy.SamplingRate);
            }
            else if (response.RateLimitingSampling != null)
            {
                RateLimitingSamplingStrategy strategy = response.RateLimitingSampling;
                sampler = new RateLimitingSampler(strategy.MaxTracesPerSecond);
            }
            else
            {
                _metrics.SamplerParsingFailure.Inc(1);
                _logger.LogError("No strategy present in response. Not updating sampler.");
                return;
            }

            lock (_lock)
            {
                if (!Sampler.Equals(sampler))
                {
                    Sampler.Close();
                    Sampler = sampler;
                    _metrics.SamplerUpdated.Inc(1);
                }
            }
        }
        public void TestParseRateLimitingSampling()
        {
            SamplingStrategyResponse response = _undertest.ParseJson(ReadFixture("ratelimiting_sampling.json"));

            Assert.Equal(new RateLimitingSamplingStrategy(2.1), response.RateLimitingSampling);
            Assert.Null(response.ProbabilisticSampling);
        }
        public async Task TestGetSamplingStrategy()
        {
            _httpClient.MakeGetRequestAsync("http://www.example.com/sampling?service=clairvoyant")
            .Returns("{\"strategyType\":\"PROBABILISTIC\",\"probabilisticSampling\":{\"samplingRate\":0.001},\"rateLimitingSampling\":null}");

            SamplingStrategyResponse response = await _undertest.GetSamplingStrategyAsync("clairvoyant");

            Assert.NotNull(response.ProbabilisticSampling);
        }
        public async Task TestAllowHostPortSyntax()
        {
            var instance = new HttpSamplingManager(_httpClient, "example.com:80");

            _httpClient.MakeGetRequestAsync("http://example.com/?service=clairvoyant")
            .Returns("{\"strategyType\":\"PROBABILISTIC\",\"probabilisticSampling\":{\"samplingRate\":0.001},\"rateLimitingSampling\":null}");

            SamplingStrategyResponse response = await instance.GetSamplingStrategyAsync("clairvoyant");

            Assert.NotNull(response.ProbabilisticSampling);
        }
Exemple #5
0
        public void TestUpdateToProbabilisticSampler()
        {
            double samplingRate = 0.55;
            SamplingStrategyResponse probabilisticResponse = new SamplingStrategyResponse(
                new ProbabilisticSamplingStrategy(samplingRate), null, null);

            _samplingManager.GetSamplingStrategyAsync(SERVICE_NAME).Returns(probabilisticResponse);

            _undertest.UpdateSampler();

            Assert.Equal(new ProbabilisticSampler(samplingRate), _undertest.Sampler);
        }
Exemple #6
0
        public void TestUpdateToRateLimitingSampler()
        {
            short tracesPerSecond = 22;
            SamplingStrategyResponse rateLimitingResponse = new SamplingStrategyResponse(null,
                                                                                         new RateLimitingSamplingStrategy(tracesPerSecond), null);

            _samplingManager.GetSamplingStrategyAsync(SERVICE_NAME).Returns(rateLimitingResponse);

            _undertest.UpdateSampler();

            Assert.Equal(new RateLimitingSampler(tracesPerSecond), _undertest.Sampler);
        }
        public void UpdateSampler_ShouldHandleNoMatchingStrategy()
        {
            //var expectedLogMessage = "No strategy present in response. Not updating sampler.";
            var ssResponse = new SamplingStrategyResponse();

            _mockSamplingManager.GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName)).Returns(ssResponse);

            _testingSampler.UpdateSampler();

            _mockSamplerFactory.Received(0).NewRateLimitingSampler(Arg.Any <short>());
            _mockSamplerFactory.Received(0).NewProbabilisticSampler(Arg.Any <double>());
            _mockMetrics.Received(0).SamplerUpdated.Inc(Arg.Is <long>(d => d == 1));
            _mockMetrics.Received(1).SamplerParsingFailure.Inc(Arg.Is <long>(d => d == 1));
            // cannot mock extension methods :/ _mockLogger.Received(1).LogError(Arg.Is<string>(lm => lm == expectedLogMessage));
            //_mockLogger.Received(1).Log(Arg.Any<LogLevel>(), Arg.Any<EventId>(), Arg.Any<string>(), Arg.Any<Exception>(), Arg.Any<Func<object, Exception, string>>());
        }
        public void TestParsePerOperationSampling()
        {
            SamplingStrategyResponse    response = _undertest.ParseJson(ReadFixture("per_operation_sampling.json"));
            OperationSamplingParameters actual   = response.OperationSampling;

            Assert.Equal(0.001, actual.DefaultSamplingProbability, 4);
            Assert.Equal(0.001666, actual.DefaultLowerBoundTracesPerSecond, 4);

            List <PerOperationSamplingParameters> actualPerOperationStrategies = actual.PerOperationStrategies;

            Assert.Equal(2, actualPerOperationStrategies.Count);
            Assert.Equal(
                new PerOperationSamplingParameters("GET:/search", new ProbabilisticSamplingStrategy(1.0)),
                actualPerOperationStrategies[0]);
            Assert.Equal(
                new PerOperationSamplingParameters("PUT:/pacifique", new ProbabilisticSamplingStrategy(0.8258308134813166)),
                actualPerOperationStrategies[1]);
        }
        public void UpdateRateLimitingOrProbabilisticSampler_ShouldNotUpdateWhenSamplersAreTheSame()
        {
            var ssResponse = new SamplingStrategyResponse
            {
                RateLimitingSampling = new RateLimitingSamplingStrategy {
                    MaxTracesPerSecond = 10
                }
            };

            _mockSamplingManager.GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName)).Returns(ssResponse);
            _mockSamplerFactory.NewRateLimitingSampler(Arg.Any <short>()).Returns(_mockSampler);

            _testingSampler.UpdateSampler();

            _mockSamplerFactory.Received(1)
            .NewRateLimitingSampler(Arg.Is <short>(mt => mt == ssResponse.RateLimitingSampling.MaxTracesPerSecond));
            _mockMetrics.Received(0).SamplerUpdated.Inc(Arg.Is <long>(d => d == 1));
        }
        public void UpdateSampler_ShouldHandleProbSampling()
        {
            var ssResponse = new SamplingStrategyResponse
            {
                ProbabilisticSampling = new ProbabilisticSamplingStrategy {
                    SamplingRate = 0.75
                }
            };

            _mockSamplingManager.GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName)).Returns(ssResponse);

            _testingSampler.UpdateSampler();

            _mockSamplingManager.Received(1).GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName));
            _mockMetrics.Received(1).SamplerRetrieved.Inc(Arg.Any <long>());
            _mockSamplerFactory.Received(1)
            .NewProbabilisticSampler(Arg.Is <double>(sr => sr == ssResponse.ProbabilisticSampling.SamplingRate));
        }
        public void UpdateSampler_ShouldHandleRateLimSampling()
        {
            var ssResponse = new SamplingStrategyResponse
            {
                RateLimitingSampling = new RateLimitingSamplingStrategy {
                    MaxTracesPerSecond = 10
                }
            };

            _mockSamplingManager.GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName)).Returns(ssResponse);

            _testingSampler.UpdateSampler();

            _mockSamplingManager.Received(1).GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName));
            _mockMetrics.Received(1).SamplerRetrieved.Inc(Arg.Any <long>());
            _mockSamplerFactory.Received(1)
            .NewRateLimitingSampler(Arg.Is <short>(mt => mt == ssResponse.RateLimitingSampling.MaxTracesPerSecond));
        }
Exemple #12
0
        public void TestUpdateToPerOperationSamplerReplacesProbabilisticSampler()
        {
            var operationToSampler = new List <PerOperationSamplingParameters>();

            operationToSampler.Add(new PerOperationSamplingParameters("operation",
                                                                      new ProbabilisticSamplingStrategy(0.1)));
            OperationSamplingParameters parameters = new OperationSamplingParameters(0.11, 0.22, operationToSampler);
            SamplingStrategyResponse    response   = new SamplingStrategyResponse(null,
                                                                                  null, parameters);

            _samplingManager.GetSamplingStrategyAsync(SERVICE_NAME).Returns(response);

            _undertest.UpdateSampler();

            PerOperationSampler perOperationSampler = new PerOperationSampler(2000, parameters, _loggerFactory);
            ISampler            actualSampler       = _undertest.Sampler;

            Assert.Equal(perOperationSampler, actualSampler);
        }
Exemple #13
0
        /// <summary>
        /// Updates <see cref="Sampler"/> to a new sampler when it is different.
        /// </summary>
        internal void UpdateSampler()
        {
            try
            {
                SamplingStrategyResponse response = _samplingManager.GetSamplingStrategyAsync(_serviceName)
                                                    .ConfigureAwait(false).GetAwaiter().GetResult();

                _metrics.SamplerRetrieved.Inc(1);

                if (response.OperationSampling != null)
                {
                    UpdatePerOperationSampler(response.OperationSampling);
                }
                else
                {
                    UpdateRateLimitingOrProbabilisticSampler(response);
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Updating sampler failed");
                _metrics.SamplerQueryFailure.Inc(1);
            }
        }
        public void UpdateSampler_ShouldHandleOperationSampling()
        {
            var ssResponse = new SamplingStrategyResponse
            {
                OperationSampling = new PerOperationSamplingStrategies
                {
                    DefaultSamplingProbability       = 0.5,
                    DefaultLowerBoundTracesPerSecond = 10,
                    PerOperationStrategies           = new List <OperationSamplingStrategy>()
                }
            };

            _mockSamplingManager.GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName)).Returns(ssResponse);

            _testingSampler.UpdateSampler();

            _mockSamplingManager.Received(1).GetSamplingStrategy(Arg.Is <string>(sn => sn == _serivceName));
            _mockMetrics.Received(1).SamplerRetrieved.Inc(Arg.Any <long>());
            _mockSamplerFactory.Received(1).NewPerOperationSampler(
                Arg.Is <int>(mo => mo == 2000),
                Arg.Is <double>(dsp => dsp == ssResponse.OperationSampling.DefaultSamplingProbability),
                Arg.Is <double>(dlbtps => dlbtps == ssResponse.OperationSampling.DefaultLowerBoundTracesPerSecond),
                Arg.Is <ILoggerFactory>(lf => lf == _mockLoggerFactory));
        }