| 1 |
import os |
| 2 |
import json |
| 3 |
from random import Random |
| 4 |
|
| 5 |
from pkg_resources import resource_filename |
| 6 |
from .sampling_rule import SamplingRule |
| 7 |
from ..exceptions.exceptions import InvalidSamplingManifestError |
| 8 |
|
| 9 |
|
| 10 |
with open(resource_filename(__name__, 'default_sampling_rule.json')) as f: |
| 11 |
default_sampling_rule = json.load(f) |
| 12 |
|
| 13 |
|
| 14 |
class DefaultSampler(object): |
| 15 |
""" |
| 16 |
The default sampler that holds either custom sampling rules |
| 17 |
or default sampling rules. Every time before the X-Ray recorder |
| 18 |
generates a segment, it calculates if this segment is sampled |
| 19 |
or not. |
| 20 |
""" |
| 21 |
def __init__(self, rules=default_sampling_rule): |
| 22 |
""" |
| 23 |
:param dict rules: a dict that defines custom sampling rules. |
| 24 |
An example configuration: |
| 25 |
{ |
| 26 |
"version": 1, |
| 27 |
"rules": [ |
| 28 |
{ |
| 29 |
"description": "Player moves.", |
| 30 |
"service_name": "*", |
| 31 |
"http_method": "*", |
| 32 |
"url_path": "/api/move/*", |
| 33 |
"fixed_target": 0, |
| 34 |
"rate": 0.05 |
| 35 |
} |
| 36 |
], |
| 37 |
"default": { |
| 38 |
"fixed_target": 1, |
| 39 |
"rate": 0.1 |
| 40 |
} |
| 41 |
} |
| 42 |
This example defines one custom rule and a default rule. |
| 43 |
The custom rule applies a five-percent sampling rate with no minimum |
| 44 |
number of requests to trace for paths under /api/move/. The default |
| 45 |
rule traces the first request each second and 10 percent of additional requests. |
| 46 |
The SDK applies custom rules in the order in which they are defined. |
| 47 |
If a request matches multiple custom rules, the SDK applies only the first rule. |
| 48 |
""" |
| 49 |
version = rules.get('version', None) |
| 50 |
if version != 1: |
| 51 |
raise InvalidSamplingManifestError('Manifest version: %s is not supported.', version) |
| 52 |
|
| 53 |
if 'default' not in rules: |
| 54 |
raise InvalidSamplingManifestError('A default rule must be provided.') |
| 55 |
|
| 56 |
self._default_rule = SamplingRule(rule_dict=rules['default'], |
| 57 |
default=True) |
| 58 |
|
| 59 |
self._rules = [] |
| 60 |
if 'rules' in rules: |
| 61 |
for rule in rules['rules']: |
| 62 |
self._rules.append(SamplingRule(rule)) |
| 63 |
|
| 64 |
self._random = Random() |
| 65 |
|
| 66 |
def should_trace(self, service_name=None, method=None, path=None): |
| 67 |
""" |
| 68 |
Return True if the sampler decide to sample based on input |
| 69 |
information and sampling rules. It will first check if any |
| 70 |
custom rule should be applied, if not it falls back to the |
| 71 |
default sampling rule. |
| 72 |
|
| 73 |
All optional arugments are extracted from incoming requests by |
| 74 |
X-Ray middleware to perform path based sampling. |
| 75 |
""" |
| 76 |
if service_name and method and path: |
| 77 |
for rule in self._rules: |
| 78 |
if rule.applies(service_name, method, path): |
| 79 |
return self._should_trace(rule) |
| 80 |
|
| 81 |
return self._should_trace(self._default_rule) |
| 82 |
|
| 83 |
def _should_trace(self, sampling_rule): |
| 84 |
|
| 85 |
if sampling_rule.reservoir.take(): |
| 86 |
return True |
| 87 |
else: |
| 88 |
return self._random.random() < sampling_rule.rate |