Skip to content

Commit

Permalink
Make shutdown function be called always (open-telemetry#2405)
Browse files Browse the repository at this point in the history
* Make shutdown function be called always

Fixes open-telemetry#2404

* Fix warning handling

* Log specific metric reader

Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
ocelotl and Alex Boten authored Jan 25, 2022
1 parent f0583e3 commit 1b7e161
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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

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"
)

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

0 comments on commit 1b7e161

Please sign in to comment.