Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 602ddb0

Browse files
aabmassalrex
andauthored
sdk: Flush metrics on exit (open-telemetry#749)
In PushController before exit, flush the meter by calling tick(), ensuring that all metrics are flushed. Co-authored-by: alrex <[email protected]>
1 parent bfc54bf commit 602ddb0

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/controller.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def run(self):
4444

4545
def shutdown(self):
4646
self.finished.set()
47+
# Run one more collection pass to flush metrics batched in the meter
48+
self.tick()
4749
self.exporter.shutdown()
4850
if self._atexit_handler is not None:
4951
atexit.unregister(self._atexit_handler)

opentelemetry-sdk/tests/metrics/export/test_export.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,15 @@ def test_push_controller(self):
597597
controller = PushController(meter, exporter, 5.0)
598598
meter.collect.assert_not_called()
599599
exporter.export.assert_not_called()
600+
600601
controller.shutdown()
601602
self.assertTrue(controller.finished.isSet())
602603
exporter.shutdown.assert_any_call()
603604

605+
# shutdown should flush the meter
606+
self.assertEqual(meter.collect.call_count, 1)
607+
self.assertEqual(exporter.export.call_count, 1)
608+
604609
def test_push_controller_suppress_instrumentation(self):
605610
meter = mock.Mock()
606611
exporter = mock.Mock()

0 commit comments

Comments
 (0)