-
Notifications
You must be signed in to change notification settings - Fork 22
AWS X-Ray Remote Sampler Part 3 - rate limiter logic and get sampling targets #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1164fa0
rate limiter logic and get sampling targets
jj22ee 18f851d
lint fix
jj22ee 5bb6b53
add multithread test case, address comments
jj22ee d277c8b
fix unit tests for python 3.8 and 3.9
jj22ee 13c4913
Merge branch 'main' into remote-sampler-3
jj22ee c363040
use lock for applier __reservoir_expiry
jj22ee fe1a815
update AwsXRayRemoteSampler initialization
jj22ee 69e872f
replace applier rather than update applier when targets are fetched
jj22ee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/_clock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import datetime | ||
|
||
|
||
class _Clock: | ||
def __init__(self): | ||
self.__datetime = datetime.datetime | ||
|
||
def now(self) -> datetime.datetime: | ||
return self.__datetime.now() | ||
|
||
# pylint: disable=no-self-use | ||
def from_timestamp(self, timestamp: float) -> datetime: | ||
return datetime.datetime.fromtimestamp(timestamp) | ||
|
||
def time_delta(self, seconds: float) -> datetime.timedelta: | ||
return datetime.timedelta(seconds=seconds) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/_rate_limiter.py
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from decimal import Decimal | ||
from threading import Lock | ||
|
||
from amazon.opentelemetry.distro.sampler._clock import _Clock | ||
|
||
|
||
class _RateLimiter: | ||
def __init__(self, max_balance_in_seconds: int, quota: int, clock: _Clock): | ||
# max_balance_in_seconds is usually 1 | ||
# pylint: disable=invalid-name | ||
self.MAX_BALANCE_MILLIS = Decimal(max_balance_in_seconds * 1000.0) | ||
self._clock = clock | ||
|
||
self._quota = Decimal(quota) | ||
self.__wallet_floor_millis = Decimal(self._clock.now().timestamp() * 1000.0) | ||
# current "wallet_balance" would be ceiling - floor | ||
|
||
self.__lock = Lock() | ||
|
||
def try_spend(self, cost: float, borrow: bool) -> bool: | ||
quota_per_millis = self._quota / Decimal(1000.0) | ||
|
||
if borrow and quota_per_millis != 0: | ||
# When `Borrowing`, pretend that the quota is 1 per second | ||
quota_per_millis = Decimal(1.0) / Decimal(1000.0) | ||
|
||
with self.__lock: | ||
wallet_ceiling_millis = Decimal(self._clock.now().timestamp() * 1000.0) | ||
current_balance_millis = wallet_ceiling_millis - self.__wallet_floor_millis | ||
if current_balance_millis > self.MAX_BALANCE_MILLIS: | ||
current_balance_millis = self.MAX_BALANCE_MILLIS | ||
|
||
# Ex: 1. current_balance_millis=1000ms, quota_per_millis=0.004 (quota=4) -> actual_balance = 4 | ||
# 2. actual_balance=4, cost=3 -> actual_remaining_balance = 1 | ||
# 3. actual_remaining_balance=1 -> remaining_balance_millis = 250ms | ||
actual_balance = current_balance_millis * quota_per_millis | ||
if actual_balance >= Decimal(cost): | ||
actual_remaining_balance = actual_balance - Decimal(cost) | ||
remaining_balance_millis = actual_remaining_balance / quota_per_millis | ||
self.__wallet_floor_millis = wallet_ceiling_millis - remaining_balance_millis | ||
return True | ||
# No changes to the wallet state | ||
return False |
39 changes: 39 additions & 0 deletions
39
aws-opentelemetry-distro/src/amazon/opentelemetry/distro/sampler/_rate_limiting_sampler.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import Optional, Sequence | ||
|
||
from amazon.opentelemetry.distro.sampler._clock import _Clock | ||
from amazon.opentelemetry.distro.sampler._rate_limiter import _RateLimiter | ||
from opentelemetry.context import Context | ||
from opentelemetry.sdk.trace.sampling import Decision, Sampler, SamplingResult | ||
from opentelemetry.trace import Link, SpanKind | ||
from opentelemetry.trace.span import TraceState | ||
from opentelemetry.util.types import Attributes | ||
|
||
|
||
class _RateLimitingSampler(Sampler): | ||
def __init__(self, quota: int, clock: _Clock): | ||
self.__reservoir = _RateLimiter(1, quota, clock) | ||
self.borrowing = False | ||
|
||
# pylint: disable=no-self-use | ||
def should_sample( | ||
self, | ||
parent_context: Optional[Context], | ||
trace_id: int, | ||
name: str, | ||
kind: SpanKind = None, | ||
attributes: Attributes = None, | ||
links: Sequence[Link] = None, | ||
trace_state: TraceState = None, | ||
) -> SamplingResult: | ||
if self.__reservoir.try_spend(1, self.borrowing): | ||
return SamplingResult(decision=Decision.RECORD_AND_SAMPLE, attributes=attributes, trace_state=trace_state) | ||
return SamplingResult(decision=Decision.DROP, attributes=attributes, trace_state=trace_state) | ||
|
||
# pylint: disable=no-self-use | ||
def get_description(self) -> str: | ||
description = ( | ||
"RateLimitingSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}" | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
return description |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.