Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit 605ae04

Browse files
authored
Add Throttler API (#330)
* Add Throttler API Signed-off-by: Yuri Shkuro <[email protected]> * fix Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 44dc207 commit 605ae04

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

jaeger_client/throttler.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@
2828
default_logger = logging.getLogger('jaeger_tracing')
2929

3030

31-
class RemoteThrottler(object):
31+
class Throttler(object):
32+
def set_client_id(self, client_id: int) -> None:
33+
"""
34+
Called by tracer to set client ID of throttler.
35+
"""
36+
pass
37+
38+
def is_allowed(self, operation: str) -> bool:
39+
raise NotImplementedError()
40+
41+
def close(self) -> None:
42+
pass
43+
44+
45+
class RemoteThrottler(Throttler):
3246
"""
3347
RemoteThrottler controls the flow of spans emitted from client to prevent
3448
flooding. RemoteThrottler requests credits from the throttling service
@@ -78,10 +92,7 @@ def is_allowed(self, operation):
7892
self.credits[operation] = value - MINIMUM_CREDITS
7993
return True
8094

81-
def _set_client_id(self, client_id):
82-
"""
83-
Method for tracer to set client ID of throttler.
84-
"""
95+
def set_client_id(self, client_id):
8596
with self.lock:
8697
if self.client_id is None:
8798
self.client_id = client_id

jaeger_client/tracer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .utils import local_ip
4040
from .sampler import Sampler
4141
from .reporter import BaseReporter
42+
from .throttler import Throttler
4243

4344
logger = logging.getLogger('jaeger_tracing')
4445

@@ -63,7 +64,7 @@ def __init__(
6364
tags: Optional[Dict[str, Any]] = None,
6465
max_tag_value_length: int = constants.MAX_TAG_VALUE_LENGTH,
6566
max_traceback_length: int = constants.MAX_TRACEBACK_LENGTH,
66-
throttler: Optional[Any] = None,
67+
throttler: Optional[Throttler] = None,
6768
scope_manager: Optional[ScopeManager] = None,
6869
) -> None:
6970
self.service_name = service_name
@@ -115,7 +116,7 @@ def __init__(
115116
self.throttler = throttler
116117
if self.throttler:
117118
client_id = random.randint(0, sys.maxsize)
118-
self.throttler._set_client_id(client_id)
119+
self.throttler.set_client_id(client_id)
119120
self.tags[constants.CLIENT_UUID_TAG_KEY] = client_id
120121

121122
self.reporter.set_process(

tests/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from jaeger_client.config import DEFAULT_THROTTLER_PORT
2525
from jaeger_client.metrics import MetricsFactory
2626
from jaeger_client.reporter import NullReporter
27-
from tests.test_utils import TestSampler
27+
from tests.test_utils import MockSampler
2828

2929

3030
class ConfigTests(unittest.TestCase):
@@ -85,7 +85,7 @@ def test_bad_sampler(self):
8585
c.sampler.is_sampled(0)
8686

8787
def test_object_sampler_sampler(self):
88-
sampler = TestSampler()
88+
sampler = MockSampler()
8989
c = Config({'sampler': sampler}, service_name='x')
9090
assert c.sampler is sampler
9191

tests/test_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ def test_get_local_ip_by_socket_does_not_blow_up():
8484
jaeger_client.utils.get_local_ip_by_socket()
8585

8686

87-
class TestSampler(Sampler):
88-
pass
87+
class MockSampler(Sampler):
88+
def __init__(self):
89+
pass

0 commit comments

Comments
 (0)