-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Monitor OpenTelemetry Exporter] Add client-side token bucket rate limiter for telemetry export #46999
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
Open
hectorhdzg
wants to merge
3
commits into
Azure:main
Choose a base branch
from
hectorhdzg:feature/add-client-side-rate-limiter-for-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Monitor OpenTelemetry Exporter] Add client-side token bucket rate limiter for telemetry export #46999
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
64 changes: 64 additions & 0 deletions
64
...nitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_rate_limiter.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,64 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import logging | ||
| import threading | ||
| import time | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Default maximum envelopes per second across all telemetry types. | ||
| # This is a client-side safety cap to prevent self-inflicted overload | ||
| # of shared ingestion infrastructure during telemetry bursts. | ||
| _DEFAULT_MAX_ENVELOPES_PER_SECOND = 10000 | ||
|
|
||
| # Minimum allowed value to prevent misconfiguration | ||
| _MIN_MAX_ENVELOPES_PER_SECOND = 1 | ||
|
|
||
|
|
||
| class _TokenBucketRateLimiter: | ||
| """Thread-safe token bucket rate limiter for outbound telemetry. | ||
|
|
||
| The bucket refills at ``max_per_second`` tokens per second and holds | ||
| at most ``max_per_second`` tokens (i.e. one second of burst capacity). | ||
|
|
||
| :param float max_per_second: Maximum tokens (envelopes) allowed per second. | ||
| """ | ||
|
|
||
| def __init__(self, max_per_second: float) -> None: | ||
| if max_per_second < _MIN_MAX_ENVELOPES_PER_SECOND: | ||
| raise ValueError(f"max_per_second must be at least {_MIN_MAX_ENVELOPES_PER_SECOND}") | ||
| self._max_per_second = float(max_per_second) | ||
| self._tokens = self._max_per_second # start full | ||
| self._last_refill = time.monotonic() | ||
| self._lock = threading.Lock() | ||
|
|
||
| def try_consume(self, count: int) -> int: | ||
| """Try to consume *count* tokens from the bucket. | ||
|
|
||
| Returns the number of tokens actually consumed (i.e. how many | ||
| envelopes may be sent). The caller should handle the remainder | ||
| (e.g. store for retry or drop). | ||
|
|
||
| :param int count: Number of tokens requested. | ||
| :return: Number of tokens granted (<= *count*). | ||
| :rtype: int | ||
| """ | ||
| if count <= 0: | ||
| return 0 | ||
|
|
||
| with self._lock: | ||
| now = time.monotonic() | ||
| elapsed = now - self._last_refill | ||
| self._last_refill = now | ||
|
|
||
| # Refill tokens based on elapsed time, capped at bucket capacity | ||
| self._tokens = min( | ||
| self._max_per_second, | ||
| self._tokens + elapsed * self._max_per_second, | ||
| ) | ||
|
|
||
| granted = min(count, int(self._tokens)) | ||
| self._tokens -= granted | ||
|
|
||
| return granted |
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.