def test_adaptive_sampler_default_values(): adaptive_sampler = AdaptiveSampler({}, 2) assert '%s' % adaptive_sampler == \ 'AdaptiveSampler(0.001000, 0.001667, 2)', 'sampler should use default values' sampled, tags = adaptive_sampler.is_sampled(0, 'op') assert sampled assert tags == \ get_tags('probabilistic', 0.001), 'should use default probability' assert '%s' % adaptive_sampler.samplers['op'] == \ 'GuaranteedThroughputProbabilisticSampler(op, 0.001000, 0.001667)' adaptive_sampler.update(strategies={ 'defaultLowerBoundTracesPerSecond': 4, 'perOperationStrategies': [ { 'operation': 'new_op', 'probabilisticSampling': { 'samplingRate': 0.002 } } ] }) assert '%s' % adaptive_sampler == 'AdaptiveSampler(0.001000, 4.000000, 2)' sampled, tags = adaptive_sampler.is_sampled(0, 'new_op') assert sampled assert tags == get_tags('probabilistic', 0.002) assert '%s' % adaptive_sampler.samplers['new_op'] == \ 'GuaranteedThroughputProbabilisticSampler(new_op, 0.002000, 4.000000)' sampled, tags = adaptive_sampler.is_sampled(0, 'op') assert sampled assert tags == get_tags('probabilistic', 0.001) # TODO ruh roh, the lowerbound isn't changed # if the operation isn't included in perOperationStrategies assert '%s' % adaptive_sampler.samplers['op'] == \ 'GuaranteedThroughputProbabilisticSampler(op, 0.001000, 0.001667)'
def test_adaptive_sampler(): strategies = { "defaultSamplingProbability":0.51, "defaultLowerBoundTracesPerSecond":3, "perOperationStrategies": [ { "operation":"op", "probabilisticSampling":{ "samplingRate":0.5 } } ] } sampler = AdaptiveSampler(strategies, 2) sampled, tags = sampler.is_sampled(MAX_INT-10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.5) # This operation is seen for the first time by the sampler sampled, tags = sampler.is_sampled(MAX_INT-10, "new_op") assert sampled assert tags == get_tags('probabilistic', 0.51) sampled, tags = sampler.is_sampled(MAX_INT+(MAX_INT/4), "new_op") assert sampled assert tags == get_tags('lowerbound', 0.51) # This operation is seen for the first time by the sampler but surpasses # max_operations of 2. The default probabilistic sampler will be used sampled, tags = sampler.is_sampled(MAX_INT-10, "new_op_2") assert sampled assert tags == get_tags('probabilistic', 0.51) sampled, _ = sampler.is_sampled(MAX_INT+(MAX_INT/4), "new_op_2") assert not sampled assert '%s' % sampler == 'AdaptiveSampler(0.51, 3, 2)' # Update the strategies strategies = { "defaultSamplingProbability":0.52, "defaultLowerBoundTracesPerSecond":4, "perOperationStrategies": [ { "operation":"op", "probabilisticSampling":{ "samplingRate":0.52 } }, { "operation":"new_op_3", "probabilisticSampling":{ "samplingRate":0.53 } } ] } sampler.update(strategies) # The probability for op has been updated sampled, tags = sampler.is_sampled(MAX_INT-10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.52) # A new operation has been added sampled, tags = sampler.is_sampled(MAX_INT-10, 'new_op_3') assert sampled assert tags == get_tags('probabilistic', 0.53) assert '%s' % sampler == 'AdaptiveSampler(0.52, 4, 2)' sampler.close()
def test_adaptive_sampler(): strategies = { 'defaultSamplingProbability': 0.51, 'defaultLowerBoundTracesPerSecond': 3, 'perOperationStrategies': [{ 'operation': 'op', 'probabilisticSampling': { 'samplingRate': 0.5 } }] } sampler = AdaptiveSampler(strategies, 2) sampled, tags = sampler.is_sampled(MAX_INT - 10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.5) # This operation is seen for the first time by the sampler sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op') assert sampled assert tags == get_tags('probabilistic', 0.51) ts = time.time() with mock.patch('jaeger_client.rate_limiter.RateLimiter.timestamp') \ as mock_time: # Move time forward by a second to guarantee the rate limiter has enough credits mock_time.side_effect = lambda: ts + 1 sampled, tags = sampler.is_sampled(int(MAX_INT + (MAX_INT / 4)), 'new_op') assert sampled assert tags == get_tags('lowerbound', 0.51) # This operation is seen for the first time by the sampler but surpasses # max_operations of 2. The default probabilistic sampler will be used sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op_2') assert sampled assert tags == get_tags('probabilistic', 0.51) sampled, _ = sampler.is_sampled(int(MAX_INT + (MAX_INT / 4)), 'new_op_2') assert not sampled assert '%s' % sampler == 'AdaptiveSampler(0.510000, 3.000000, 2)' # Update the strategies strategies = { 'defaultSamplingProbability': 0.52, 'defaultLowerBoundTracesPerSecond': 4, 'perOperationStrategies': [{ 'operation': 'op', 'probabilisticSampling': { 'samplingRate': 0.52 } }, { 'operation': 'new_op_3', 'probabilisticSampling': { 'samplingRate': 0.53 } }] } sampler.update(strategies) # The probability for op has been updated sampled, tags = sampler.is_sampled(MAX_INT - 10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.52) # A new operation has been added sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op_3') assert sampled assert tags == get_tags('probabilistic', 0.53) assert '%s' % sampler == 'AdaptiveSampler(0.520000, 4.000000, 2)' sampler.close()
def test_adaptive_sampler(): strategies = { 'defaultSamplingProbability': 0.51, 'defaultLowerBoundTracesPerSecond': 3, 'perOperationStrategies': [ { 'operation': 'op', 'probabilisticSampling': { 'samplingRate': 0.5 } } ] } sampler = AdaptiveSampler(strategies, 2) sampled, tags = sampler.is_sampled(MAX_INT - 10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.5) # This operation is seen for the first time by the sampler sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op') assert sampled assert tags == get_tags('probabilistic', 0.51) ts = time.time() with mock.patch('jaeger_client.rate_limiter.RateLimiter.timestamp') \ as mock_time: # Move time forward by a second to guarantee the rate limiter has enough credits mock_time.side_effect = lambda: ts + 1 sampled, tags = sampler.is_sampled(int(MAX_INT + (MAX_INT / 4)), 'new_op') assert sampled assert tags == get_tags('lowerbound', 0.51) # This operation is seen for the first time by the sampler but surpasses # max_operations of 2. The default probabilistic sampler will be used sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op_2') assert sampled assert tags == get_tags('probabilistic', 0.51) sampled, _ = sampler.is_sampled(int(MAX_INT + (MAX_INT / 4)), 'new_op_2') assert not sampled assert '%s' % sampler == 'AdaptiveSampler(0.510000, 3.000000, 2)' # Update the strategies strategies = { 'defaultSamplingProbability': 0.52, 'defaultLowerBoundTracesPerSecond': 4, 'perOperationStrategies': [ { 'operation': 'op', 'probabilisticSampling': { 'samplingRate': 0.52 } }, { 'operation': 'new_op_3', 'probabilisticSampling': { 'samplingRate': 0.53 } } ] } sampler.update(strategies) # The probability for op has been updated sampled, tags = sampler.is_sampled(MAX_INT - 10, 'op') assert sampled assert tags == get_tags('probabilistic', 0.52) # A new operation has been added sampled, tags = sampler.is_sampled(MAX_INT - 10, 'new_op_3') assert sampled assert tags == get_tags('probabilistic', 0.53) assert '%s' % sampler == 'AdaptiveSampler(0.520000, 4.000000, 2)' sampler.close()
def test_parse_sampling_strategy(): sampler = RemoteControlledSampler( channel=mock.MagicMock(), service_name='x', max_operations=10 ) # noinspection PyProtectedMember s, strategies = sampler._parse_sampling_strategy(None, '{"strategyType":0,"probabilisticSampling":{"samplingRate":0.001}}') assert '%s' % s == 'ProbabilisticSampler(0.001)' assert not strategies with pytest.raises(ValueError): # noinspection PyProtectedMember sampler._parse_sampling_strategy(None,'{"strategyType":0,"probabilisticSampling":{"samplingRate":2}}') # noinspection PyProtectedMember s, strategies = sampler._parse_sampling_strategy(None, '{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":10}}') assert '%s' % s == 'RateLimitingSampler(10)' assert not strategies with pytest.raises(ValueError): # noinspection PyProtectedMember sampler._parse_sampling_strategy(None, '{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":-10}}') with pytest.raises(ValueError): # noinspection PyProtectedMember sampler._parse_sampling_strategy(None, '{"strategyType":2}') response = """ { "strategyType":1, "operationSampling": { "defaultSamplingProbability":0.001, "defaultLowerBoundTracesPerSecond":2, "perOperationStrategies": [ { "operation":"op", "probabilisticSampling":{ "samplingRate":0.002 } } ] } } """ # noinspection PyProtectedMember s, strategies = sampler._parse_sampling_strategy(None, response) assert '%s' % s == 'AdaptiveSampler(0.001, 2, 10)' assert strategies existing_strategies = { "defaultSamplingProbability":0.51, "defaultLowerBoundTracesPerSecond":3, "perOperationStrategies": [ { "operation":"op", "probabilisticSampling":{ "samplingRate":0.5 } } ] } existing_sampler = AdaptiveSampler(existing_strategies, 2) assert '%s' % existing_sampler == 'AdaptiveSampler(0.51, 3, 2)' # noinspection PyProtectedMember s, strategies = sampler._parse_sampling_strategy(existing_sampler, response) assert '%s' % existing_sampler == 'AdaptiveSampler(0.51, 3, 2)' assert strategies sampler.close()