public PerOperationSampler(int maxOperations, OperationSamplingParameters strategies, ILoggerFactory loggerFactory)
     : this(maxOperations,
            new Dictionary <string, GuaranteedThroughputSampler>(),
            new ProbabilisticSampler(strategies.DefaultSamplingProbability),
            strategies.DefaultLowerBoundTracesPerSecond,
            loggerFactory)
 {
     Update(strategies);
 }
示例#2
0
        public void TestUpdate()
        {
            GuaranteedThroughputSampler guaranteedThroughputSampler = Substitute.ForPartsOf <GuaranteedThroughputSampler>(0, 0);

            _operationToSamplers.Add(operation, guaranteedThroughputSampler);

            var perOperationSamplingParameters = new PerOperationSamplingParameters(operation, new ProbabilisticSamplingStrategy(SamplingRate));
            var parametersList = new List <PerOperationSamplingParameters>();

            parametersList.Add(perOperationSamplingParameters);

            var parameters = new OperationSamplingParameters(DefaultSamplingProbability, DefaultLowerBoundTracesPerSecond, parametersList);

            Assert.True(_undertest.Update(parameters));
            guaranteedThroughputSampler.Received(1).Update(SamplingRate, DefaultLowerBoundTracesPerSecond);
            //verifyNoMoreInteractions(guaranteedThroughputSampler);
        }
示例#3
0
 internal void UpdatePerOperationSampler(OperationSamplingParameters samplingParameters)
 {
     lock (_lock)
     {
         if (Sampler is PerOperationSampler sampler)
         {
             if (sampler.Update(samplingParameters))
             {
                 _metrics.SamplerUpdated.Inc(1);
             }
         }
         else
         {
             Sampler.Close();
             Sampler = new PerOperationSampler(_maxOperations, samplingParameters, _loggerFactory);
         }
     }
 }
        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]);
        }
示例#5
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);
        }
        /// <summary>
        /// Updates the <see cref="GuaranteedThroughputSampler"/> for each operation.
        /// </summary>
        /// <param name="strategies">The parameters for operation sampling.</param>
        /// <returns><c>true</c>, if any samplers were updated.</returns>
        public bool Update(OperationSamplingParameters strategies)
        {
            lock (_lock)
            {
                var isUpdated = false;

                LowerBound = strategies.DefaultLowerBoundTracesPerSecond;
                ProbabilisticSampler defaultSampler = new ProbabilisticSampler(strategies.DefaultSamplingProbability);

                if (!defaultSampler.Equals(DefaultSampler))
                {
                    DefaultSampler.Close();
                    DefaultSampler = defaultSampler;
                    isUpdated      = true;
                }

                foreach (var strategy in strategies.PerOperationStrategies)
                {
                    var operation    = strategy.Operation;
                    var samplingRate = strategy.ProbabilisticSampling.SamplingRate;
                    if (OperationNameToSampler.TryGetValue(operation, out var sampler))
                    {
                        isUpdated = sampler.Update(samplingRate, LowerBound) || isUpdated;
                    }
                    else
                    {
                        if (OperationNameToSampler.Count < MaxOperations)
                        {
                            sampler = new GuaranteedThroughputSampler(samplingRate, LowerBound);
                            OperationNameToSampler[operation] = sampler;
                            isUpdated = true;
                        }
                        else
                        {
                            _logger.LogInformation("Exceeded the maximum number of operations {maxOperations} for per operations sampling", MaxOperations);
                        }
                    }
                }

                return(isUpdated);
            }
        }
示例#7
0
        public async Task TestUpdatePerOperationSamplerUpdatesExistingPerOperationSampler()
        {
            OperationSamplingParameters parameters          = new OperationSamplingParameters(1, 1, new List <PerOperationSamplingParameters>());
            PerOperationSampler         perOperationSampler = Substitute.ForPartsOf <PerOperationSampler>(10, parameters, NullLoggerFactory.Instance);

            _samplingManager.GetSamplingStrategyAsync(SERVICE_NAME)
            .Returns(new SamplingStrategyResponse(null, null, parameters));

            _undertest = new RemoteControlledSampler.Builder(SERVICE_NAME)
                         .WithSamplingManager(_samplingManager)
                         .WithInitialSampler(perOperationSampler)
                         .WithMetrics(_metrics)
                         .Build();

            _undertest.UpdateSampler();
            await Task.Delay(20);

            //updateSampler is hit once automatically because of the pollTimer
            perOperationSampler.Received(2).Update(parameters);
        }
示例#8
0
        public void TestNoopUpdate()
        {
            ProbabilisticSampler defaultProbabilisticSampler = new ProbabilisticSampler(DefaultSamplingProbability);
            double operationSamplingRate = 0.23;

            _operationToSamplers.Add(operation, new GuaranteedThroughputSampler(operationSamplingRate,
                                                                                DefaultLowerBoundTracesPerSecond));
            _undertest = new PerOperationSampler(MaxOperations, _operationToSamplers, defaultProbabilisticSampler,
                                                 DefaultLowerBoundTracesPerSecond, _loggerFactory);

            var parametersList = new List <PerOperationSamplingParameters>();

            parametersList.Add(new PerOperationSamplingParameters(operation, new ProbabilisticSamplingStrategy(operationSamplingRate)));

            var parameters = new OperationSamplingParameters(DefaultSamplingProbability, DefaultLowerBoundTracesPerSecond, parametersList);

            Assert.False(_undertest.Update(parameters));
            Assert.Equal(_operationToSamplers, _undertest.OperationNameToSampler);
            Assert.Equal(DefaultLowerBoundTracesPerSecond, _undertest.LowerBound, DoublePrecision);
            Assert.Equal(DefaultSamplingProbability, _undertest.DefaultSampler.SamplingRate, DoublePrecision);
        }