Skip to content

Commit

Permalink
sdk: Flush metrics on exit (#749)
Browse files Browse the repository at this point in the history
In PushController before exit, flush the meter by calling tick(), ensuring that all metrics are flushed.

Co-authored-by: alrex <[email protected]>
  • Loading branch information
aabmass and alrex authored May 30, 2020
1 parent bfc54bf commit 602ddb0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def run(self):

def shutdown(self):
self.finished.set()
# Run one more collection pass to flush metrics batched in the meter
self.tick()
self.exporter.shutdown()
if self._atexit_handler is not None:
atexit.unregister(self._atexit_handler)
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry-sdk/tests/metrics/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,15 @@ def test_push_controller(self):
controller = PushController(meter, exporter, 5.0)
meter.collect.assert_not_called()
exporter.export.assert_not_called()

controller.shutdown()
self.assertTrue(controller.finished.isSet())
exporter.shutdown.assert_any_call()

# shutdown should flush the meter
self.assertEqual(meter.collect.call_count, 1)
self.assertEqual(exporter.export.call_count, 1)

def test_push_controller_suppress_instrumentation(self):
meter = mock.Mock()
exporter = mock.Mock()
Expand Down

0 comments on commit 602ddb0

Please sign in to comment.