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
|
import copy
import pytest
from aws_xray_sdk.core.sampling.sampling_rule import SamplingRule
from aws_xray_sdk.core.sampling.default_sampler import DefaultSampler
from aws_xray_sdk.core.exceptions.exceptions import InvalidSamplingManifestError
RULE = {"description": "Player moves.",
"service_name": "*",
"http_method": "*",
"url_path": "/api/move/*",
"fixed_target": 0,
"rate": 0.05
}
RULE_MANIFEST = {
"version": 1,
"rules": [{
"description": "Player moves.",
"service_name": "*",
"http_method": "*",
"url_path": "/api/move/*",
"fixed_target": 0,
"rate": 0
}],
"default": {
"fixed_target": 1,
"rate": 1
}
}
def test_should_trace():
sampler = DefaultSampler(RULE_MANIFEST)
assert sampler.should_trace(None, 'GET', '/view')
assert not sampler.should_trace('name', 'method', '/api/move/left')
def test_missing_version_num():
rule = copy.deepcopy(RULE_MANIFEST)
del rule['version']
with pytest.raises(InvalidSamplingManifestError):
DefaultSampler(rule)
def test_path_matching():
rule = SamplingRule(RULE)
assert rule.applies('name', 'GET', '/api/move/up')
assert rule.applies(None, 'POST', '/api/move/up')
assert rule.applies('name', None, '/api/move/up')
assert rule.applies('name', 'PUT', None)
assert not rule.applies(None, 'GET', '/root')
def test_negative_rate():
rule = copy.deepcopy(RULE)
rule['rate'] = -1
with pytest.raises(InvalidSamplingManifestError):
SamplingRule(rule)
def test_negative_fixed_target():
rule = copy.deepcopy(RULE)
rule['fixed_target'] = -1
with pytest.raises(InvalidSamplingManifestError):
SamplingRule(rule)
def test_invalid_default():
with pytest.raises(InvalidSamplingManifestError):
SamplingRule(RULE, default=True)
def test_incomplete_path_rule():
rule = copy.deepcopy(RULE)
del rule['url_path']
with pytest.raises(InvalidSamplingManifestError):
SamplingRule(rule)
|