fix: respect timeout_millis in BatchProcessor.force_flush (#4568)#4982
Open
chimchim89 wants to merge 3 commits intoopen-telemetry:mainfrom
Open
fix: respect timeout_millis in BatchProcessor.force_flush (#4568)#4982chimchim89 wants to merge 3 commits intoopen-telemetry:mainfrom
chimchim89 wants to merge 3 commits intoopen-telemetry:mainfrom
Conversation
…ut_exceeded to avoid flakiness on fast CI runners Fixes open-telemetry#4568.
7a97df8 to
e7f7b93
Compare
Contributor
|
Please add a changelog entry |
e7f7b93 to
3e1e396
Compare
Contributor
Author
yes. I added a changelog entry under ## Unreleased. |
herin049
requested changes
Mar 16, 2026
| def _export( | ||
| self, | ||
| batch_strategy: BatchExportStrategy, | ||
| flush_should_end: Optional[float] = None, |
Contributor
There was a problem hiding this comment.
Maybe we could go with something like
Suggested change
| flush_should_end: Optional[float] = None, | |
| deadline: float = math.inf, |
instead?
Comment on lines
+253
to
+259
| flush_should_end = ( | ||
| time.time() + (timeout_millis / 1000) | ||
| if timeout_millis is not None | ||
| else None | ||
| ) | ||
| # Blocking call to export. | ||
| self._export(BatchExportStrategy.EXPORT_ALL) | ||
| return True | ||
| return self._export(BatchExportStrategy.EXPORT_ALL, flush_should_end) |
Contributor
There was a problem hiding this comment.
Same here, it might be easier to just default to math.inf
Suggested change
| flush_should_end = ( | |
| time.time() + (timeout_millis / 1000) | |
| if timeout_millis is not None | |
| else None | |
| ) | |
| # Blocking call to export. | |
| self._export(BatchExportStrategy.EXPORT_ALL) | |
| return True | |
| return self._export(BatchExportStrategy.EXPORT_ALL, flush_should_end) | |
| deadline = ( | |
| time.time() + (timeout_millis / 1000) | |
| if timeout_millis is not None | |
| else math.inf | |
| ) | |
| # Blocking call to export. | |
| return self._export(BatchExportStrategy.EXPORT_ALL, deadline) |
| batch_processor.shutdown() | ||
|
|
||
| # pylint: disable=no-self-use | ||
| def test_force_flush_returns_false_when_timeout_exceeded( |
Contributor
There was a problem hiding this comment.
Can we just add some mocks here to test this behavior?
3e1e396 to
fa68592
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
force_flushinBatchProcessoraccepted atimeout_millisparameter but completely ignored it, blocking until all telemetry was exported regardless of the timeout passed by the caller.The fix has two parts:
_export()now accepts aflush_should_endparameter (an absolutetime.time()deadline). Before each batch export, itchecks whether the deadline has been reached and returns
Falseearly if so. ReturnsTruewhen all batches are exportednormally.
force_flush()now computes a deadline fromtimeout_millisand passes it to_export(), then propagates the result back tothe caller.
Note: the timeout is checked between batches, not during a single
exporter.export()call. Passing the timeout intoexporter.export()itself is a separate issue tracked in #4555.Fixes #4568
Type of change
How Has This Been Tested?
Three new unit tests were added to
opentelemetry-sdk/tests/shared_internal/test_batch_processor.py. Each test runs for bothBatchLogRecordProcessorandBatchSpanProcessorvia the existing@pytest.mark.parametrizedecorator (6 test runs total):test_force_flush_returns_true_when_all_exported— verifies thatforce_flushreturnsTruewhen all telemetry is exportedwithin the timeout.
test_force_flush_returns_false_when_timeout_exceeded— verifies thatforce_flushreturnsFalsewhen the deadline isexceeded before the queue is fully drained. Uses a slow exporter (
time.sleep(0.2)) with a 100ms timeout to reliably trigger thetimeout between batches.
test_force_flush_returns_false_when_shutdown— verifies thatforce_flushreturnsFalseimmediately when the processor isalready shut down and nothing is exported.
Ran new tests only:
Ran full test file to verify no regressions:
Ran ruff linter to verify style:
Ran tox to verify full lint and type check pipeline:
Does This PR Require a Contrib Repo Change?
Checklist: