Skip to content

Commit

Permalink
Check exceptions in force_flush too
Browse files Browse the repository at this point in the history
Fixes #2665
  • Loading branch information
ocelotl committed May 7, 2022
1 parent db9d40b commit 684e4c8
Showing 1 changed file with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,41 @@ def __init__(
def force_flush(self, timeout_millis: float = 10_000) -> bool:
deadline_ns = _time_ns() + timeout_millis * 10**6

metric_reader_error = {}

for metric_reader in self._sdk_config.metric_readers:
current_ts = _time_ns()
if current_ts >= deadline_ns:
raise Exception("Timed out while flushing metric readers")
metric_reader.collect(
timeout_millis=(deadline_ns - current_ts) / 10**6
try:
if current_ts >= deadline_ns:
raise Exception("Timed out while flushing metric readers")
metric_reader.collect(
timeout_millis=(deadline_ns - current_ts) / 10**6
)

# pylint: disable=broad-except
except Exception as error:

metric_reader_error[metric_reader] = error

if self._atexit_handler is not None:
unregister(self._atexit_handler)
self._atexit_handler = None

if metric_reader_error:

metric_reader_error_string = "\n".join(
[
f"{metric_reader.__class__.__name__}: {repr(error)}"
for metric_reader, error in metric_reader_error.items()
]
)

raise Exception(
(
"MeterProvider.force_flush failed because the following "
"metric readers failed during collect:\n"
f"{metric_reader_error_string}"
)
)
return True

Expand Down

0 comments on commit 684e4c8

Please sign in to comment.