-
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
Merged
hectorhdzg
merged 8 commits into
Azure:main
from
hectorhdzg:feature/add-client-side-rate-limiter-for-export
May 22, 2026
+440
−14
Merged
[Monitor OpenTelemetry Exporter] Add client-side token bucket rate limiter for telemetry export #46999
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b423a37
Add client-side token bucket rate limiter for telemetry export
hectorhdzg 2b4ce8c
Address PR review: fix overflow double-persist, validate negative rat…
hectorhdzg 49c8b8e
Fix pylint warnings: implicit-str-concat and too-many-statements
hectorhdzg 44a2d77
Fix redirect double rate-limit: skip rate limiting on recursive _tran…
hectorhdzg 5d041d7
Fix black formatting issues in rate limiter and base exporter
hectorhdzg e553f5a
Retrigger CI checks
hectorhdzg 91c8d60
Retrigger CI checks (attempt 2)
hectorhdzg 1b6d94d
Merge branch 'main' into feature/add-client-side-rate-limiter-for-export
JacksonWeber 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
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.