-
Notifications
You must be signed in to change notification settings - Fork 869
fix(sdk): force_flush returns meaningful bool on MetricReader #5085
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
base: main
Are you sure you want to change the base?
Changes from all commits
9e0c780
5680779
1644453
b282129
9f369e9
3533f03
7f10555
ec604fe
17bab68
b45fe1d
260e72b
f24411a
719f80c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
| from logging import WARNING | ||
| from time import sleep, time_ns | ||
| from typing import Optional, cast | ||
| from unittest.mock import Mock | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
|
|
||
|
|
@@ -360,3 +360,52 @@ def test_metric_reader_metrics(self): | |
| self.assertTrue(name.startswith("periodic_metric_reader/")) | ||
|
|
||
| mp.shutdown() | ||
|
|
||
| def test_force_flush_returns_true_on_success(self): | ||
| exporter = FakeMetricsExporter() | ||
| pmr = self._create_periodic_reader(metrics, exporter) | ||
| result = pmr.force_flush(timeout_millis=5_000) | ||
| self.assertTrue(result) | ||
| pmr.shutdown() | ||
|
|
||
| def test_force_flush_returns_false_on_export_failure(self): | ||
| exporter = FakeMetricsExporter() | ||
| exporter.export = Mock(return_value=MetricExportResult.FAILURE) | ||
| pmr = self._create_periodic_reader(metrics, exporter) | ||
| result = pmr.force_flush(timeout_millis=5_000) | ||
| self.assertFalse(result) | ||
| pmr.shutdown() | ||
|
|
||
| def test_force_flush_skips_exporter_flush_when_collect_fails(self): | ||
| exporter = FakeMetricsExporter() | ||
| exporter.force_flush = Mock(return_value=True) | ||
| pmr = PeriodicExportingMetricReader( | ||
| exporter, export_interval_millis=math.inf | ||
| ) | ||
| # No collect callback registered → collect returns None → force_flush | ||
| # on base treats None as not-False (success), so wire up a failing one | ||
| exporter.export = Mock(return_value=MetricExportResult.FAILURE) | ||
|
|
||
| def _collect_failure(reader, timeout_millis): | ||
| return metrics | ||
|
|
||
| pmr._set_collect_callback(_collect_failure) | ||
| exporter.export = Mock(return_value=MetricExportResult.FAILURE) | ||
| result = pmr.force_flush(timeout_millis=5_000) | ||
| self.assertFalse(result) | ||
| exporter.force_flush.assert_not_called() | ||
| pmr.shutdown() | ||
|
|
||
| def test_detach_called_on_export_failure(self): | ||
| """detach(token) must run in finally even when export returns FAILURE.""" | ||
|
|
||
| exporter = FakeMetricsExporter() | ||
| exporter.export = Mock(return_value=MetricExportResult.FAILURE) | ||
| pmr = self._create_periodic_reader(metrics, exporter) | ||
|
|
||
| with patch( | ||
| "opentelemetry.sdk.metrics._internal.export.detach" | ||
| ) as mock_detach: | ||
|
ravitheja4531-cell marked this conversation as resolved.
|
||
| pmr.force_flush(timeout_millis=5_000) | ||
| pmr.shutdown() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the correct fix for the test.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un-resolving the comment since the fix is still not correct.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback. Could you suggest the correct approach? Should I remove the test_detach_called_on_export_failure test entirely, or is there a better way to verify detach is called without patching it globally?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still patch, but using |
||
| self.assertTrue(mock_detach.called) | ||
Uh oh!
There was an error while loading. Please reload this page.