forked from jaegertracing/jaeger-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sampler.py
More file actions
606 lines (528 loc) · 20 KB
/
Copy pathtest_sampler.py
File metadata and controls
606 lines (528 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from __future__ import division
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from builtins import range
import time
import math
import mock
import pytest
from jaeger_client.sampler import (
Sampler,
ConstSampler,
ProbabilisticSampler,
RateLimitingSampler,
RemoteControlledSampler,
GuaranteedThroughputProbabilisticSampler,
AdaptiveSampler,
DEFAULT_SAMPLING_PROBABILITY,
get_sampling_probability,
get_rate_limit,
)
MAX_INT = 1 << 63
def get_tags(type, param):
return {
'sampler.type': type,
'sampler.param': param,
}
def test_abstract_sampler_errors():
sampler = Sampler()
with pytest.raises(NotImplementedError):
sampler.is_sampled(trace_id=123)
with pytest.raises(NotImplementedError):
sampler.close()
def test_probabilistic_sampler_errors():
with pytest.raises(AssertionError):
ProbabilisticSampler(-0.1)
with pytest.raises(AssertionError):
ProbabilisticSampler(1.1)
def test_probabilistic_sampler():
sampler = ProbabilisticSampler(0.5)
assert MAX_INT == 0x8000000000000000
sampled, tags = sampler.is_sampled(MAX_INT-10)
assert sampled
assert tags == get_tags('probabilistic', 0.5)
sampled, _ = sampler.is_sampled(MAX_INT+10)
assert not sampled
sampler.close()
assert '%s' % sampler == 'ProbabilisticSampler(0.5)'
def test_const_sampler():
sampler = ConstSampler(True)
sampled, _ = sampler.is_sampled(1)
assert sampled
sampled, _ = sampler.is_sampled(MAX_INT)
assert sampled
sampler = ConstSampler(False)
sampled, tags = sampler.is_sampled(1)
assert not sampled
sampled, tags = sampler.is_sampled(MAX_INT)
assert not sampled
assert tags == get_tags('const', False)
assert '%s' % sampler == 'ConstSampler(False)'
def test_rate_limiting_sampler():
sampler = RateLimitingSampler(2)
# stop time by overwriting timestamp() function to always return
# the same time
ts = time.time()
sampler.rate_limiter.last_tick = ts
with mock.patch('jaeger_client.rate_limiter.RateLimiter.timestamp') \
as mock_time:
mock_time.side_effect = lambda: ts # always return same time
assert sampler.rate_limiter.timestamp() == ts
sampled, _ = sampler.is_sampled(0)
assert sampled, 'initial balance allows first item'
sampled, _ = sampler.is_sampled(0)
assert sampled, 'initial balance allows second item'
sampled, _ = sampler.is_sampled(0)
assert not sampled, 'initial balance exhausted'
# move time 250ms forward, not enough credits to pay for one sample
mock_time.side_effect = lambda: ts + 0.25
sampled, _ = sampler.is_sampled(0)
assert not sampled, 'not enough time passed for full item'
# move time 500ms forward, now enough credits to pay for one sample
mock_time.side_effect = lambda: ts + 0.5
sampled, _ = sampler.is_sampled(0)
assert sampled, 'enough time for new item'
sampled, _ = sampler.is_sampled(0)
assert not sampled, 'no more balance'
# move time 5s forward, enough to accumulate credits for 10 samples,
# but it should still be capped at 2
sampler.last_tick = ts # reset the timer
mock_time.side_effect = lambda: ts + 5
sampled, _ = sampler.is_sampled(0)
assert sampled, 'enough time for new item'
sampled, _ = sampler.is_sampled(0)
assert sampled, 'enough time for second new item'
for i in range(0, 8):
sampled, tags = sampler.is_sampled(0)
assert not sampled, 'but no further, since time is stopped'
assert tags == get_tags('ratelimiting', 2)
sampler.close()
assert '%s' % sampler == 'RateLimitingSampler(2)'
# Test with rate limit of greater than 1 second
sampler = RateLimitingSampler(0.1)
ts = time.time()
sampler.rate_limiter.last_tick = ts
with mock.patch('jaeger_client.rate_limiter.RateLimiter.timestamp') \
as mock_time:
mock_time.side_effect = lambda: ts # always return same time
assert sampler.rate_limiter.timestamp() == ts
sampled, _ = sampler.is_sampled(0)
assert sampled, 'initial balance allows first item'
sampled, _ = sampler.is_sampled(0)
assert not sampled, 'initial balance exhausted'
# move time 11s forward, enough credits to pay for one sample
mock_time.side_effect = lambda: ts + 11
sampled, _ = sampler.is_sampled(0)
assert sampled
sampler.close()
assert '%s' % sampler == 'RateLimitingSampler(0.1)'
def test_guaranteed_throughput_probabilistic_sampler():
sampler = GuaranteedThroughputProbabilisticSampler('op', 2, 0.5)
sampled, tags = sampler.is_sampled(MAX_INT-10)
assert sampled
assert tags == get_tags('probabilistic', 0.5)
sampled, tags = sampler.is_sampled(MAX_INT+10)
assert sampled
assert tags == get_tags('lowerbound', 0.5)
sampled, _ = sampler.is_sampled(MAX_INT+10)
assert not sampled
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.500000, 2.000000)'
sampler.update(3, 0.51)
sampled, tags = sampler.is_sampled(MAX_INT-10)
assert sampled
assert tags == get_tags('probabilistic', 0.51)
sampled, tags = sampler.is_sampled(MAX_INT+(MAX_INT/4))
assert sampled
assert tags == get_tags('lowerbound', 0.51)
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.510000, 3.000000)'
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)
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.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_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_sampler_equality():
const1 = ConstSampler(True)
const2 = ConstSampler(True)
const3 = ConstSampler(False)
assert const1 == const2
assert const1 != const3
prob1 = ProbabilisticSampler(rate=0.01)
prob2 = ProbabilisticSampler(rate=0.01)
prob3 = ProbabilisticSampler(rate=0.02)
assert prob1 == prob2
assert prob1 != prob3
assert const1 != prob1
rate1 = RateLimitingSampler(max_traces_per_second=0.01)
rate2 = RateLimitingSampler(max_traces_per_second=0.01)
rate3 = RateLimitingSampler(max_traces_per_second=0.02)
assert rate1 == rate2
assert rate1 != rate3
assert rate1 != const1
assert rate1 != prob1
def test_remotely_controlled_sampler():
sampler = RemoteControlledSampler(
channel=mock.MagicMock(),
service_name='x'
)
sampled, tags = sampler.is_sampled(1)
assert sampled
assert tags == get_tags('probabilistic', DEFAULT_SAMPLING_PROBABILITY)
init_sampler = mock.MagicMock()
init_sampler.is_sampled = mock.MagicMock()
channel = mock.MagicMock()
channel.io_loop = None
sampler = RemoteControlledSampler(
channel=channel,
service_name='x',
init_sampler=init_sampler,
logger=mock.MagicMock(),
)
assert init_sampler.is_sampled.call_count == 1
sampler.is_sampled(1)
assert init_sampler.is_sampled.call_count == 2
sampler.io_loop = mock.MagicMock()
# noinspection PyProtectedMember
sampler._init_polling()
assert sampler.io_loop.call_later.call_count == 1
sampler._create_periodic_callback = mock.MagicMock()
# noinspection PyProtectedMember
sampler._delayed_polling()
sampler.close()
# noinspection PyProtectedMember
def test_sampling_request_callback():
channel = mock.MagicMock()
channel.io_loop = mock.MagicMock()
error_reporter = mock.MagicMock()
error_reporter.error = mock.MagicMock()
sampler = RemoteControlledSampler(
channel=channel,
service_name='x',
error_reporter=error_reporter,
max_operations=10,
)
return_value = mock.MagicMock()
return_value.exception = lambda *args: False
probabilistic_strategy = """
{
"strategyType":0,
"probabilisticSampling":
{
"samplingRate":0.002
}
}
"""
return_value.result = lambda *args: \
type('obj', (object,), {'body': probabilistic_strategy})()
sampler._sampling_request_callback(return_value)
assert '%s' % sampler.sampler == 'ProbabilisticSampler(0.002)', 'sampler should have changed to probabilistic'
prev_sampler = sampler.sampler
sampler._sampling_request_callback(return_value)
assert prev_sampler is sampler.sampler, "strategy hasn't changed so sampler should not change"
adaptive_sampling_strategy = """
{
"strategyType":0,
"operationSampling":
{
"defaultSamplingProbability":0.001,
"defaultLowerBoundTracesPerSecond":2,
"perOperationStrategies":
[
{
"operation":"op",
"probabilisticSampling":{
"samplingRate":0.002
}
}
]
}
}
"""
return_value.result = lambda *args: \
type('obj', (object,), {'body': adaptive_sampling_strategy})()
sampler._sampling_request_callback(return_value)
assert '%s' % sampler.sampler == 'AdaptiveSampler(0.001000, 2.000000, 10)', 'sampler should have changed to adaptive'
prev_sampler = sampler.sampler
sampler._sampling_request_callback(return_value)
assert prev_sampler is sampler.sampler, "strategy hasn't changed so sampler should not change"
return_value.exception = lambda *args: True
sampler._sampling_request_callback(return_value)
assert error_reporter.error.call_count == 1
assert prev_sampler is sampler.sampler, 'error fetching strategy should not update the sampler'
return_value.exception = lambda *args: False
return_value.result = lambda *args: type('obj', (object,), {'body': 'bad_json'})()
sampler._sampling_request_callback(return_value)
assert error_reporter.error.call_count == 2
assert prev_sampler is sampler.sampler, 'error updating sampler should not update the sampler'
return_value.result = lambda *args: \
type('obj', (object,), {'body': probabilistic_strategy})()
sampler._sampling_request_callback(return_value)
assert '%s' % sampler.sampler == 'ProbabilisticSampler(0.002)', 'updating sampler from adaptive to probabilistic should work'
sampler.close()
probabilistic_sampler = ProbabilisticSampler(0.002)
other_probabilistic_sampler = ProbabilisticSampler(0.003)
rate_limiting_sampler = RateLimitingSampler(10)
other_rate_limiting_sampler = RateLimitingSampler(20)
@pytest.mark.parametrize("response,init_sampler,expected_sampler,err_count,err_msg,reference_equivalence", [
(
{"strategyType":0,"probabilisticSampling":{"samplingRate":0.003}},
probabilistic_sampler,
other_probabilistic_sampler,
0,
'sampler should update to new probabilistic sampler',
False,
),
(
{"strategyType":0,"probabilisticSampling":{"samplingRate":400}},
probabilistic_sampler,
probabilistic_sampler,
1,
'sampler should remain the same if strategy is invalid',
True,
),
(
{"strategyType":0,"probabilisticSampling":{"samplingRate":0.002}},
probabilistic_sampler,
probabilistic_sampler,
0,
'sampler should remain the same with the same strategy',
True,
),
(
{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":10}},
probabilistic_sampler,
rate_limiting_sampler,
0,
'sampler should update to new rate limiting sampler',
False,
),
(
{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":10}},
rate_limiting_sampler,
rate_limiting_sampler,
0,
'sampler should remain the same with the same strategy',
True,
),
(
{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":-10}},
rate_limiting_sampler,
rate_limiting_sampler,
1,
'sampler should remain the same if strategy is invalid',
True,
),
(
{"strategyType":1,"rateLimitingSampling":{"maxTracesPerSecond":20}},
rate_limiting_sampler,
other_rate_limiting_sampler,
0,
'sampler should update to new rate limiting sampler',
False,
),
(
{},
rate_limiting_sampler,
rate_limiting_sampler,
1,
'sampler should remain the same if strategy is empty',
True,
),
(
{"strategyType":2},
rate_limiting_sampler,
rate_limiting_sampler,
1,
'sampler should remain the same if strategy is invalid',
True,
),
])
def test_update_sampler(response, init_sampler, expected_sampler, err_count, err_msg, reference_equivalence):
error_reporter = mock.MagicMock()
error_reporter.error = mock.MagicMock()
remote_sampler = RemoteControlledSampler(
channel=mock.MagicMock(),
service_name='x',
error_reporter=error_reporter,
max_operations=10,
init_sampler=init_sampler,
)
# noinspection PyProtectedMember
remote_sampler._update_sampler(response)
assert error_reporter.error.call_count == err_count
if reference_equivalence:
assert remote_sampler.sampler is expected_sampler, err_msg
else:
assert remote_sampler.sampler == expected_sampler, err_msg
remote_sampler.close()
# noinspection PyProtectedMember
def test_update_sampler_adaptive_sampler():
error_reporter = mock.MagicMock()
error_reporter.error = mock.MagicMock()
remote_sampler = RemoteControlledSampler(
channel=mock.MagicMock(),
service_name='x',
error_reporter=error_reporter,
max_operations=10,
)
response = {
"strategyType":1,
"operationSampling":
{
"defaultSamplingProbability":0.001,
"defaultLowerBoundTracesPerSecond":2,
"perOperationStrategies":
[
{
"operation":"op",
"probabilisticSampling":{
"samplingRate":0.002
}
}
]
}
}
remote_sampler._update_sampler(response)
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.001000, 2.000000, 10)'
new_response = {
"strategyType":1,
"operationSampling":
{
"defaultSamplingProbability":0.51,
"defaultLowerBoundTracesPerSecond":3,
"perOperationStrategies":
[
{
"operation":"op",
"probabilisticSampling":{
"samplingRate":0.002
}
}
]
}
}
remote_sampler._update_sampler(new_response)
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.510000, 3.000000, 10)'
remote_sampler._update_sampler({"strategyType":0,"probabilisticSampling":{"samplingRate":0.004}})
assert '%s' % remote_sampler.sampler == 'ProbabilisticSampler(0.004)', \
'should not fail going from adaptive sampler to probabilistic sampler'
remote_sampler._update_sampler({"strategyType":1,"operationSampling":{"defaultSamplingProbability":0.4}})
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.400000, 0.001667, 10)'
remote_sampler.close()
@pytest.mark.parametrize("strategy,expected", [
({"probabilisticSampling":{"samplingRate":0.003}}, 0.003),
({}, 0.001),
(None, 0.001),
({"probabilisticSampling":{}}, 0.001),
({"probabilisticSampling":None}, 0.001),
])
def test_get_sampling_probability(strategy, expected):
assert expected == get_sampling_probability(strategy)
@pytest.mark.parametrize("strategy,expected", [
({"rateLimitingSampling":{"maxTracesPerSecond":1}}, 1),
({}, 0.0016666),
(None, 0.0016666),
({"rateLimitingSampling":{}}, 0.0016666),
({"rateLimitingSampling":None}, 0.0016666),
])
def test_get_rate_limit(strategy, expected):
assert math.fabs(expected - get_rate_limit(strategy)) < 0.0001