| 1 |
from .reservoir import Reservoir |
| 2 |
from ..utils.search_pattern import wildcard_match |
| 3 |
from ..exceptions.exceptions import InvalidSamplingManifestError |
| 4 |
|
| 5 |
|
| 6 |
class SamplingRule(object): |
| 7 |
""" |
| 8 |
One SamolingRule represents one rule defined from rule json file |
| 9 |
or from a dictionary. It can be either a custom rule or default rule. |
| 10 |
""" |
| 11 |
FIXED_TARGET = 'fixed_target' |
| 12 |
RATE = 'rate' |
| 13 |
|
| 14 |
SERVICE_NAME = 'service_name' |
| 15 |
METHOD = 'http_method' |
| 16 |
PATH = 'url_path' |
| 17 |
|
| 18 |
def __init__(self, rule_dict, default=False): |
| 19 |
""" |
| 20 |
:param dict rule_dict: The dictionary that defines a single rule. |
| 21 |
:param bool default: Indicates if this is the default rule. A default |
| 22 |
rule cannot have `service_name`, `http_method` or `url_path`. |
| 23 |
""" |
| 24 |
self._fixed_target = rule_dict.get(self.FIXED_TARGET, None) |
| 25 |
self._rate = rule_dict.get(self.RATE, None) |
| 26 |
|
| 27 |
self._service_name = rule_dict.get(self.SERVICE_NAME, None) |
| 28 |
self._method = rule_dict.get(self.METHOD, None) |
| 29 |
self._path = rule_dict.get(self.PATH, None) |
| 30 |
|
| 31 |
self._default = default |
| 32 |
|
| 33 |
self._validate() |
| 34 |
|
| 35 |
self._reservoir = Reservoir(self.fixed_target) |
| 36 |
|
| 37 |
def applies(self, service_name, method, path): |
| 38 |
""" |
| 39 |
Determines whether or not this sampling rule applies to |
| 40 |
the incoming request based on some of the request's parameters. |
| 41 |
Any None parameters provided will be considered an implicit match. |
| 42 |
""" |
| 43 |
return (not service_name or wildcard_match(self.service_name, service_name)) \ |
| 44 |
and (not method or wildcard_match(self.service_name, method)) \ |
| 45 |
and (not path or wildcard_match(self.path, path)) |
| 46 |
|
| 47 |
@property |
| 48 |
def fixed_target(self): |
| 49 |
""" |
| 50 |
Defines fixed number of sampled segments per second. |
| 51 |
This doesn't count for sampling rate. |
| 52 |
""" |
| 53 |
return self._fixed_target |
| 54 |
|
| 55 |
@property |
| 56 |
def rate(self): |
| 57 |
""" |
| 58 |
A float number less than 1.0 defines the sampling rate. |
| 59 |
""" |
| 60 |
return self._rate |
| 61 |
|
| 62 |
@property |
| 63 |
def service_name(self): |
| 64 |
""" |
| 65 |
The host name of the reqest to sample. |
| 66 |
""" |
| 67 |
return self._service_name |
| 68 |
|
| 69 |
@property |
| 70 |
def method(self): |
| 71 |
""" |
| 72 |
HTTP method of the request to sample. |
| 73 |
""" |
| 74 |
return self._method |
| 75 |
|
| 76 |
@property |
| 77 |
def path(self): |
| 78 |
""" |
| 79 |
The url path of the request to sample. |
| 80 |
""" |
| 81 |
return self._path |
| 82 |
|
| 83 |
@property |
| 84 |
def reservoir(self): |
| 85 |
""" |
| 86 |
Keeps track of used sampled targets within the second. |
| 87 |
""" |
| 88 |
return self._reservoir |
| 89 |
|
| 90 |
def _validate(self): |
| 91 |
if self.fixed_target < 0 or self.rate < 0: |
| 92 |
raise InvalidSamplingManifestError('All rules must have non-negative values for ' |
| 93 |
'fixed_target and rate') |
| 94 |
|
| 95 |
if self._default: |
| 96 |
if self.service_name or self.method or self.path: |
| 97 |
raise InvalidSamplingManifestError('The default rule must not specify values for ' |
| 98 |
'url_path, service_name, or http_method') |
| 99 |
else: |
| 100 |
if not self.service_name or not self.method or not self.path: |
| 101 |
raise InvalidSamplingManifestError('All non-default rules must have values for ' |
| 102 |
'url_path, service_name, and http_method') |