Skip to content
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

Make shutdown function be called always #2405

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,25 @@ def shutdown(self):
_logger.warning("shutdown can only be called once")
return False

result = True
overall_result = True
ocelotl marked this conversation as resolved.
Show resolved Hide resolved

for metric_reader in self._sdk_config.metric_readers:
result = result and metric_reader.shutdown()
metric_reader_result = metric_reader.shutdown()

if not result:
_logger.warning("A MetricReader failed to shutdown")
if not metric_reader_result:
_logger.warning(
"MetricReader {metric_reader} failed to shutdown"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use f-string here?

)

overall_result = overall_result and metric_reader_result

self._shutdown = True

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

return result
return overall_result

def get_meter(
self,
Expand Down
24 changes: 24 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ def test_get_meter_duplicate(self):
self.assertIs(meter1, meter2)
self.assertIsNot(meter1, meter3)

def test_shutdown(self):

mock_metric_reader_0 = Mock(**{"shutdown.return_value": False})
mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

meter_provider = MeterProvider(
metric_readers=[mock_metric_reader_0, mock_metric_reader_1]
)

self.assertFalse(meter_provider.shutdown())
mock_metric_reader_0.shutdown.assert_called_once()
mock_metric_reader_1.shutdown.assert_called_once()

mock_metric_reader_0 = Mock(**{"shutdown.return_value": True})
mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

meter_provider = MeterProvider(
metric_readers=[mock_metric_reader_0, mock_metric_reader_1]
)

self.assertTrue(meter_provider.shutdown())
mock_metric_reader_0.shutdown.assert_called_once()
mock_metric_reader_1.shutdown.assert_called_once()

def test_shutdown_subsequent_calls(self):
"""
No subsequent attempts to get a `Meter` are allowed after calling
Expand Down